week3-leetcode #3-Longest Substring Without Repeating Characters[Medium]
来源:互联网 发布:python高维数据可视化 编辑:程序博客网 时间:2024/05/22 01:47
week3-leetcode #3-Longest Substring Without Repeating Characters[Medium]
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/
Question
Given a string, find the length of the longest substring without repeating characters.
Example
Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with the length of 1.Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
Solution
time complecity:
space complecity:
runtime:22ms
class Solution {public: int lengthOfLongestSubstring(string s) { // 创建一个hashmap用于标记字符的位置 vector<int> hashmap(128, -1); int max_length = 0; // 用于标记最大的长度 int str_length = s.length(); // 用于确定开头 int start = 0; // 用于确定结尾 int end = 1; for (int i = 0; i < str_length; i++) { if (hashmap[s[i]] == -1) { hashmap[s[i]] = i; } else { int origin_start = start; start = max(start+1, hashmap[s[i]]+1); for (int j = origin_start; j < start; j++) { if (s[j] != s[i]) hashmap[s[j]] = -1; } hashmap[s[i]] = i; } max_length = max(max_length, end-start); end++; } return max_length; }};
思路:本题有很多种做法,其中一种是找出所有的substring,然后注意比较他们的大小,但其实这样的效率是很低的,需要
使用一个vector来标记窗口里面的元素;
给定一个start和end,每次计算end-start的值;
对于start值的确定,每次遇到与集合内相同的元素的时候改变,更改后的值应该是与集合中重复的那个元素的后一个值;
对于end值的确定,每次都会加一来扩大窗口的大小。
阅读全文
0 0
- week3-leetcode #3-Longest Substring Without Repeating Characters[Medium]
- 【LeetCode】(3)Longest Substring Without Repeating Characters(Medium)
- <LeetCode><Medium> 3 Longest Substring Without Repeating Characters
- LeetCode-3-Longest Substring Without Repeating Characters(穷举)-Medium
- Leetcode 3. Longest Substring Without Repeating Characters (Medium) (cpp)
- 【leetcode】3. Longest Substring Without Repeating Characters 【medium】
- LeetCode第三关:Longest Substring Without Repeating Characters --> Difficulty: Medium
- LeetCode 3. Longest Substring Without Repeating Characters [Medium]
- Leetcode解题笔记 3.Longest Substring Without Repeating Characters [Medium]
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- 两个链表的第一个公共结点
- linux centos7下简单安装 7-zip
- Merkle Tree
- MQL5语法基础(四)
- C# MySqlHelp类 "DbModel.MySql"数据库操作类
- week3-leetcode #3-Longest Substring Without Repeating Characters[Medium]
- php验证码
- cf 862C Mahmoud and Ehab and the xor 思维+构造
- 贝塞尔曲线扫盲
- CentOS7 64位下MySQL5.7安装与配置(YUM)
- java开发规范:效率与避免问题
- 如何深入和高效的学习编程语言
- LeetCode编程练习
- 遗传算法基本原理及在互联网中的应用