408算法题leetcode--第11天

3. 无重复字符的最长子串

cpp 复制代码
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // 滑动窗口:如果没有出现相同的字符,那么右指针一直向右
        int ret = 0, size = s.size();
        unordered_map<char, int>mp;
        for(int i = 0, j = 0; j < size; j++){
            if(mp.find(s[j]) != mp.end()){
                while(mp.find(s[j]) != mp.end()){
                    mp.erase(s[i]);
                    i++;
                }
            }
            mp[s[j]] = 1;
            ret = max(ret, j - i + 1);
        }
        return ret;
    }
};

48. 旋转图像

cpp 复制代码
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]);
            }
        }
    }
};

54. 螺旋矩阵

cpp 复制代码
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int>ret;
        int left = 0, right = matrix[0].size() - 1, top = 0, bottom = matrix.size() - 1;
        while(left <= right && top <= bottom){
            for(int i = left; i <= right; i++){
                ret.push_back(matrix[top][i]);
            }
            if(++top > bottom){
                break;
            }
            for(int i = top; i <= bottom; i++){
                ret.push_back(matrix[i][right]);
            }
            if(--right < left){
                break;
            }
            for(int i = right; i >= left; i--){
                ret.push_back(matrix[bottom][i]);
            }
            if(--bottom < top){
                break;
            }
            for(int i = bottom; i >= top; i--){
                ret.push_back(matrix[i][left]);
            }
            if(++left > right){
                break;
            }
        }
        return ret;
    }
};

20. 有效的括号

  • 20. 有效的括号
  • 思路:左括号入栈,遇到对应的右括号出栈
  • 时间:O(n);空间:O(n)
cpp 复制代码
class Solution {
public:
    bool isValid(string s) {
        stack<int>stk;
        for(auto c : s){
            if(c == '(' || c == '{' || c == '['){
                stk.push(c);
            } else if(c == ')' && stk.size() && stk.top() == '('){
                stk.pop();
            } else if(c == ']' && stk.size() && stk.top() == '['){
                stk.pop();
            }else if(c == '}' && stk.size() && stk.top() == '{'){
                stk.pop();
            } else {
                return false;
            }
        }
        if(stk.size()){
            return false;
        }
        return true;
    }
};

150. 逆波兰表达式求值

cpp 复制代码
class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        // 遇到数字就入栈
        // 遇到符号就弹出两个数计算,然后将数重新入栈
        stack<long long>stk;
        for(auto c : tokens){
            if(c == "+" || c == "*" || c == "-" || c == "/"){
                auto t1 = stk.top();
                stk.pop();
                auto t2 = stk.top();
                stk.pop();
                long long temp = 0;
                if(c == "+") temp = t1 + t2;
                else if(c == "-") temp = t2 - t1;
                else if(c == "*") temp = t1 * t2;
                else temp = t2 / t1;
                stk.push(temp);
            } else {
                stk.push(stoll(c));
            }
        }
        return stk.top();
    }
};
相关推荐
草莓熊Lotso23 分钟前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
KyollBM29 分钟前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
CV点灯大师44 分钟前
C++算法训练营 Day10 栈与队列(1)
c++·redis·算法
GGBondlctrl1 小时前
【leetcode】递归,回溯思想 + 巧妙解法-解决“N皇后”,以及“解数独”题目
算法·leetcode·n皇后·有效的数独·解数独·映射思想·数学思想
武子康1 小时前
大数据-276 Spark MLib - 基础介绍 机器学习算法 Bagging和Boosting区别 GBDT梯度提升树
大数据·人工智能·算法·机器学习·语言模型·spark-ml·boosting
武子康1 小时前
大数据-277 Spark MLib - 基础介绍 机器学习算法 Gradient Boosting GBDT算法原理 高效实现
大数据·人工智能·算法·机器学习·ai·spark-ml·boosting
Andrew_Xzw2 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
超的小宝贝3 小时前
数据结构算法(C语言)
c语言·数据结构·算法
木子.李3479 小时前
排序算法总结(C++)
c++·算法·排序算法