• 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);
    }
    }
    }
    }

    }
  • 相关阅读:
    cookie和session区别
    jsp编写页面时常见错误提示
    web处理jsp文件的三个阶段
    JSP页面元素
    B/S与C/S区别
    url组成部分
    css选择器
    ol,ul,dl,table标签的基本语法
    剑指offer-把二叉树打印成多行
    剑指offer-从上往下打印二叉树
  • 原文地址:https://www.cnblogs.com/gylhaut/p/8670822.html
Copyright © 2020-2023  润新知