• LintCode 练习题


    import java.util.ArrayList;
    import java.util.List;


    // This is the interface that allows for creating nested lists.
    // You should not implement it, or speculate about its implementation

    interface NestedInteger {

    // @return true if this NestedInteger holds a single integer,
    // rather than a nested list.
    public boolean isInteger();

    // @return the single integer that this NestedInteger holds,
    // if it holds a single integer
    // Return null if this NestedInteger holds a nested list
    public Integer getInteger();

    // @return the nested list that this NestedInteger holds,
    // if it holds a nested list
    // Return null if this NestedInteger holds a single integer
    public List<NestedInteger> getList();
    }



    public class Solution {

    private int partition(int[] array, int lo, int hi){
    //固定的切分方式
    int key =array[lo];
    while (lo<hi){
    while (array[hi]>=key&&hi>lo){
    hi--;
    }
    if(lo <hi){
    //交换
    int temp = array[lo];
    array[lo] = array[hi];
    array[hi] = temp;
    lo++;
    }
    while (array[lo]<=key&& hi>lo){
    lo++;
    }
    if(lo <hi){
    //交换
    int temp = array[lo];
    array[lo] = array[hi];
    array[hi] = temp;
    hi--;
    }
    }

    return hi;
    }

    public void sort(int[] array,int lo, int hi){
    if(lo>=hi){
    return;
    }
    int index = partition(array,lo,hi);
    sort(array,lo,index-1);
    sort(array,index+1,hi);
    }

    /*
    * @param dictionary: an array of strings
    * @return: an arraylist of strings
    */
    public List<String> longestWords(String[] dictionary) {
    // write your code here
    List<String> words = new ArrayList<>();
    if(dictionary == null){
    return null;
    }else {
    int len=0;
    for (int i=0;i < dictionary.length;i++){
    if(dictionary[i].length() >len){
    words.clear();
    words.add(dictionary[i]);
    len = dictionary[i].length();
    continue;
    }
    if(dictionary[i].length() == len){
    words.add(dictionary[i]);
    }

    }
    }
    return words;
    }

    /**
    * 求数据中间值
    * @param nums
    * @return
    */
    public int median(int[] nums) {
    // write your code here
    int count = nums.length;
    if(count/2==0) {
    //偶数
    int target = nums[count/2];
    for(int i=0;i<count;i++){
    if(nums[i]<target){
    target = nums[i];
    }

    }

    }else {
    //奇数
    for(int i=0;i<count;i++){

    }
    }
    return 0;

    }

    /**
    * @param nums: A list of integers
    * @return: A integer indicate the sum of max subarray
    */
    public int maxSubArray(int[] nums) {
    // write your code here
    int max=nums[0];
    for(int i=0;i<nums.length;i++)
    {
    if(max<nums[i])
    max=nums[i];
    }
    if(max<0)return max;
    int sum=0;
    max=0;
    for(int i=0;i<nums.length;i++)
    {
    sum+=nums[i];
    if(sum<0)sum=0; //子串为负 ,丢掉
    if(sum>max)
    max=sum;
    }
    return max;
    }

    // @return a list of integer
    // @param nestedList a list of NestedInteger
    public List<Integer> flatten(List<NestedInteger> nestedList) {
    // Write your code here
    List<Integer> list = new ArrayList<>();
    doflatten(nestedList,list);
    return list;
    }

    private void doflatten(List<NestedInteger> nestedList,List<Integer> list){
    if(nestedList != null){
    for(NestedInteger nested : nestedList){
    if(nested.isInteger()){
    list.add(nested.getInteger());
    }else {
    doflatten(nested.getList(),list);
    }
    }
    }
    }

    }
  • 相关阅读:
    VMware + CentOS 7搭建环境(二)
    VMware + CentOS 7搭建环境(一)
    电脑清理的问题整理
    windows下搭建vue+webpack的开发环境
    git使用指南
    如何使用前端技术设置地理围栏?
    js点击按钮button效果(波效果)
    计算两个日期时间之间的时间差:28小时38分钟
    jQuery实现的全选、反选和获取当前所有选中的值功能
    vue恼人的node_modules目录删除方法
  • 原文地址:https://www.cnblogs.com/gylhaut/p/8670822.html
Copyright © 2020-2023  润新知