首页 > 其他 > 详细

Leetcode个人总结10 字符串题(2)

时间:2014-03-28 20:08:30      阅读:385      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣
 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 };
bubuko.com,布布扣

2. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

bubuko.com,布布扣
 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 };
bubuko.com,布布扣

Leetcode个人总结10 字符串题(2),布布扣,bubuko.com

Leetcode个人总结10 字符串题(2)

原文:http://www.cnblogs.com/zhengjiankang/p/3629838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!