LeetCode高频算法刷题记录10

1. 旋转图像【中等】

题目链接:https://leetcode.cn/problems/rotate-image/
参考题解:https://leetcode.cn/problems/rotate-image/solution/xuan-zhuan-tu-xiang-by-leetcode-solution-vu3m/

1.1 题目描述

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

示例 1:
在这里插入图片描述

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[7,4,1],[8,5,2],[9,6,3]]

示例 2:
在这里插入图片描述

输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

提示:

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000

1.2 解题思路

1.3 代码实现

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for(int i = 0; i < n / 2; i++) {
            for(int j = 0; j < n; j++) {
                swap(matrix[i][j], matrix[n - i -1][j]);
            }
        }
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < i; j++) {
                swap(matrix[i][j], matrix[j][i]);
            }
        }
    }
};

2. 组合总和【中等】

题目链接:https://leetcode.cn/problems/combination-sum/
参考题解:https://leetcode.cn/problems/combination-sum/solution/zu-he-zong-he-by-leetcode-solution/

2.1 题目描述

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1
输出: []

提示:

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • candidates 的所有元素 互不相同
  • 1 <= target <= 40

2.2 解题思路

2.3 代码实现

class Solution {
public:
    void findCombination(vector<int>& candidates, int target, vector<vector<int>>& ans, vector<int>& combination, int current) {
        if(current == candidates.size())
            return;
        if(target == 0) {
            ans.push_back(combination);
            return;
        }
        findCombination(candidates, target, ans, combination, current + 1);
        if(target - candidates[current] >= 0) {
            combination.push_back(candidates[current]);
            findCombination(candidates, target - candidates[current], ans, combination, current);
            combination.pop_back();
        }
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> ans;
        vector<int> combination;
        findCombination(candidates, target, ans, combination, 0);
        return ans;
    }
};

3. 回文链表【简单】

题目链接:https://leetcode.cn/problems/palindrome-linked-list/
参考题解:https://leetcode.cn/problems/palindrome-linked-list/solution/hui-wen-lian-biao-by-leetcode-solution/

3.1 题目描述

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

示例 1:
在这里插入图片描述

输入:head = [1,2,2,1]
输出:true

示例 2:
在这里插入图片描述

输入:head = [1,2]
输出:false

提示:

  • 链表中节点数目在范围[1, 10^5] 内
  • 0 <= Node.val <= 9

进阶: 你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

3.2 解题思路

3.3 代码实现

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        vector<int> nums;
        while(head) {
            nums.push_back(head->val);
            head = head->next;
        }
        int len = nums.size();
        for(int i = 0, j = len - 1; i < j; i++, j--) {
            if(nums[i] != nums[j])
                return false;
        }
        return true;
    }
};

4. 字符串解码【中等】

题目链接:https://leetcode.cn/problems/decode-string/
参考题解:https://leetcode.cn/problems/decode-string/solution/decode-string-fu-zhu-zhan-fa-di-gui-fa-by-jyd/

4.1 题目描述

给定一个经过编码的字符串,返回它解码后的字符串。

编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。

你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。

此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。

示例 1:

输入:s = “3[a]2[bc]”
输出:“aaabcbc”

示例 2:

输入:s = “3[a2[c]]”
输出:“accaccacc”

示例 3:

输入:s = “2[abc]3[cd]ef”
输出:“abcabccdcdcdef”

示例 4:

输入:s = “abc3[cd]xyz”
输出:“abccdcdcdxyz”

提示:

  • 1 <= s.length <= 30
  • s 由小写英文字母、数字和方括号 ‘[]’ 组成
  • s 保证是一个 有效 的输入。
  • s 中所有整数的取值范围为 [1, 300]

4.2 解题思路

4.3 代码实现

class Solution {
public:
    string decodeString(string s) {
        stack<pair<string, int>> stk;
        string res = "";
        int multi = 0;
        int len = s.length();
        for(int i = 0; i < len; i++) {
            if(isdigit(s[i])) {
                multi = multi * 10 + (s[i] - '0');
            }
            else if(s[i] == '[') {
                stk.push(pair(res, multi));
                res = "";
                multi = 0;
            }
            else if(s[i] == ']') {
                string currentRes = stk.top().first;
                int currentMulti = stk.top().second;
                stk.pop();
                for(int j = 0; j < currentMulti; j++)
                    currentRes += res;
                res = currentRes;
            }
            else {
                res += s[i];
            }
        }
        return res;
    }
};

5. 多数元素【简单】

题目链接:https://leetcode.cn/problems/majority-element/
参考题解:https://leetcode.cn/problems/majority-element/solution/duo-shu-yuan-su-by-leetcode-solution/

5.1 题目描述

给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

示例 1:

输入:nums = [3,2,3]
输出:3

示例 2:

输入:nums = [2,2,1,1,1,2,2]
输出:2

提示:

  • n == nums.length
  • 1 <= n <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9

进阶: 尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。

5.2 解题思路

5.3 代码实现

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int len = nums.size();
        int ans;
        unordered_map<int, int> count;
        for(int i = 0; i < len; i++) {
            count[nums[i]]++;
            if(count[nums[i]] > len / 2) {
                ans = nums[i];
                break;
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Frankenstein@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值