1、动态规划 最长不重复连续子串分类: ACM2011-11-10 22:39 198人阅读 评论(0) 收藏 举报题意Longest Substring Without Repeating CharactersGiven a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb“ is “abc“, which the length is 3.
2、For “bbbbb“ the longest substring is “b“, with the length of 1.这个题是在 LeetCode 上面看到的。想到是用动态规划来做。但是具体如何推导没有想出来。后来看了一下别人的思路,整理一下。写个总结。 ,题意很简单,就是给定一个字符串,找到其中出现了的最长不重复子串。比如 abcc 就是 abcabcdcdefg 就是 cdefg。动态规划我的动态规划一直学得比较挫。这里也是借他山之石。cpp view plaincopy int lengthOfLongestSubstring(string s) const int len = s.length(); int *dp = new intlen+2; int last256; for (int i = 0; i = 0; -i) dpi = min(dpi+1, lastsi - 1); lastsi = i; int ans = -1; for (int i = 0; i ret ? ans : ret; delete dp; return ans + 1;