LeetCode-3-Longest Substring Without Repeating Characters(穷举)-Medium
来源:互联网 发布:全国网络诈骗报警电话 编辑:程序博客网 时间:2024/05/12 23:28
题意理解
解决最长字串长度;
解题思路:
1. 递归(好像会栈溢出);
2. 记录比较遍历中的最大值;
解题代码:
class Solution {public: int lengthOfLongestSubstring(string s) { if(s.length()==0){ return 0; } string subStr=""; int maxLen=0; int maxSubLen=0; int initSubPos=0; for(int i=0;i<s.length();i++){ string::size_type idx=subStr.find(s[i]); if(idx==string::npos){ subStr+=s[i]; maxSubLen++; }else{ if(maxSubLen>maxLen){ maxLen=maxSubLen; } maxSubLen=0; subStr=""; cout<<"idx= "<<idx<<endl; i=idx+initSubPos; initSubPos=i+1; } } return maxLen>maxSubLen?maxLen:maxSubLen; }};
递归做法(会出现栈溢出)
#include<string>using namespace std;class Solution {public: int lengthOfLongestSubstring(string s) { if(s.length()==0){ return 0; } string subStr=""; int maxLen=0; for(int i=0;i<s.length();i++){ string::size_type idx=subStr.find(s[i]); if(idx==string::npos){ subStr+=s[i]; maxLen++; }else{ int t=lengthOfLongestSubstring(s.substr(idx+1)); return maxLen>t?maxLen:t; } } return maxLen; }};
0 0
- LeetCode-3-Longest Substring Without Repeating Characters(穷举)-Medium
- 【LeetCode】(3)Longest Substring Without Repeating Characters(Medium)
- <LeetCode><Medium> 3 Longest Substring Without Repeating Characters
- week3-leetcode #3-Longest Substring Without Repeating Characters[Medium]
- Longest Substring Without Repeating Characters(medium)
- Leetcode 3. Longest Substring Without Repeating Characters (Medium) (cpp)
- 【leetcode】3. Longest Substring Without Repeating Characters 【medium】
- LeetCode第三关:Longest Substring Without Repeating Characters --> Difficulty: Medium
- LeetCode 3. Longest Substring Without Repeating Characters [Medium]
- Leetcode解题笔记 3.Longest Substring Without Repeating Characters [Medium]
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- openssl对rsa密钥的管理用法
- Android GridView中的item实现长宽一样
- Yocto tips (4): Yocto 如何确定(找到)一个包的名字
- Ubuntu Apache2 进入HTTPS
- 基于jQuery的TreeGrid组件
- LeetCode-3-Longest Substring Without Repeating Characters(穷举)-Medium
- Java String数组对象的创建测试
- 开门大吉
- VIJOS1592不听话的机器人
- C#_FileInfo文件属性类和DirectoryInfo文件夹属性类
- 改变UITableView的headerView、footerView背景颜色
- 结合volley实现图片三级缓存
- mysql非常规的中文乱码问题
- RESTful 初识