HOT100与剑指Offer

文章目录


前言

一个本硕双非的小菜鸡,备战24年秋招,计划刷完hot100和剑指Offer的刷题计划,加油!

根据要求,每一道题都要写出两种以上的解题技巧。

一、283. 移动零(HOT100)

283. 移动零

Note:快慢指针解题,慢指针指向0的最前一位,快指针遍历,如果快指针指向非0,就与慢指针指向交换(把0换到最后),慢指针再++。

cpp 复制代码
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int size = nums.size();
        int slowIndex = 0;

        for (int fastIndex = 0; fastIndex < size; fastIndex++) {
            if (nums[fastIndex] != 0) {
                swap(nums[fastIndex], nums[slowIndex]);
                slowIndex++;
            }
        }
    }
};

遍历整个数组,找出所有非零元素并复制到前面,然后剩下位数补0

Note:

cpp 复制代码
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int size = nums.size();
        int j = 0;
        for (int i = 0; i < size; i++) {
            if (nums[i] != 0) {
                nums[j] = nums[i];
                j++;
            }
        }
        
        while (j < size)
            nums[j++] = 0;
    }
};

二、11.旋转数组的最小数字(剑指Offer)

旋转数组的最小数字

Note:剑指offer的写法,有点小麻烦

旋转数组可以划分为两个排序的子数组。用二分查找法,设定一个中间节点。如果中间元素位于前面的递增子数组,那么它应该大于或者等于第一个指针的元素。此时数组中最小的元素应该位于该中间元素的后面。

同样,如果中间元素位于后面的递增子数组,那么它应该小于或者等于第二个指针的元素。此时数组中最小的元素应该位于该中间元素的前面。

最终第一个指针指向前面子数组的最后一个元素,第二个指针会指向后面子数组的第一个元素,也就是最小的元素。

有一个特殊情况,三数相同时必须顺序查找

cpp 复制代码
class Solution {
public:
    int minOrder(vector<int>& nums, int startIndex, int endIndex) {
        int res = nums[startIndex];
        
        for (int i = startIndex + 1; i <= endIndex; i++) {
            if (res > nums[i])
                res = nums[i];
        }
        return res;
    }

    int findMin(vector<int>& nums) {
        int size = nums.size();
        if (size == 0) return -1;
        
        int startIndex = 0;
        int endIndex = size - 1;
        int middle = startIndex;
        while (nums[startIndex] >= nums[endIndex]) {
            if (endIndex - startIndex == 1) {
                middle = endIndex;
                break;
            }
            middle = startIndex + ((endIndex - startIndex) / 2);
            
            if (nums[startIndex] == nums[endIndex] && nums[middle] == nums[startIndex])
                return minOrder(nums, startIndex, endIndex);
            if (nums[middle] >= nums[startIndex])
                startIndex = middle;
            else if (nums[middle] <= nums[endIndex])
                endIndex = middle;
        }
        return nums[middle];
    }
};

Note:直接二分,感觉还能简洁点哈哈

cpp 复制代码
class Solution {
public:
    int findMin(vector<int>& nums) {
        int n = nums.size() - 1;
        if (n < 0) return -1;
        while (n > 0 && nums[n] == nums[0]) n -- ;
        if (nums[n] >= nums[0]) return nums[0];
        int l = 0, r = n;
        while (l < r) {
            int mid = l + r >> 1; 
            if (nums[mid] < nums[0]) r = mid;
            else l = mid + 1;
        }
        return nums[r];
    }
};

总结

祝大家都能学有所成,找到一份好工作!

相关推荐
强盛小灵通专卖员1 小时前
分类分割详细指标说明
人工智能·深度学习·算法·机器学习
李匠20243 小时前
C++GO语言微服务之图片、短信验证码生成及存储
开发语言·c++·微服务·golang
IT猿手4 小时前
基于强化学习 Q-learning 算法求解城市场景下无人机三维路径规划研究,提供完整MATLAB代码
神经网络·算法·matlab·人机交互·无人机·强化学习·无人机三维路径规划
是孑然呀5 小时前
【小记】word批量生成准考证
笔记·学习·excel
万能程序员-传康Kk8 小时前
旅游推荐数据分析可视化系统算法
算法·数据分析·旅游
PXM的算法星球8 小时前
【并发编程基石】CAS无锁算法详解:原理、实现与应用场景
算法
ll7788118 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法
烨然若神人~8 小时前
算法第十七天|654. 最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
算法
爱coding的橙子8 小时前
每日算法刷题Day2 5.10:leetcode数组1道题3种解法,用时40min
算法·leetcode
我不想当小卡拉米8 小时前
【Linux】操作系统入门:冯诺依曼体系结构
linux·开发语言·网络·c++