LeetCode #3 简单题

题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

题解: i j 分别记录目标字符串的左右边界。对当前字符 x,如果前面出现过,则更新左边界为上次出现位置的下一个,然后更新当前字符 x 的位置,遍历过程中记录一下 j - i + 1的最大值就好了。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int ans = 0;
        int len = s.size();
        std::map<int, int> pos;
        for (int i = 0, j = 0; j < len; ++j){
            if (pos.find(s[j]) != pos.end()){
                int left = pos[s[j]];
                i = left > i ? left : i;
            }
            ans = (j - i + 1) > ans ? (j - i + 1) : ans;
            pos[s[j]] = j + 1;
        }
        return ans;
    }
};

原文地址:https://www.cnblogs.com/error408/p/11546510.html

时间: 2024-07-30 09:17:19

LeetCode #3 简单题的相关文章

LeetCode #1 简单题

题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 题解:简单题,没啥可说的,不想 n2 复杂度搞,map存一下logn吧先 1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) { 4 std::map<int, int> numMap; 5 vector<int>

每日一题之LeetCode 栈简单题集合496,682,232,225,155,844,20

496 下一个最大的元素方法1 :自己瞎写的 没有考虑到栈的特性class Solution:def nextGreaterElement(self, nums1, nums2): L=[] for i in nums1: k=nums2.index(i) lenth2=len(nums2) if k==lenth2-1: L.append(-1) for j in range(k+1,lenth2): if nums2[j]>i: L.append(nums2[j]) break if j==

LeetCode #6 简单题(小模拟)

题目: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.然后横向输出.LeetCode上有几个样例可以看看. 题解:模拟一下就好了- -,对原字符串s排列完后,横向每个添加到ans中去就行了 class Solution { public: string convert(string s, int numRows) { if (numRows == 1) return s; int len = (int)s.size(), gridSize = numRows * 2 -

leetCode #2 简单题

题目:给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储一位数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和.您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 题解:就指向两个链表的头的指针依次向后遍历.算和的余数,记录一下进位.如果L1先遍历到末尾的话,就把L1最后一个指针指向L2当前的next,即把L2剩余的部分接在L1的结尾.这样可以节省不少开辟新Node的空间,然后继续用进位和L1当前N

LeetCode #5 简单题

题目:求最长回文子串 题解:manacher算法,百度一堆讲解- -,我也不说了,想知道啥的自己百度去吧 class Solution { public: string longestPalindrome(string s) { string manaStr = "$#"; for (int i=0;i<s.size();i++){ manaStr += s[i]; manaStr += '#'; } vector<int> rd(manaStr.size(), 0)

LeetCode #7 简单题(反转整数)

题目:翻转整数  123 -> 321,-123 -> -321 题解: long long 存一下好了,注意溢出返回0就行了 class Solution { public: int reverse(int x) { long long orix = x; long long rev = 0; bool isLess0 = orix < 0; orix = orix < 0 ? -1 * orix : orix; while(orix != 0){ rev = rev * 10

Leetcode动态规划【简单题】

目录 Leetcode动态规划[简单题] 53. 最大子序和 题目描述 思路分析 复杂度分析 70.爬楼梯 题目描述 思路分析 复杂度分析 121.买卖股票的最佳时机 题目描述 思路分析 复杂度分析 303.区域和检索-数组不可变 题目描述 思路分析 复杂度分析 Leetcode动态规划[简单题] 动态规划(Dynamic programming,简称DP),是一种把原问题分解为相对简单的子问题的方式求解复杂问题的方法.动态规划相较于递归,拥有更少的计算量. 53. 最大子序和 题目描述 给定一

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a