Leetcode Array刷題筆記

Array

1月9日

27,26,80 🔸 刪除元素 🔸 MID/EASY

雙指針操做數組java

189 🔸 旋轉數組 🔸 EASY

  • 三重reverse
  • 遞歸旋轉
  • 屢次轉動
  • 新建數組

41 🔸 第一個缺失正數 🔸 HARD

桶排序,把每一個數字放在對應的位置上算法

299 🔸 猜數字遊戲 🔸 MID

遍歷數組,找到在正確位置的,把其餘的存在新建的數組裏,再次遍歷數組數組

134 🔸 加油站 🔸 MID

若是remain不爲負,則必定有一個位置出發能走完整圈,若是i處remain爲負數則從i+1處開始bash

1月10日

274/275 🔸 H-index 🔸 MID

先sort,當citations[i]>=length-i的時候return len-i。其餘狀況return 0由於沒有such indexspa

217 🔸 存在重複元素 🔸 EASY

HashSet的元素不能重複,若是hashset add失敗則出現重複元素指針

55 🔸 Jump Game 🔸 MID

貪心算法,一直記錄最遠位置看是否涵蓋當前位置code

121 🔸 買賣股票1 🔸 EASY

隨時記錄和替換min和profitcdn

122 🔸 買賣股票2 🔸 EASY

貪心算法,找localmin和localMaxblog

123 🔸 買賣股票3 🔸 HARD

只容許買賣兩次,動態規劃 每個位置有四種狀況,能夠是排序

  • 第一次賣
  • 第一次買
  • 第二次賣
  • 第二次買
class Solution {
    public int maxProfit(int[] prices) {
        int buy1 = Integer.MIN_VALUE;
        int buy2 = Integer.MIN_VALUE;
        int sell1 = 0;
        int sell2 = 0;
        for(int price: prices){
            buy1 = Math.max(buy1, -price);
            sell1 = Math.max(sell1, buy1+price);
            buy2 = Math.max(buy2, sell1-price);
            sell2 = Math.max(sell2, buy2+price);
        }
        return sell2;
    }
}
複製代碼

11 🔸 乘最多的水 🔸 MID

雙指針,一塊兒向中間靠近,取代短的一邊

1月12日

42 🔸 接雨水 🔸 HARD

雙指針,每個元素若是大於peak則取代peak,若是小於peak則水量增長

public int trap(int[] height) {
        if(height==null || height.length<3) return 0;
        int left = 0;
        int right = height.length-1;
        int res = 0;
        int peakLeft = height[0];
        int peakRight = height[height.length - 1];
        while(left<=right){
            if(peakLeft<=peakRight){
                if(height[left] > peakLeft){
                    peakLeft = height[left];
                    left++;
                }else{
                    res+= peakLeft - height[left];
                    left++;
                }
            }else{
                if(height[right] > peakRight){
                    peakRight = height[right];
                    right--;
                }else{
                    res+= peakRight - height[right];
                    right--;
                }
            }
        } 
        return res;
    }
複製代碼

334 🔸 遞增三元子序列 🔸 MID

動態規劃,記錄下min1和min2,不停替換,若是大於min2則返回true

1月14日

128 🔸 最長連續序列 🔸 HARD

利用HashSet的contains和add方法爲O(1)的特性

public int longestConsecutive(int[] nums) {
        if(nums.length<2 || nums==null) return nums.length;
        int res = 0;
        int cur = 0;
        HashSet<Integer> set = new HashSet<>();
        for(int num: nums){
            set.add(num);
        }
        for(int i=0; i<nums.length; i++){
            int low = nums[i] - 1;
            if(!set.contains(low)){
                int temp = nums[i];
                while(set.contains(temp)){
                    cur++;
                    temp++;
                }
                res = Math.max(res,cur);
                cur = 0;
            }
        }
        return res;
    }
複製代碼

4 🔸 尋找兩個有序數組的中位數 🔸 HARD

兩個指針分別指向兩個array的開頭,每次較小的前進一位

