[LeetCode] Longest Substring Without Repeating Characters
来源:互联网 发布:varian软件 编辑:程序博客网 时间:2024/06/03 06:26
题目链接
int lengthOfLongestSubstring(string s) {int last_pos[128] = {-1};for(int i = 0; i < 128; i++){last_pos[i] = -1;}int sub_length = 0, max_length = 0;for(int i = 0; i < s.length(); i++){int index = int(s[i]);if(last_pos[index] == -1){last_pos[index] = i;sub_length++;}else{max_length = sub_length > max_length? sub_length: max_length;sub_length = 0;i = last_pos[index];for(int j = 0; j < 128; j++){last_pos[j] = -1;}}}max_length = sub_length > max_length? sub_length: max_length;return max_length;}
第一次Wrong Answer是因为直接从重复的字符开始找新的子串,应该是从该重复字符上一次出现的位置后一位开始找;
第二次Wrong Answer是误将int last_pos[128] = {-1};当做对数组所有元素初始化了,应该写个循环来赋值。
程序Run Time是264 ms。。有待改进啊。
0 0
- 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 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [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] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- base64与UIImage转换
- 并查集&最小生成树、最短路
- IIS 服务器 支持.apk文件的下载
- asp.net购物车,订单以及模拟支付宝支付(三)---提交订单
- Maven2配置详解
- [LeetCode] Longest Substring Without Repeating Characters
- MATLAB GUI axes的ButtondownFCN无效的问题
- jQuery片段 - 表单action的更改和提交
- Swift编程语言学习1.3——类型安全和类型推测
- Codeforces 444C DZY Loves Colors 水线段树
- ECShop网站 用360浏览器无法添加产品进购物车
- MFC调试技巧之-Trace
- 谈一谈Cocos2d-x中的某些“大小”
- Ubuntu下彻底卸载重装mysql