1 reverse words in a string
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",return "blue is sky the".Clarification
- What constitutes a word?A sequence of non-space characters constitutes a word.- Could the input string contain leading or trailing spaces?Yes. However, your reversed string should not contain leading or trailing spaces.- How about multiple spaces between two words?Reduce them to a single space in the reversed string.class Solution{public: string reverseWords(string s){ if(s.empty()){ return s; } string s_ret, s_temp; string::size_type ix = s.size(); while(ix != 0){ s_temp.clear(); while(!isspace(s[--ix])){ s_temp.push_back(s[ix]); if(ix == 0){ break; } } if(!s_temp.empty()){ if(!s_ret.empty()){ s_ret.push_back(' '); } std::reverse(s_temp.begin(), s_temp.end()); s_ret.append(s_temp); } } return s_ret; }};
2 valid palindrome
Given a string, determine if it is a palindrome,
considering only alphanumeric characters and ignoring cases.Example
"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome.Note
Have you consider that the string might be empty?This is a good question to ask during an interview.For the purpose of this problem,we define empty string as valid palindrome.Challenge
O(n) time without extra memory.class Solution{public: bool isPalindrome(string& s){ if(s.empty()) return true; int l = 0, r = s.size() - 1; while(l < r){ if(!isalnum(s[l])){ ++l; continue; } if(!isalnum(s[r])){ --r; continue; } if(tolower(s[l]) == tolower(s[r])){ ++l; --r; }else{ return false; } } return true; }};
3 longest palindromic substring
Given a string S, find the longest palindromic substring in S.
You may assume that the maximum length of S is 1000,and there exists one unique longest palindromic substring.Example
Given the string = "abcdzdcab", return "cdzdc".ChallengeO(n2) time is acceptable. Can you do it in O(n) time.//题解1穷竭搜索 穷举所有可能的子串,判断子串是否为回文,使用一变量记录最大回文长度//若新的回文超过之前的最大回文长度则更新标记变量并记录当前回文的起止索引,最后返回最长回文子串。class Solution{public: string longestPalindrome(string& s){ string result; if(s.empty()) return s; int n = s.size(); int longest = 0, left = 0, right = 0; for(int i = 0; i < n; ++i){ for(int j = i + 1; j <= n; ++j){ string substr = s.substr(i ,j - i); if(isPalindrome(substr) && substr.size() > longest){ longest = j - i; left = i; right = j; } } } result = s.substr(left, right - left); return result; }private: bool isPalindrome(string &s){ int n = s.size(); for(int i = 0; i < n; i++){ if(s[i] != s[n - i - 1]) return false; } return true; }};
//题解2 假定扫描的每个字母是回文的中间位置(需要处理奇偶两种情况),从该位置向两头搜索寻找最大回文长度
class Solution{public: string palindrome(string& s, int l, int r){ while(l >= 0 && r < s.size() && s[l] == s[r]) l--, r++; return s.substr(l + 1, r - l - 1); } string longestPalindrome(string s){ if(s.empty()) return s; string res; for(int i = 0; i < s.size(); i++){ string t; t = palindrome(s, i, i); if(t.size() > res.size()) res = t; t = palindrome(s, i, i + 1); if(t.size() > res.size()) res = t; } return res; }};
4 space replacement
Write a method to replace all spaces in a string with %20.
The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string.Example
Given "Mr John Smith", length = 13.The string after replacement should be "Mr%20John%20Smith".Note
If you are using Java or Python,please use characters array instead of string.Challenge
Do it in-place.//先遍历一遍求得空格数,得到『新数组』的实际长度,从后往前遍历。class Solution{public: int replaceBlank(char string[], int length){ int n = 0; for(int i = 0; i < length; i++) if(string[i] == ' ') n++; int new_len = length + n*2; for(int i = length - 1; i >= 0; i--){ if(string[i] != ' '){ string[--new_len] = string[i]; }else{ string[--new_len] = '0'; string[--new_len] = '2'; string[--new_len] = '%'; } } return length + n * 2; }};
5 wildcard matching
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).Example
isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → falseclass Solution{public: bool isMatch(string s, string p) { int star = 0, ss = 0, i = 0, j = 0; while (s[i]) { if (p[j] == '?' || p[j] == s[i]) { j++; i++; continue; } if (p[j] == '*') { star = ++j; ss = i; continue; } if (star) { j = star; i = ++ss; continue; } return false; } while (p[j] == '*') j++; return !p[j]; }};
6 length of last word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ',
return the length of last word in the string.If the last word does not exist, return 0.
ExampleGiven s = "Hello World", return 5.Note
A word is defined as a character sequence consists of non-space characters only.class Solution{public: int lengthOfLastWord(string s){ if(s.size() == 0) return 0; int count = 0; for(int i = s.size() - 1; i >= 0; i--) if(s[i] == ' '){ if(count) break; }else count++; return count; }};
7 count and say
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211.Given an integer n, generate the nth sequence.
ExampleGiven n = 5, return "111221".Note
The sequence of integers will be represented as a string.//题解1 本课程内容,由作者授权实验楼发布,未经允许,禁止转载、下载及非法传播题目大意是找第 n 个数(字符串表示),规则则是对于连续字符串,表示为重复次数+数本身。class Solution{public: string countAndSay(int n){ if(n == 0) return ""; string res = "1"; while(--n){ string cur = ""; for(int i = 0; i < res.size(); i++){ int count = 1; while((i + 1 < res.size()) && (res[i] == res[i + 1])){ count++; i++; } cur += to_string(count) + res[i]; } res = cur; } return res; }};
//题解2 递归class Solution{public: string countAndSay(int n){ if(n == 1) return "1"; string res, tmp = countAndSay(n - 1); char c = tmp[0]; int count = 1; for(int i = 1; i < tmp.size(); i++) if(tmp[i] == c) count++; else{ res += to_string(count); res.push_back(c); c = tmp[i]; count = 1; } res += to_string(count); res.push_back(c); return res; }};