36. Valid Sudoku
题目描述
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.
代码实现
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
vector<map<char, int> > stro(27);
const unsigned int num = 9;
for(int row = 0; row < num; row++) {
for(int col = 0; col < num; col++) {
char tmp = board[row][col];
if(tmp != '.') {
if(stro[row].count(tmp)) return false;
else stro[row][tmp] = 1;
if(stro[num + col].count(tmp)) return false;
else stro[num + col][tmp] = 1;
if(stro[(num << 1) + (row/3)*3 + (col/3)].count(tmp)) return false;
else stro[(num << 1) + (row/3)*3 + (col/3)][tmp] = 1;
}
}
}
return true;
}
};
434. Number of Segments in a String
题目描述
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.
Example:
Input: "Hello, my name is John"
Output: 5
代码实现
使用了stringstream实现。
class Solution {
public:
int countSegments(string s) {
stringstream ss(s); string tmp; int cnt = 0;
while(ss >> tmp) cnt++; return cnt;
}
};
344. Reverse String
题目描述
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
代码实现
class Solution {
public:
string reverseString(string s) {
int lower = 0, higher = s.length() - 1;
while(lower < higher) {
char tmp = s[lower]; s[lower] = s[higher]; s[higher] = tmp;
lower++, higher--;
}
return s;
}
};
345. Reverse Vowels of a String
题目描述
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
The vowels does not include the letter "y".
代码实现
class Solution {
public:
bool isVowel(char tmp) {
return (tmp == 'a' || tmp == 'e' || tmp == 'i' || tmp == 'o' || tmp == 'u' || tmp == 'A' || tmp == 'E' || tmp == 'I' || tmp == 'O' || tmp == 'U');
}
string reverseVowels(string s) {
int lower = 0, higher = s.length() - 1;
while(lower < higher) {
while(!isVowel(s[lower]) && lower < higher) lower++;
while(!isVowel(s[higher]) && lower < higher) higher--;
if(lower >= higher) return s;
else {
char tmp = s[lower];
s[lower] = s[higher];
s[higher] = tmp;
}
lower++;
higher--;
}
return s;
}
};
字符串处理:http://blog.youkuaiyun.com/weixin_35929051/article/details/52502486