zoukankan      html  css  js  c++  java
  • leetcode第一天

    leetcode 第一天
    2017年12月24日
    第一次刷leetcode真的是好慢啊,三道题用了三个小时,而且都是简单题。

    数组

    1.(674)Longest Continuous Increasing Subsequence

    JAVA
    class Solution {
        public int findLengthOfLCIS(int[] nums) {
            int tem = 1 , length = 1;
            if(nums.length==0) return 0;
            for (int i = 0;i<nums.length -1;i++){
                if ( nums[i+1] - nums[i] > 0){
                    tem ++;
                    length = Math.max(tem,length);
                }else{
                   tem = 1;
                }
            }
            return length;
        }
    }
    
    Python
    class Solution(object):
        def findLengthOfLCIS(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            max_len = tem_len = 1
            if len(nums)==0:
                return 0
            for i in range(len(nums)-1):
                if nums[i+1] >nums [i]:
                    tem_len += 1
                    max_len = max(tem_len,max_len)
                else:
                    tem_len = 1
            return max_len
    

    2.(283)Move Zeroes

    JAVA
    class Solution {
        public void moveZeroes(int[] nums) {
            int pointer = 0;
            for (int i = 1; i<nums.length;i++){
                if(nums[i]-nums[pointer] == nums[i]){
                    if(nums[i] != 0){
                        nums[pointer] = nums[i];
                        nums[i]=0;
                        pointer++;
                    }
                }else pointer++;
            }
        }
    }
    
    Python
    class Solution(object):
        def moveZeroes(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            pointer = 0
            for i in range(len(nums)):
                if(nums[i]-nums[pointer] == nums[i]):
                    if(nums[i]!=0):
                        nums[pointer] = nums[i]
                        nums[i] = 0
                        pointer +=1
                else:
                    pointer+=1
    

    3.(581)Shortest Unsorted Continuous Subarray


    算法:1.先找到排序错误子集的起止点和终止点。2.计算该子集内的最大值最小值。3.判断前部分序列是否有大于子集最小值的部分,后部分序列是否有小于最大值的部分。如果有则修改子序列起始点终点

    JAVA
    class Solution {
        public int findUnsortedSubarray(int[] nums) {
            boolean isFirst = true;
            int left = 0,right = 0,min = Integer.MAX_VALUE,max=Integer.MIN_VALUE;
            for(int i = 0;i<nums.length-1;i++){
                if(nums[i+1] < nums[i]){
                    if(isFirst){
                        left = i;
                        right = i+1;
                        isFirst = false;
                    }else{
                        right = i+1;
                    } 
                }
            }
            for (int i = left;i<=right;i++){
                min = Math.min(min,nums[i]);
                max = Math.max(max,nums[i]);
            }
            for(int i=0;i <= left;i++){
                if(nums[i] > min){
                    left = i;
                }
            }
            for(int i = nums.length-1 ; i>=right;i--){
                if(nums[i]<max){
                    right = i;
                    
                }
            }
            return right==0?0:right-left+1;
        }
    }
    
  • 相关阅读:
    6月11日 python学习总结 框架理论
    6月7日 python 复习 collections
    6月6日 python复习 面向对象
    6月6日 python学习总结 jQuery (三)
    6月5日 python复习 模块
    6月5日 python学习总结 jQuery (二)
    6月4日 python学习总结 装饰器复习
    Redis源码分析(五)--- sparkline微线图
    Redis源码分析(六)--- ziplist压缩列表
    Redis源码分析(六)--- ziplist压缩列表
  • 原文地址:https://www.cnblogs.com/guoyaohua/p/8111681.html
Copyright © 2011-2022 走看看