摘要:
思路: 这道题比较难,有一个解释很好:Brute force solution, O(N3):The obvious brute force solution is to pick all possible starting and ending positions for a substring, and verify if it is a palindrome. There are a total of C(N, 2) such substrings (excluding the trivial solution where a character itself is a palindro 阅读全文
摘要:
1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string> &strs) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 string result; 7 if (strs.size() == 0) return result; 8 if (strs.size() == 1){ 9 ... 阅读全文
摘要:
1 class Solution { 2 private: 3 map<char, vector<char> > dict; 4 vector<string> ret; 5 public: 6 void createDict() 7 { 8 dict.clear(); 9 dict['2'].push_back('a');10 dict['2'].push_back('b');11 dict['2'].push_back('c');12 dict['3 阅读全文
摘要:
1 class Solution { 2 public: 3 int lengthOfLastWord(const char *s) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (*s == NULL) return 0; 7 8 int length = 0; 9 int result = 0;10 11 while (*... 阅读全文
摘要:
1 class Solution { 2 public: 3 int largestRectangleArea(vector<int> &h) { 4 5 stack<int> stk; 6 int index = 0, maxArea = 0; 7 h.push_back(0); 8 9 while(index < h.size()) {10 11 if(stk.empty() || h[stk.top()] <= h[index]){12 13 stk... 阅读全文
摘要:
1 class Solution { 2 public: 3 int jump(int A[], int n) { 4 5 if (n <= 1) return 0; 6 7 int maxReachedLastTime = 0; 8 int maxReachedNow = 0; 9 int minSteps = 0;10 11 for (int index = 0; index<n; index++){12 13 ... 阅读全文
摘要:
1 class Solution { 2 public: 3 bool canJump(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (n <=1) return true; 7 8 int maxReached = 0; 9 int index = 0;10 11 while (index <... 阅读全文
摘要:
1 class Solution { 2 public: 3 bool isInterleave(string s1, string s2, string s3) { 4 if(s3.size() != s1.size() + s2.size()) 5 return false; 6 //create indicator 7 vector<vector<bool> > match(s1.size() + 1, vector<bool>(s2.size() + 1, false)); 8 //ini... 阅读全文
摘要:
1 class Solution { 2 public: 3 string intToRoman(int num) { 4 const static char* Roman = "IVXLCDM"; 5 string ret; 6 for (int base = 0; num; num /= 10, base += 2) { 7 int x = num % 10; 8 switch (x) { 9 case 1: case 2: case 3:10 ... 阅读全文