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];
    }
};

总结

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

相关推荐
古月-一个C++方向的小白5 小时前
C++11之lambda表达式与包装器
开发语言·c++
tanyongxi667 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
Wendy14417 小时前
【线性回归(最小二乘法MSE)】——机器学习
算法·机器学习·线性回归
拾光拾趣录7 小时前
括号生成算法
前端·算法
渣呵8 小时前
求不重叠区间总和最大值
算法
拾光拾趣录8 小时前
链表合并:双指针与递归
前端·javascript·算法
好易学·数据结构8 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
斯是 陋室8 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
tju新生代魔迷9 小时前
C++:list
开发语言·c++
iFulling9 小时前
【计算机网络】第四章:网络层(上)
学习·计算机网络