本文主要是介绍[leetcode刷题系列]Longest Substring Without Repeating Characters,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简单的dp题
const static int MAXN = 1 << 20;
int dp[MAXN];
class Solution {
public:int lengthOfLongestSubstring(string s) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(s.size() <= 0)return 0;int ans = 1;map<char, int> hash;dp[0] = 1;hash[s[0]] = 0;for(int i = 1; i < s.size(); ++i){if(hash.find(s[i]) == hash.end())dp[i] = dp[i - 1] + 1;elsedp[i] = min(i - hash[s[i]], dp[i - 1] + 1);ans = max(ans, dp[i]);hash[s[i]] = i;}return ans;}
};
这篇关于[leetcode刷题系列]Longest Substring Without Repeating Characters的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!