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

力扣第34题:在排序数组中查找第一个数和最后一个数

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

java 复制代码
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)

java 复制代码
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)

java 复制代码
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)

java 复制代码
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;
    }
}
复制代码

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

相关推荐
坚持就完事了9 分钟前
数据结构之链表
数据结构·python·算法·链表
c#上位机24 分钟前
halcon图像去噪—均值滤波
图像处理·算法·均值算法·halcon
曾几何时`40 分钟前
347. 前 K 个高频元素 分别使用sort和priority_queue 对哈希结构自定义排序
算法
小李小李快乐不已1 小时前
图论理论基础(3)
数据结构·c++·算法·图论
牙牙要健康1 小时前
【open3d】示例:自动计算点人脸点云模型面部朝向算法
人工智能·python·算法
youngee111 小时前
hot100-41二叉搜索树中第K小的元素
算法
mmz12072 小时前
双指针问题5(c++)
c++·算法
星空露珠2 小时前
lua获取随机颜色rgb转换hex
数据结构·数据库·算法·游戏·lua
mit6.8242 小时前
预hash|vector<int> dfs
算法
Zsy_0510032 小时前
【数据结构】堆简单介绍、C语言实现堆和堆排序
c语言·数据结构·算法