• 力扣面试经典100题


    进阶,其他解法

    数组

    88. 合并两个有序数组 - 力扣(LeetCode)

    1、按非递减顺序合并两个数组

    从末尾开始,用while分没到两个数组头,到第一个数组头,到第二个数组头三种情况

    1. class Solution {
    2. public:
    3. void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
    4. int i=m-1,j=n-1,k=m+n-1;
    5. while(i>=0&&j>=0){
    6. nums1[k--]=nums1[i]>nums2[j]?nums1[i--]:nums2[j--];
    7. }
    8. while(i>=0){
    9. nums1[k--]=nums1[i--];
    10. }
    11. while(j>=0){
    12. nums1[k--]=nums2[j--];
    13. }
    14. }
    15. };

     2、移除掉某个数值的元素,无序

    27. 移除元素 - 力扣(LeetCode)

    借助一个新标号

    1. class Solution {
    2. public:
    3. int removeElement(vector<int>& nums, int val) {
    4. //双指针法
    5. int slowIndex=0;
    6. for(int i=slowIndex;isize();i++){
    7. if (val!=nums[i]){
    8. nums[slowIndex++]=nums[i];//先赋值,再++
    9. }
    10. }
    11. return slowIndex;
    12. }
    13. };

    3、删除有序数组中的重复项

    26. 删除有序数组中的重复项 - 力扣(LeetCode)

    同样用一个标号,在原数组覆盖(序列长度有变化,且后面的元素不重要)同2

    1. class Solution {
    2. public:
    3. int removeDuplicates(vector<int>& nums) {
    4. int index=1;
    5. for(int i=1;isize();i++){
    6. if(nums[i]!=nums[i-1]){
    7. nums[index++]=nums[i];
    8. }
    9. }
    10. return index;
    11. }
    12. };

    4、80. 删除有序数组中的重复项 II - 力扣(LeetCode)

    使得出现次数超过两次的元素只出现两次,记录相等的次数

    1. class Solution {
    2. public:
    3. int removeDuplicates(vector<int>& nums) {
    4. if(nums.size()<=2)
    5. return nums.size();
    6. int index=1;
    7. int count=0;
    8. for(int i=1;isize();i++){
    9. if(nums[i]==nums[i-1]){
    10. count++;
    11. if(count<=1){
    12. nums[index++]=nums[i];
    13. }
    14. }
    15. else{
    16. count=0;
    17. nums[index++]=nums[i];
    18. }
    19. }
    20. return index;
    21. }
    22. };

    5、169. 多数元素 - 力扣(LeetCode)

    数组中出现次数超过一半的数字” 被称为 “众数” 

    此数字出现次数大于所有其他数字出现次数

    1. class Solution {
    2. public:
    3. int majorityElement(vector<int>& nums) {
    4. int x=0,votes=0;
    5. for(int num:nums){
    6. if(votes==0) x=num;
    7. votes+=x==num?1:-1;
    8. }
    9. return x;
    10. }
    11. };

    6、189. 轮转数组 - 力扣(LeetCode) 

    (i+k)%n,新建数组

    1. class Solution {
    2. public:
    3. void rotate(vector<int>& nums, int k) {
    4. int n=nums.size();
    5. vector<int> tmp(n);
    6. for(int i=0;i
    7. tmp[(i+k)%n]=nums[i];
    8. }
    9. nums.assign(tmp.begin(),tmp.end());
    10. }
    11. };

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

    某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票

    1. class Solution {
    2. public:
    3. int maxProfit(vector<int>& prices) {
    4. int pre =prices[0],ans=0;
    5. for(auto &m:prices){
    6. pre=min(pre,m);//pre来选极小值,越小越好
    7. ans=max(ans,m-pre);//ans来选抛售的极大值
    8. }
    9. return ans;
    10. }
    11. };

    8、122. 买卖股票的最佳时机 II - 力扣(LeetCode)------贪心解法

    可购买出售多次,策略:只要不赔钱就一直买卖

    1. class Solution {
    2. public:
    3. int maxProfit(vector<int>& prices) {
    4. int profit=0;
    5. for(int i=1;isize();i++){
    6. int tmp=prices[i]-prices[i-1];
    7. if(tmp>0) profit+=tmp;
    8. }
    9. return profit;
    10. }
    11. };

    9、55. 跳跃游戏 - 力扣(LeetCode)

    数组中的每个元素代表你在该位置可以跳跃的最大长度。

    策略:看是否可以跳过最大长度为0 的位置

    未成清贫难成人,不经挫折永天真 ;人情似纸张张薄,世事如棋局局新。

    1. class Solution {
    2. public:
    3. bool canJump(vector<int>& nums) {
    4. int step=1;
    5. int i=0;
    6. int n=nums.size()-1;
    7. for(int i=n-1;i>=0;i--){
    8. if(nums[i]>=step){
    9. step=0;
    10. }
    11. step++;
    12. }
    13. return step==1;
    14. }
    15. };

    贪心解法:判断并留下   之前走的最远的策略    和   当前策略    中最好的,若最好的去不到循环中下一个位置,则失败。

    1. class Solution {
    2. public:
    3. bool canJump(vector<int>& nums) {
    4. int rightMost=0;
    5. int n=nums.size();
    6. for(int i=0;i
    7. if(i<=rightMost){
    8. rightMost=max(rightMost,i+nums[i]) ;
    9. if(rightMost>=n-1) return true;
    10. }
    11. }
    12. return false;
    13. }
    14. };

    10、45. 跳跃游戏 II - 力扣(LeetCode)

    要求:返回到达 nums[n - 1] 的最小跳跃次数

    //数组[2,3,1,2,4,2,3]

    //下标 0 1 2 3 4 5 6

    策略:先看每个位置能跳的最远的职位级别;若看完了当前水平能看的公司,跑路一次(第一次可能在0,可能在1),当前水平增加

    1. class Solution {
    2. public:
    3. int jump(vector<int>& nums) {
    4. int ans = 0; //跳槽次数
    5. int curUnlock = 0; //当前你的水平能入职的最高公司级别
    6. int maxUnlock = 0; //当前可选公司最多能帮你提到几级
    7. for (int i = 0; i < nums.size() - 1; i++) { //从前向后遍历公司,最高级公司(nums.length-1)是目标,入职后不再跳槽,所以不用看,故遍历范围是左闭右开区间[0,nums.length-1)
    8. maxUnlock = max(maxUnlock, i + nums[i]); //计算该公司最多能帮你提到几级(公司级别i+成长空间nums[i]),与之前的提级最高记录比较,打破记录则更新记录
    9. if (i == curUnlock) { // 把你当前水平级别能选的公司都看完了,你选择跳槽到记录中给你提级最多的公司,以解锁更高级公司的入职权限
    10. curUnlock = maxUnlock; // 你跳槽到了该公司,你的水平级别被提升了
    11. ans++; //这里记录你跳槽了一次
    12. }
    13. if(curUnlock>=nums.size()-1) break;
    14. }
    15. return ans; //返回跳槽总次数
    16. }
    17. };

    11、274. H 指数 - 力扣(LeetCode)

    看引用次数

    策略:从高往低看,

    1. int cmp(int*a,int *b){
    2. return *a-*b;
    3. }
    4. int hIndex(int* citations, int citationsSize) {
    5. qsort(citations,citationsSize,sizeof(int),cmp);
    6. int h=0,i=citationsSize-1;
    7. while(i>=0&&citations[i]>h){
    8. h++;
    9. i--;
    10. }
    11. return h;
    12. }

    12、380. O(1) 时间插入、删除和获取随机元素 - 力扣(LeetCode)

  • 相关阅读:
    springboot+rocketmq(4):实现延时消息
    C语言字符函数和字符串函数详解
    算法入门教程(三、递推与递归)
    Google终于开始革C++的命了!
    【Python学习笔记】Python近期总结
    .360、.halo勒索病毒数据怎么处理|数据解密恢复
    oracle 自定义存储过程(非常简单明了)
    由一亿多条仇恨言论训练后,这个AI机器人成了恶毒的“键盘侠”
    一、C语言[指针]
    动态规划:回文串问题(C++)
  • 原文地址:https://blog.csdn.net/m0_47239466/article/details/141095949