【LeetCode】String

[227] Basic Calculator II [Medium]

实现一个简单的计算器,可以+,-,*,/。

用一个数组存数, 遇到+, - 就放进数组 ; 遇到 *, / 就先计算好,再放进数组。

用char op存当前数之前的运算符。

 1 //author: Wanying Zhang
 2 class Solution {
 3 public:
 4     int calculate(string s) {
 5         int digit = 0;
 6         int ans = 0;
 7         vector<int> tmp;
 8         char op = '+'; //op marks pre operator to determine how to deal current digit.
 9         for (int i = 0; i < s.size(); ++i) {
10             //if (s[i] == ' ') continue;  // what if the last character is ' '
11             if (isdigit(s[i])) {
12                 digit = digit * 10 + (s[i] - '0');
13             }
14             if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || i == s.size()-1) {
15                 if (op == '+') {
16                     tmp.push_back(digit);
17                 }
18                 if (op == '-') {
19                     tmp.push_back((-1) * digit);
20                 }
21                 if (op == '*' || op == '/') {
22                     tmp.back() = op == '*' ? tmp.back() * digit : (tmp.back() / digit);
23                 }
24                 op = s[i];
25                 digit = 0;
26             }
27         }
28         for (int i = 0; i < tmp.size(); ++i) {
29             ans += tmp[i];    
30         }
31         return ans;
32     }
33 };
View Code

 

[434] Number of Segments in a String [Easy]

给个字符串,返回里面有几段。(空格分割)

Example:

Input: "Hello, my name is John"
Output: 5
 1 class Solution {
 2 public:
 3     int countSegments(string s) {
 4         int ans = 0;
 5         istringstream iss(s);
 6         string seg;
 7         while (iss >> seg) {
 8             ans++;
 9         }
10         return ans;
11     }
12 };
View Code

字符串前面加空格。

 1 class Solution {
 2 public:
 3     int countSegments(string s) {
 4         s = " " + s;
 5         int ans = 0;
 6         for (int i = 1; i < s.size(); ++i) {
 7             if (s[i-1] == ' ' && s[i] != ' ') {
 8                 ans++;
 9             }
10         }
11         return ans;
12      }
13 };
View Code

 

[91] Decode Ways  [Medium]

给个字符串,比如12121这种,问如果用A-Z 26个字母表翻译的话,能有几种翻译方式。

思路就是参考走台阶那个题,一次走一步和一次走两步,有几种走法。注意bug-free,以及要想评价高,就用滚动数组

 1 class Solution {
 2 public:
 3     int numDecodings(string s) {
 4         // Write your code here
 5         // 有很多违规的例子,要注意返回0,比如说空串,第一个字母为0,或者连续两个0,或者存在 30 ,40...这种东西
 6         if (s.size() == 0 || s[0] == '0') {
 7             return 0;
 8         }
 9         int a0 = 1, a1 = 1, a2 = 1, ans = 1;
10         for (int i = 1; i < s.size(); ++i) {
11             if (s[i] != '0') {
12                 if (s[i-1] == '1' || s[i-1] == '2' &&  s[i] >= '1' && s[i] <= '6') {
13                     ans = a1 + a2;
14                 } else {
15                     ans = a2;
16                 }
17             } else {
18                 if (s[i-1] != '1' && s[i-1] != '2') {
19                     return 0;
20                 } else {
21                     ans = a1;
22                 }
23             }
24             a0 = a1;
25             a1 = a2;
26             a2 = ans;
27         }
28         return ans;
29     }
30 };
View Code

 

[151] Reverse Words in a String [Medium]

Given s = "the sky is blue",
return "blue is sky the".

思路是先反转一遍字符串,然后再每个单词反转一遍。

这个题目对空格输出有要求,不能有连续的空格,串首和串尾都没有空格。

空格搞的非常恶心。去除空格的套路就是流式处理法。

1 string s = "asssss kd ds";
2 string tmp = "";
3 stringstream ss(s);
4 while (ss >> tmp) {
5     ....        
6 }
流式处理空格 
 1 class Solution {
 2 public:
 3     void reverseWords(string &s) {
 4         if (s.empty()) { return; }
 5         reverse(s, 0, s.size()-1);
 6         int start = 0, end = -1;
 7         for (int i = 0; i < s.size(); ++i) {
 8             if (s[i] != ' ') {
 9                 if (i == 0 || i-1 >= 0 && s[i-1] == ' ') { start = i; }
10                 if (i == s.size()-1 || i+1 < s.size() && s[i+1] == ' ') { end = i; }
11                 if (start < end) {
12                     reverse(s, start, end);
13                     start = end + 1;
14                 }
15             } else {
16                 if (i == 0 || i == s.size()-1 || s[i-1] == ' ') {
17                     s.erase(i--, 1);
18                 }
19             }
20         }
21         if (s.back() == ' ') s.pop_back();
22     }
23     void reverse(string &s, int start, int end) {
24         if(s == "") return;
25         while (start < end) {
26             swap(s[start++], s[end--]);
27         }
28     }
29     
30     
31 };
View Code

 

[214] Shortest Palindrome [Hard]

给个字符串,问前面最少补几字符能使整个字符串为回文串。

暴力法超时了:找出从第一个字符开始的最长回文串,然后主串之后的那一堆就是要翻转加到字符串前面的东西。

Manacher方法:利用P数组找出一个位置j,使得s[0..j]是最长的回文串。然后翻转后面的。注意下manacher的写法。这个是一个非常好实现的算法。

 1 class Solution {
 2 public:
 3     string shortestPalindrome(string s) {
 4         if (s.size() == 0) return s;
 5         string ori_str = s;
 6         s = preProcess(s);
 7         vector<int> p(s.size(), 0);
 8         int mx =0, dx = 0;
 9         for (auto i = 1; i < s.size(); ++i) {
10             if (mx > i) {
11                 p[i] = min(p[2*dx-i], mx-i);
12             } else {
13                 p[i] = 1;
14             }
15             while (s[i+p[i]] == s[i-p[i]]) {
16                 p[i]++;
17             }
18             if (i + p[i] > mx) {
19                 mx = i + p[i];
20                 dx = i;
21             }
22         }
23         int max_len = 0;
24         for (auto i = 1; i < p.size(); ++i) {
25             if (i == p[i] && i > max_len) {
26                 max_len = i;
27             }
28         }
29         max_len--;  // P[i]-1正好是原字符串中回文串的总长度
30         string ss = ori_str.substr(0, max_len);
31         string need_reverse = ori_str.substr(max_len);
32         reverse(need_reverse.begin(), need_reverse.end());
33         string ans = need_reverse + ori_str;
34         return ans;
35     }
36     
37     //变成 $#a#b#c#d# 这种模式
38     string preProcess(string &s) {
39         string tmp = "$#";
40         for (int i = 0; i < s.size(); ++i) {
41             tmp += s[i];
42             tmp += "#";
43         }
44         return tmp;
45     }
46 };
View Code

 

posted @ 2017-02-22 13:34  zhangwanying  阅读(202)  评论(0编辑  收藏  举报