public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int len = nums1.length+nums2.length;
        int count = 1;
        int odd = 0;
        int index1 = 0;
        int index2 = 0;
        if(len%2!=0){
            odd = 1;
        }
        if(odd==1){//when length is odd
            int res = 0;
            while(count<=(len/2+1)){
                if(index1>=nums1.length){
                    res = nums2[index2];
                    index2++;
                    count++;
                }else if(index2>=nums2.length){
                    res = nums1[index1];
                    index1++;
                    count++;
                }else{
                    if(nums1[index1]>=nums2[index2]){
                        res = nums2[index2];
                        index2++;
                        count++;
                    }else{
                        res = nums1[index1];
                        index1++;
                        count++;
                    }
                }
            }
            return res;
        }else{
            int res1 = 0;
            int res2 = 0;
            while(count<=(len/2+1)){
                if(index1>=nums1.length){
                    res2 = res1;
                    res1 = nums2[index2];
                    index2++;
                    count++;
                }else if(index2>=nums2.length){
                    res2 = res1;
                    res1 = nums1[index1];
                    index1++;
                    count++;
                }else{
                    if(nums1[index1]>=nums2[index2]){
                        res2 = res1;
                        res1 = nums2[index2];
                        index2++;
                        count++;
                    }else{
                        res2 = res1;
                        res1 = nums1[index1];
                        index1++;
                        count++;
                    }
                }
            }
            double res = (double)(res1+res2)/2;
            return res;
        }
    }
複製代碼

53 🔸 最大子序和 🔸 EASY

Math.max(beforeSum+currentElement,currentElement);
複製代碼

209 🔸 長度最小的子數組 🔸 MID

滑窗算法,雙指針一個控制窗子左邊一個控制窗子右邊

238 🔸 除自身之外數組的乘積 MID

用兩個array儲存從前日後的到i的乘積和從i開始的乘積 i的值等於到i-1的乘積乘上從i+1開始的乘積

1月15日

152 🔸 乘積最大子序列 🔸 MID

和最大子序和的原理類似,可是要考慮到乘積有正負,因此要有beforeMax和beforeMin

228 🔸 Summary Range 🔸 MID ♍️

雙指針,遇到不連續的就add進res而後重置指針

88 🔸 Merge Sorted Array 🔸 EASY

  • 兩個指針分別指向兩個array開頭而後每次把較小的傳入新array,再賦值回來♍️
  • 因爲nums1[]的space足夠大,能夠從後往前賦值,這樣的space comlexity 是O(1)

75 🔸 Sort Colors 🔸 MID

  • Arrays.sort()
  • 雙指針,while循環(於桶排序找first missing positive相似)

283 🔸 移動零 🔸 EASY♍️

相似桶排序思想,遇到非零的數就與current ptr換位置

1月16日

239 🔸 滑動窗口最大值 🔸 HARD ♍️

left和right指針表明窗戶兩端,currMax表明當前窗內最大值,若是滑動後移除的是最大值則從新搜索新窗,若是不是最大值,則currMax = Math.max(nums[left-1],nums[right])

1月17日

376 🔸 擺動序列 🔸 MID

若是當前值大於前一項的值且前一項爲valley則當前項爲新peak,反之亦然

class Solution {
    public int wiggleMaxLength(int[] nums) {
        if(nums.length<2) return nums.length;
        int count = 1;
        Boolean peak = null;
        for(int i=1; i<nums.length; i++){
            if(nums[i]>nums[i-1] && (peak==null || peak==false)){
                peak = true;
                count++;
            }else if(nums[i]<nums[i-1] && (peak==null || peak==true)){
                peak = false;
                count++;
            }
        }
        return count;
    }
}
複製代碼

45 🔸 跳躍遊戲2 🔸 HARD

    1. 動態規劃->創建新數組,for i,更新i能到達的每一位的minimum jump,效率低 ♍️
    1. 更好的辦法?

118 楊輝三角 EASY

給定一個非負整數 numRows,生成楊輝三角的前 numRows 行。
輸入: 5
輸出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
複製代碼

重點:經過儲存前一列來省去兩個get() function提高效率

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        if(numRows == 0) return list;
        List<Integer> pre = new ArrayList<>();
        pre.add(1);
        list.add(pre);
        for(int i=1; i<numRows; i++){
            List<Integer> line = new ArrayList<>();
            line.add(1);
            for(int j=1;j<i;j++){
                line.add(pre.get(j-1) + pre.get(j));
            }
            line.add(1);
            pre = line;
            list.add(line);
        }
        
        return list;
    }
}
複製代碼
相關文章
相關標籤/搜索