Fork me on GitHub

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;
    }
}
posted @ 2017-12-25 23:05  郭耀华  阅读(324)  评论(0编辑  收藏  举报