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

总结

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

相关推荐
qq_4298796722 分钟前
省略号和可变参数模板
开发语言·c++·算法
moxiaoran575328 分钟前
uni-app学习笔记二十三--交互反馈showToast用法
笔记·学习·uni-app
CodeWithMe1 小时前
【C/C++】std::vector成员函数清单
开发语言·c++
uyeonashi1 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
飞川撸码1 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
Neil今天也要学习1 小时前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法
yzx9910132 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
亮亮爱刷题2 小时前
算法练习-回溯
算法
眼镜哥(with glasses)3 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
zh_xuan6 小时前
c++ 单例模式
开发语言·c++·单例模式