408算法题leetcode--第11天

news/2024/9/22 22:59:27 标签: 算法, leetcode, 考研

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

  • 3. 无重复字符的最长子串
  • 思路:滑动窗口
  • 时间:O(n);空间:O(字符种类数)
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. 旋转图像

  • 48. 旋转图像
  • 思路:观察法
  • 时间:O( n 2 n^2 n2);空间:O(1)
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. 螺旋矩阵

  • 54. 螺旋矩阵
  • 思路:模拟
  • 时间:O( n 2 n^2 n2);空间:O(1)
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)
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. 逆波兰表达式求值

  • 150. 逆波兰表达式求值
  • 思路:栈
  • 时间:时间:O(n);空间:O(n)
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();
    }
};

http://www.niftyadmin.cn/n/5670942.html

相关文章

sqli-lab靶场学习(三)——Less8-10(盲注、时间盲注)

Less8 第八关依然是先看一般状态 http://localhost/sqli-labs/Less-8/?id1 然后用单引号闭合&#xff1a; http://localhost/sqli-labs/Less-8/?id1 这关的问题在于报错是不显示&#xff0c;那没办法通过上篇文章的updatexml大法处理。对于这种情况&#xff0c;需要用“盲…

Trace纳米侦查无人机技术详解

纳米无人机&#xff0c;作为微型无人机的一种&#xff0c;通常指尺寸和重量都非常小的无人机&#xff0c;其重量一般不超过几百克&#xff0c;甚至更小。这类无人机由于体积小、重量轻&#xff0c;具备高度的隐蔽性和灵活性&#xff0c;在军事侦察、环境监测、搜救行动等领域具…

Poetry超好用python依赖、包管理工具

介绍 Poetry 用一个pyproject.toml 代替 setup.py, requirements.txt, setup.cfg, MANIFEST.in and Pipfile文件。 用过vue的肯定看上去会无比熟悉&#xff0c;类似于package.json文件。 [tool.poetry] name "my-package" version "0.1.0" description…

淘宝npm镜像源更新后,如何正常使用npm命令

文章目录 一. npm命令报错二. 更换淘宝最新npm镜像源三. npm命令使用 一. npm命令报错 使用npm install *****命令 报错 npm error code CERT_HAS_EXPIRED npm error errno CERT_HAS_EXPIRED npm error request to https://registry.npm.taobao.org/express failed, reason: …

Vue的指令v-model的原理

v-model的原理 原理&#xff1a;v-model本质上是一个语法糖。例如应用在输入框上&#xff0c;就是value属性和input事件的合写。 作用&#xff1a;提供数据的双向绑定 数据变&#xff0c;视图跟着变 :value视图变&#xff0c;数据跟这变 input 注意&#xff1a;$event用于在…

二分查找算法(3) _x的平方根

个人主页&#xff1a;C忠实粉丝 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 C忠实粉丝 原创 二分查找算法(3) _x的平方根 收录于专栏【经典算法练习】 本专栏旨在分享学习算法的一点学习笔记&#xff0c;欢迎大家在评论区交流讨论&#x1f48c; 目录 温馨…

【玉米田】

题目 代码 #include <bits/stdc.h> using namespace std; typedef long long LL;const int mod 1e8; const int M 1 << 12; LL f[13][M]; int g[13]; vector<int> state; vector<int> p[M]; int n, m; bool check(int x) {return !(x & x <&…

深度学习02-pytorch-05-张量的索引操作

在 PyTorch 中&#xff0c;张量的索引操作是非常灵活且强大的&#xff0c;允许你访问和修改张量中的特定元素或子集。与 NumPy 的数组操作类似&#xff0c;PyTorch 的张量支持多种索引方法&#xff0c;包括单纯的基础索引、高级索引和切片等。下面详细介绍 PyTorch 中常见的张量…