12.5—贪心法—Longest Substring Without Repeating Characters
来源:互联网 发布:社交网络剧情 编辑:程序博客网 时间:2024/05/18 03:59
描述
Given a string, find the length of the longest substring without repeating characters. For example, the
longest substring without repeating leers for ”abcabcbb” is ”abc”, which the length is 3. For ”bbbbb”
Given a string, find the length of the longest substring without repeating characters. For example, the
longest substring without repeating leers for ”abcabcbb” is ”abc”, which the length is 3. For ”bbbbb”
the longest substring is ”b”, with the length of 1.注:(LeetCode上面解法的复杂度不是它所说的O(n))
#include<iostream>#include<vector>#include<string>using namespace std;//找最长的没有重复字符的子串pair<int, string> FindLongestSubstrNoRep(const string &str){if (str == "")return make_pair(0, "");//===vector<int> hashtable(256, 0);int len = str.size();int begin = 0;int end = 0;hashtable[str[begin]] = begin+1;int longest = end-begin+1;string longeststr=str.substr(0,1);for (int i = 1; i < len; i++){end = i;if (hashtable[str[end]] == 0||hashtable[str[end]]<begin){hashtable[str[end]] = i + 1;if (end - begin + 1>longest){longest = end - begin + 1;longeststr = str.substr(begin, longest);}}else{begin = hashtable[str[end]];hashtable[str[end]] = end + 1;}}return make_pair(longest, longeststr);}int main(){string str = "abcbak";pair<int, string> res = FindLongestSubstrNoRep(str);cout << res.first << " " << res.second << endl;}
阅读全文
0 0
- 12.5—贪心法—Longest Substring Without Repeating Characters
- LeetCode03—Longest Substring Without Repeating Characters
- LeetCode3—Longest Substring Without Repeating Characters
- 【贪心算法】Longest Substring Without Repeating Characters
- 12.5 Longest Substring without Repeating Characters
- Longest Substring without repeating Characters(12.5)
- LeetCode —— Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- [LeetCode]——Longest Substring Without Repeating Characters
- leetcode 003 —— Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- LeetCode题解——3Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters —— Leetcode
- LeetCode3——Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- #3 LeetCode——Longest Substring Without Repeating Characters
- [leetcode Q3] —— Longest Substring Without Repeating Characters
- 游戏room8 第六关的答案
- 12.3—贪心法—Best Time to Buy and Sell Sto
- 系统开发团队需要什么样的专业人才?
- 12.4—贪心法—Best Time to Buy and Sell Sto II
- Echarts关系图-力引导布局
- 12.5—贪心法—Longest Substring Without Repeating Characters
- 系统开发团队如何招聘到合适的人才?
- 12.6—贪心法—Container With Most Water
- 我所接触的工程师们
- java基础复习-socket
- 提高深度学习的性能的几点小意见
- leetcode(102). Binary Tree Level Order Traversal
- 13.1—动态规划—Triangle
- 大学为什么需要外部协作单位