LeetCode Algorithm
原文出处:【LeetCode】
算法参考:【陈皓 coolshell】
1. Two Sum
3. Longest Substring Without Repeating Characters
5. Longest Palindromic Substring
6. ZigZag Conversion
7. Reverse Integer
8. String to Integer (atoi)
9. Palindrome Number
12. Integer to Roman
13. Roman to Integer
14. Longest Common Prefix
15. 3Sum
17. Letter Combinations of a Phone Number
19. Remove Nth Node From End of List
20. Valid Parentheses
22. Generate Parentheses
24. Swap Nodes in Pairs
26. Remove Duplicates from Sorted Array
27. Remove Element
28. Implement strStr()
31. Next Permutation
32. Longest Valid Parentheses
34. Search for a Range
35. Search Insert Position
1. Two Sum
/********************************************************** ** 1. Two Sum ** Given an array of integers, return indices of the two numbers such that they add up to a specific target. ** You may assume that each input would have exactly one solution, and you may not use the same element twice. ** Example: ** Given nums = [2, 7, 11, 15], target = 9, ** Because nums[0] + nums[1] = 2 + 7 = 9, ** return [0, 1]. **********************************************************/ #include <iostream> #include <unordered_map> #include <vector> using namespace std; vector<int> twoSum(vector<int> &numbers, int target) { unordered_map<int, int> m; vector<int> result; for (int i=0; i<numbers.size(); ++i) { if (m.find(numbers[i]) == m.end()) { m[target-numbers[i]] = i; }else { result.push_back(m[numbers[i]]); result.push_back(i); break; } } return result; } //-----example----- int main(int argc, char **argv) { vector<int> numbers = {2, 7, 11, 15}; int target = 9; vector<int> result = twoSum(numbers, target); cout << "result["; for (auto it=result.begin(); it!=result.end(); ++it) { cout << *it; if (it != result.end()-1) { cout << ", "; } } cout << "]" << endl; return 0; } //-----output----- //result[0, 1]
3.Longest Substring Without Repeating Characters
/********************************************************** ** 3.Longest Substring Without Repeating Characters ** Given a string, find the length of the longest substring without repeating characters. ** ** Examples: ** Given "abcabcbb", the answer is "abc", which the length is 3. ** Given "bbbbb", the answer is "b", with the length of 1. ** Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. **********************************************************/ #include <string> #include <iostream> #include <map> using namespace std; int lengthOfLongestSubstring(string s) { int longestLen = 0; int lastRepeatPos = -1; map<char, int> matchCharacters; for (int i=0; i<s.size(); ++i) { if (matchCharacters.find(s[i]) != matchCharacters.end() && lastRepeatPos < matchCharacters[s[i]]) { lastRepeatPos = matchCharacters[s[i]]; } if (i-lastRepeatPos > longestLen) { longestLen = i-lastRepeatPos; } matchCharacters[s[i]] = i; } return longestLen; } //-----example----- int main(int argc, char ** argv) { string s = "abcabcbb"; cout << s << ":" << lengthOfLongestSubstring(s) << endl; s = "bbbbb"; cout << s << ":" << lengthOfLongestSubstring(s) << endl; s = "pwwkew"; cout << s << ":" << lengthOfLongestSubstring(s) << endl; return 0; } //-----output----- //abcabcbb:3 //bbbbb:1 //pwwkew:3
5. Longest Palindromic Substring
/********************************************************** ** 5. 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. ** ** Example: ** Input: "babad" ** Output: "bab" ** ** Note: "aba" is also a valid answer. ** ** Example: ** Input: "cbbd" ** Output: "bb" **********************************************************/ #include <string> #include <iostream> #include <vector> using namespace std; string findPalindrom(string s, int left, int right) { while (left>=0 && right<=s.size()-1 && s[left]==s[right]) { left--; right++; } return s.substr(left+1, right-left-1); } string longestPalindrom(string s) { int n = s.size(); if (n <= 1) return s; string longest; string str; for (int i=0; i<n-1; ++i) { str = findPalindrom(s, i, i); if (str.size() > longest.size()) { longest = str; } str = findPalindrom(s, i, i+1); if (str.size() > longest.size()) { longest = str; } } return longest; } //-----example----- int main(int argc, char **argv) { string s = "babad"; cout << s << " : " << longestPalindrom(s) << endl; s = "cbbd"; cout << s << " : " << longestPalindrom(s) << endl; return 0; } //-----output----- //babad : bab //cbbd : bb
6. ZigZag Conversion
/********************************************************** ** 6. ZigZag Conversion ** The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) ** ** P A H N ** A P L S I I G ** Y I R ** And then read line by line: "PAHNAPLSIIGYIR" ** Write the code that will take a string and make this conversion given a number of rows: ** ** string convert(string text, int nRows); ** convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR". **********************************************************/ #include <string> #include <iostream> #include <vector> using namespace std; string zigzagConvert(string s, int rows) { if (rows<=1 || rows>=s.size()) return s; vector<string> r(rows); int index = 0; int step = 1; for (int i=0; i<s.size(); ++i) { if (index == 0) step = 1; if (index == rows-1) step = -1; r[index] += s[i]; index += step; } string result; for (int i=0; i<rows; ++i) { result += r[i]; } return result; } //-----example----- int main(int argc, char **argv) { string s = "PAYPALISHIRING"; int rows = 3; cout << s << "[" << rows << "]:" <<zigzagConvert(s, rows) << endl; return 0; } //-----output----- //PAYPALISHIRING[3]:PAHNAPLSIIGYIR
7. Reverse Integer
/********************************************************** ** 7. Reverse Integer ** Reverse digits of an integer. ** ** Example1: x = 123, return 321 ** Example2: x = -123, return -321 ** ** click to show spoilers. ** ** Note: ** The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. **********************************************************/ #include <iostream> using namespace std; #define INT_MAX_VALUE 2147483647 #define INT_MIN_VALUE (-INT_MAX_VALUE-1) int reverseInterget(int x) { int result = 0; int n = 0; while (x != 0) { n = x%10; if (result>INT_MAX_VALUE/10 || result <INT_MIN_VALUE/10) { return 0; } result = result*10+n; x /=10; } return result; } //-----example----- int main(int argc, char **argv) { cout << "123 : " << reverseInterget(123) << endl; cout << "-123 : " << reverseInterget(-123) << endl; cout << "1020 : " << reverseInterget(1020) << endl; cout << "2147483647 : " << reverseInterget(2147483647) << endl; cout << "-2147483648 : " << reverseInterget(-2147483648) << endl; return 0; } //-----output----- //123 : 321 //-123 : -321 //1020 : 201 //2147483647 : 0 //-2147483648 : 0
8. String to Integer (atoi)
/********************************************************** ** 8. String to Integer (atoi) ** Implement atoi to convert a string to an integer. ** ** Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. ** ** Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. **********************************************************/ #include <iostream> #include <ctype.h> using namespace std; #define INT_MAX_VALUE 2147483647 #define INT_MIN_VALUE (-INT_MAX_VALUE-1) int stringToInterget(const char *str) { if (str==nullptr || *str=='