一道vector实现字典的题目 C++
使用vector 在解決一些問題的時候確實非常高效。
可以不像Array 那樣一個一個去查。
可以大幅度縮減代碼實現的時間。
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
https://leetcode.com/problems/longest-substring-without-repeating-characters/submissions/
Leetcode 里面的第三題我的解法,非常冗長。
在上面看到一個大神的解法,拿來分享。
int main(){string s="uvuioinmk";int maxLen = 0;vector<int> dict(256, -1);int start = -1;for(int i=0; i<s.length();i++){if(dict[s[i]]>start)//該重復字符有效...start = dict[s[i]]; //替換到最近出現的重復字符位置dict[s[i]] = i; //最近出現的位置maxLen = max(maxLen, i - start);}cout<<maxLen; }注釋是我自己加的,理解這個算法確實需要一點抽象思維。
轉載于:https://www.cnblogs.com/venusian/p/10480872.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的一道vector实现字典的题目 C++的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 斯坦福DAWNBench:华为云Mode
- 下一篇: Zulip 2.0.1 发布,功能强大的