• 365天挑战LeetCode1000题——Day 088 最大交换 跳跃游戏 III 到家的最少跳跃次数 水壶问题


    670. 最大交换

    在这里插入图片描述

    代码实现

    class Solution {
    public:
        int maximumSwap(int num) {
            if (num < 10) return num;
            int tmp = num;
            vector<int> arr;
            while (tmp) {
                arr.insert(arr.begin(), tmp % 10);
                tmp /= 10;
            }
            vector<int> tmp1 = arr;
            sort(tmp1.begin(), tmp1.end(), greater<int>());
            int i = 0;
            for (; i < arr.size(); i++) {
                if (tmp1[i] != arr[i]) break;
            }
            if (i == arr.size()) return num;
            int j = i;
            int sub = j, maxNum = 0;
            // cout << i;
            for (; i < arr.size(); i++) {
                if (arr[i] >= maxNum) {
                    maxNum = arr[i];
                    sub = i;
                }
            }
            // cout << endl;
            // cout << sub;
            swap(arr[j], arr[sub]);
            int ans = 0;
            for (int i = 0; i < arr.size(); i++) {
                ans = ans * 10 + arr[i];
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    1306. 跳跃游戏 III

    在这里插入图片描述

    代码实现

    class Solution {
    public:
        bool canReach(vector<int>& arr, int start) {
            int n = arr.size();
            vector<bool> visited(n, false);
            queue<int> myQueue;
            myQueue.push(start);
            int tmp;
            while (!myQueue.empty()) {
                tmp = myQueue.front();
                myQueue.pop();
                if (visited[tmp]) continue;
                visited[tmp] = true;
                if (!arr[tmp]) return true;
                if (tmp + arr[tmp] < n) myQueue.push(tmp + arr[tmp]);
                if (tmp - arr[tmp] >= 0) myQueue.push(tmp - arr[tmp]);
            }
            return false;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    1654. 到家的最少跳跃次数

    在这里插入图片描述

    代码实现

    class Solution {
    public:
        int minimumJumps(vector<int>& forbidden, int a, int b, int x) {
            queue<pair<int, int>> myQueue;
            set<int> f;
            for (auto & b : forbidden) {
                f.emplace(b);
            }
            set<pair<int, int>> visited;
            myQueue.push({x, 0});
            int t = 0;
            while (!myQueue.empty()) {
                int sz = myQueue.size();
                while (sz--) {
                    auto [loc, time] = myQueue.front();
                    // cout << loc << " " << time << endl;
                    myQueue.pop();
                    if (loc == 0) return t;
                    if (f.count(loc) || visited.count(make_pair(loc, time))) continue;
                    visited.emplace(make_pair(loc, time));
                    if (loc < 0) continue;
                    if (loc > 6000) continue;
                    myQueue.push({loc - a, 0});
                    if (!time) myQueue.push({loc + b, 1});
                }
                t++;
            }
            return -1;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    365. 水壶问题

    在这里插入图片描述

    代码实现

    class Solution {
    public:
        bool canMeasureWater(int x, int y, int z) {
            if (x + y < z) return false;
            if (x == 0 || y == 0) return z == 0 || (x + y) == z;
            return z % gcd(x, y) == 0;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    java面试小经历
    【LTTng】核心概念精读
    前端AJAX入门到实战,学习前端框架前必会的(ajax+node.js+webpack+git)(一)
    数智化升级必答题,尚美数智如何成为酒店行业标准答案?
    androd 如何使得升级更加容易 以及规范app文件使用
    web:[极客大挑战 2019]Upload
    解决 React forwardRef 与 TypeScript 泛型组件冲突的问题
    JS语言里常见的随机函数示例,实验结果分布规律分析
    CDH大数据平台 /bin/sh: mysql_config: command not found
    解决一次Cannot read properties of null (reading ‘length‘)的问题:
  • 原文地址:https://blog.csdn.net/ShowMeTheCod3/article/details/126827532