日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

一道vector实现字典的题目 C++

發(fā)布時(shí)間:2025/6/17 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一道vector实现字典的题目 C++ 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用vector 在解決一些問題的時(shí)候確實(shí)非常高效。
可以不像Array 那樣一個(gè)一個(gè)去查。
可以大幅度縮減代碼實(shí)現(xiàn)的時(shí)間。
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 = "qrebtmppwuuhapcegnaon";char t_string[101]="";int max_ceil = 0;int ceil = 0;for (int i=0; i<s.length()-max_ceil;i++){for(int j=0; j<s.length()-i; j++){for(int k=0; k < ceil;k++){if(s[i+j] == t_string[k]){if(ceil>max_ceil){max_ceil = ceil;}ceil=0;j=0;i++;break;}}t_string[ceil]=s[i+j];ceil++;}}if(ceil>max_ceil){max_ceil = ceil;}cout<< max_ceil; return 0; }

在上面看到一個(gè)大神的解法,拿來分享。

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)//該重復(fù)字符有效...start = dict[s[i]]; //替換到最近出現(xiàn)的重復(fù)字符位置dict[s[i]] = i; //最近出現(xiàn)的位置maxLen = max(maxLen, i - start);}cout<<maxLen; }

注釋是我自己加的,理解這個(gè)算法確實(shí)需要一點(diǎn)抽象思維。

轉(zhuǎn)載于:https://www.cnblogs.com/venusian/p/10480872.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的一道vector实现字典的题目 C++的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。