LeetCode面试经典150最优答案汇总

系列博客目录



数组/字符串

88.合并两个有序数组

class Solution {
   
   
    public void merge(int[] nums1, int m, int[] nums2, int n) {
   
   
        int p1 = 0;
        int p2 = 0;
        int[] res = new int[m+n];
        int cur;
        while(p1 < m || p2 < n){
   
   
            if(p2 == n){
   
   
                cur = nums1[p1++];
            }else if (p1 == m) {
   
   
                cur = nums2[p2++];
            }else if (nums1[p1] < nums2[p2]){
   
   
                cur = nums1[p1++];
            }else {
   
   
                cur = nums2[p2++];
            }
            res[p1 + p2 -1] = cur;
        }
        for (int i = 0; i < res.length; i++) {
   
   
            nums1[i] = res[i];
        }

    }
}

27.移除元素

class Solution {
   
   
    public int removeElement(int[] nums, int val) {
   
   
        int left = 0;
        int right = nums.length-1;
        while(left<=right){
   
   
            if(nums[left] == val){
   
   
                nums[left] = nums[right--];

            }else {
   
   
                left++;
            }

        }
        return left;
    }
}

26.删除有序数组中的重复项

class Solution {
   
   
    public int removeDuplicates(int[] nums) {
   
   
        int fast = 1;
        int slow = 1;
        while(fast<nums.length){
   
   
            if(nums[fast]!=nums[fast-1]){
   
   
                nums[slow++] = nums[fast++];
            }else {
   
   
                fast++;
            }
        }
        return  slow;
    }
}

80.删除有序数组中的重复项 II

class Solution {
   
   
    public int removeDuplicates(int[] nums) {
   
   
      int fast = 2;
      int slow = 2;
      while(fast < nums.length){
   
   
          if(nums[fast]!=nums[slow-2]){
   
   
              nums[slow++] = nums[fast++];
          }else {
   
   
              fast++;
          }
      }
      return slow;
    }
}

169.多数元素

class Solution {
   
   
    public int majorityElement(int[] nums) {
   
   
        Arrays.sort(nums);
        return nums[nums.length/2];
    }
}

189.轮转数组

class Solution {
   
   
    public void rotate(int[] nums, int k){
   
   
    k = k%nums.length;
    reverse(nums,0,nums.length-1);
    reverse(nums,0,k-1);
    reverse(nums, k,nums.length-1);

    }
    public void reverse(int[] nums, int s, int e){
   
   
        while(s<e){
   
   
            int temp = nums[s];
            nums[s] = nums[e];
            nums[e] = temp;
            s++;
            e--;
        }
    }
}

121

class Solution {
   
   
    public int maxProfit(int[] prices) {
   
   
        int low = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < prices.length; i++) {
   
   
            if(prices[i]<low) low = prices[i];
            max = Math.max(prices[i] - low , max);
        }
        return max;
    }
}

122

class Solution {
   
   
    public int maxProfit(int[] prices) {
   
   
        int val = 0;
        if (prices.length < 2) return val;
        for (int i = 1; i < prices.length; i++) {
   
   
            if (prices[i] > prices[i-1]) {
   
   //提高代码效率
                val += prices[i] - prices[i-1];
            }
        }
        return val;
    }
}

55

class Solution {
   
   
    public boolean canJump(int[] nums) {
   
   
        int index = 0;
        while (true) {
   
   
            int length = nums[index];
            int max = Integer.MIN_VALUE;
            int maxIndex = 0;
            for (int j = index + 1; j <= index + length && j<nums.length ; j++) {
   
   
                if(nums[j] + j > max){
   
   
                    max = nums[j]+j;
                    maxIndex = j;
                }
            }
            if(nums[maxIndex]+maxIndex >= nums.length -1 ) return true;
            if(maxIndex == 0) return false;
            index = maxIndex;
        }

    }
}

45

class Solution {
   
   
    public int jump(int[] nums) {
   
   
        int step = 0;
        int index = 0;
        while(index<nums.length-1){
   
   
            step++;
            int length = nums[index];
            int max = 0;
            int next = index;
            for (int i = length; i >= 1; i--) {
   
   
                if(index+i>=nums.length-1) return step;
                if(nums[index+i]+i>max){
   
   
                    next = index + i;
                    max = nums[index+i] + i;
                }
            }
            index = next;
        }
        return 0;
    }
}

274

链接

class Solution {
   
   
    public int hIndex(int[] citations) {
   
   
        Arrays.sort(citations);
        int res = 0;
        for(int i = citations.length - 1;i>=0;i--){
   
   
            if(citations[i] >= citations.length - i){
   
   
                res = citations.length - i;
            }
        }
        return res;
    }
}

238

class Solution {
   
   
    public int[] productExceptSelf(int[] nums) {
   
   
        int[] L = new int[nums.length];
        int[] R = new int[nums.length];
        int[] answer = new int[nums.length];
        L[0] = 1;
        for (int i = 1; i < nums.length; i++
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值