**一. 題目描述**
Given 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\. For “bbbbb” the longest substring is “b”, with the length of 1.
**二. 題目分析**
題目的大意是,給出一串字符串,找出無重復字符的最長子串,輸出其長度。可以使用兩個指針,一個指向當前子串的頭,一個指向尾,尾指針不斷往后掃描,當有字符前面出現過,記錄當前子串長度和最優解的比較結果。然后頭指針不斷往后掃描,直到掃描到一個字符和尾指針相同,則尾指針繼續掃描,當尾指針到達字符串結尾時算法結束。算法復雜度O(n) + O(n) = O(n)。
**三. 示例代碼**
~~~
class Solution {
private:
bool canUse[256];
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
memset(canUse, true, sizeof(canUse));
int count = 0;
int start = 0;
int ret = 0;
for(int i = 0; i < s.size(); i++)
{
if (canUse[s[i]])
{
canUse[s[i]] = false;
count++;
}
else
{
ret = max(ret, count);
while(true)
{
canUse[s[start]] = true;
count--;
if (s[start] == s[i])
break;
start++;
}
start++;
canUse[s[i]] = false;
count++;
}
}
ret = max(ret, count);
return ret;
}
};
~~~
**四. 小結**
參考:[http://www.cnblogs.com/remlostime/archive/2012/11/12/2766530.html](http://www.cnblogs.com/remlostime/archive/2012/11/12/2766530.html)
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching