Leetcode (力扣)做题记录 hot100(34,215,912,121)

发布于:2025-05-13 ⋅ 阅读:(10) ⋅ 点赞:(0)
力扣第34题:在排序数组中查找第一个数和最后一个数

34. 在排序数组中查找元素的第一个和最后一个位置 - 力扣(LeetCode)

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int[] arr = new int[2];
        arr[0] = -1;
        arr[1] = -1;
        while(left <= right){
            int mid= (left + right) /2;
            if(nums[mid] == target){
                arr[0] = mid;
                arr[1] = mid;
                int temp = mid;
                while(temp > 0 && nums[temp - 1] == target){
                    temp --;
                }
                arr[0] = temp;
                temp = mid;
                while(temp<nums.length -1 && nums[temp +1] == target){
                    temp ++;
                }
                arr[1] = temp;
                    return arr;
            }
            else if (nums[mid] < target){
                left = mid +1;
            }
            else{
                right = mid -1;
            }
        }
        return arr;
    }
}
力扣第215题:数组中的第K个最大元素

215. 数组中的第K个最大元素 - 力扣(LeetCode)

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int n = nums.length;
       return quikSort(nums,0,n-1,n-k);
    }
    private int quikSort(int[] nums,int l,int r ,int k){
        if(l == r) return nums[k];
        int x = nums[l];
        int i =l -1;
        int j = r +1;
        //分区
        while(i <j){
            //bix小
            do i++;while(nums[i] < x);
            do j--;while(nums[j] > x);
            if(i < j){
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
           
        }
         if(k <= j){
                return quikSort(nums,l,j,k);
            }
            else{
                return quikSort(nums,j+1,r,k);
            }
    }
}
力扣第912题:排序数组

912. 排序数组 - 力扣(LeetCode)

class Solution {
    public int[] sortArray(int[] nums) {
      quikSort(nums,0,nums.length - 1);
      return nums;
    }
    private void quikSort(int[] nums,int l ,int r){
        if(l >= r){
            return;
        }
        int x= nums[l];
        int i = l-1;
        int j = r +1;
        while(i<j){
            do i++;while(x > nums[i]);
            do j--;while(x < nums[j]);
            if(i< j){
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        } 
        quikSort(nums,l,j);
        quikSort(nums,j+1,r);

    }
}

力扣第121题:买卖股票的最佳时机

121. 买卖股票的最佳时机 - 力扣(LeetCode)

class Solution {
    public int maxProfit(int[] prices) {
        int min = prices[0];
        int max = 0;
        for(int i = 1;i < prices.length ;i++){
            if(min > prices[i]){
                min = prices[i];
            }
            else{
                max =Math.max(max, prices[i] - min);
            }
        }
        return max;
    }
}


 本文相关图片资源来自于网络中,如有侵权请联系删除!