Longest Substring Without Repeating Characters
来源:互联网 发布:linux echo e 编辑:程序博客网 时间:2024/04/28 14:49
解题思路:两个指针,开始都指向字符串起始位置, 然后指针2向后遍历直至从pos1到pos2的子串出现重复字符,这时pos1开始自增,直至从pos1到pos2没有重复字符。
也就是遍历2遍,复杂度O(n)。
class Solution {public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int ans = 0, current_len = 0; int pos1 = 0, pos2 = 0; int arr[300]; memset(arr, 0, sizeof(arr)); while(pos2 < s.size()) { if(arr[s[pos2]] == 0) { current_len ++ ; arr[s[pos2]] = 1; } else { if(current_len > ans) ans = current_len; while(s[pos1] != s[pos2]) { arr[s[pos1]] = 0; pos1 ++ ; current_len -- ; } pos1 ++ ; } pos2 ++ ; } if(current_len > ans) ans = current_len; return ans; }};
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- android 使用ViewFlow实现图片带焦点左右滑动(定时滑动)
- 3.写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。
- TopCoder 250 points 28-SRM 157 DIV 2 185.60/250 74.24%
- OpenCV利用鼠标进行矩形ROI选取
- 瞎写点
- Longest Substring Without Repeating Characters
- DOS相关指令
- luan78zao
- 【时间规划】个性化简历
- 百度首页快照不更新,内页收录正常是什么原因
- malloc()/calloc()、realloc函数
- Android开发,实现摇一摇功能
- php读文件和写文件
- AndEngine学习笔记--1