1. Longest Substring Without Repeating Characters
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.
1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 vector<bool> exist(256, false); 5 int res = 0; 6 int start = 0, end = 0; 7 int N = s.size(); 8 while(end < N) { 9 if(!exist[s[end]]) { 10 exist[s[end++]] = true; 11 } 12 else { 13 exist[s[start++]] = false; 14 } 15 res = max(end-start, res); // already +1 in var: end 16 } 17 return res; 18 } 19 };
2. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4 string res; 5 if(strs.empty()) 6 return res; 7 for(int i = 0; i < strs[0].size(); i++) { 8 char ch = strs[0][i]; 9 for(int j = 1; j < strs.size(); j++) { 10 if(strs[j][i] != ch || i == strs[j].size()) { 11 return res; 12 } 13 } 14 res.push_back(ch); 15 } 16 } 17 };
Leetcode个人总结10 字符串题(2),布布扣,bubuko.com
原文:http://www.cnblogs.com/zhengjiankang/p/3629838.html