面试经典150题(5-7)

leetcode 150道题 计划花两个月时候刷完,今天(第二天)完成了三道(5-7)150:

  1. (169. 多数元素) 题目描述:
bash 复制代码
给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。

第一版(这个我是一次就写出来了,这个就是投票的简单类型,当然今天这个最多元素也可以排序后直接返回中间元素也行)

java 复制代码
class Solution {
    public int majorityElement(int[] nums) {
        int len=nums.length;
        if(len<=2){
            return nums[0];
        }
        int res=1;
        int temp=nums[0];
        for(int i=1;i<len;i++){
            if(temp==nums[i]){
                res++;
            }else{
                if(res==0){
                    res=1;
                    temp=nums[i];
                }
                res--;
            }
        }
        return temp;
    }
}
  1. (189. 轮转数组)题目描述:
bash 复制代码
给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。

第一版(确实刚开始没想到好办法,就用了数组暂存,然后登挪完其他的,把这些加到数组头部,但是这种题必须先求余!!)

java 复制代码
class Solution {
    public void rotate(int[] nums, int k) {
        int len=nums.length;
        if(len<=1)
            return ;
        int count=k%len;
        int[] temp=new int[count];
        for(int i=0;i<count;i++)
        {
            temp[i]=nums[i+len-count];
        }
        for(int i=len-count-1;i>=0;i--){
            nums[i+count]=nums[i];
        }
        for(int i=0;i<count;i++){
            nums[i]=temp[i];
        }
    }
}

第二版(看完解析恍然大悟,希望我下次再碰到了能直接恍然大悟)

java 复制代码
class Solution {
    public void rotate(int[] nums, int k) {
        int len=nums.length;
        if(len<=1)
            return;
        int count=k%len;
        if(count==0)
            return ;
        // 反转
        reserve(nums,0,len-1);
        // 再反转 前半部分(【0-count-1】)
        reserve(nums,0,count-1);
        // 再反转 后半部分(【count-len-1】)
        reserve(nums,count,len-1);
    }
    public void reserve(int[] nums,int first,int last){
        while(first<last){
            int temp=nums[last];
            nums[last--]=nums[first];
            nums[first++]=temp;
        }
    }
}
  1. (121. 买卖股票的最佳时机)题目描述:
bash 复制代码
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

第一版(这个题我也是已经刷了好多遍了,还是记不住啊,还是没思路就直接暴力求解了,但是 leetcode 能过,哈哈哈)

java 复制代码
class Solution {
    public int maxProfit(int[] prices) {
        int res=0;
        int temp=Integer.MAX_VALUE;
        for(int i=0;i<prices.length-1;i++){
            if(prices[i]>=temp)
                continue;
            temp=prices[i];
            for(int j=i+1;j<prices.length;j++){
                res=Math.max(res,prices[j]-prices[i]);
            }
        }
        return res;
    }
}

第二版(翻了解题的评论才明白了--"降价时候找最低价--涨价时候 算差价")

java 复制代码
class Solution {
    public int maxProfit(int[] prices) {
        // 降价时候找最低价--涨价时候 算差价 牛
        int minPrice=Integer.MAX_VALUE;
        int res=0;
        for(int price:prices){
            if(price<minPrice){
                minPrice=price;
            }else{
                res=Math.max(res,price-minPrice);
            }
        }
        return res;
    }
}

今天看了三个题有进步,加油!!!

我们的目标是早日跳槽!!!!

相关推荐
huapiaoy7 分钟前
Redis中数据类型的使用(hash和list)
redis·算法·哈希算法
冷白白20 分钟前
【C++】C++对象初探及友元
c语言·开发语言·c++·算法
鹤上听雷28 分钟前
【AGC005D】~K Perm Counting(计数抽象成图)
算法
睡觉然后上课38 分钟前
c基础面试题
c语言·开发语言·c++·面试
一叶祇秋41 分钟前
Leetcode - 周赛417
算法·leetcode·职场和发展
武昌库里写JAVA1 小时前
【Java】Java面试题笔试
c语言·开发语言·数据结构·算法·二维数组
ya888g1 小时前
GESP C++四级样题卷
java·c++·算法
Funny_AI_LAB1 小时前
MetaAI最新开源Llama3.2亮点及使用指南
算法·计算机视觉·语言模型·llama·facebook
NuyoahC1 小时前
算法笔记(十一)——优先级队列(堆)
c++·笔记·算法·优先级队列
jk_1011 小时前
MATLAB中decomposition函数用法
开发语言·算法·matlab