You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Given a string, find the length of the longest substring without repeating characters.
Example 1:
1 2 3
Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.
Example 2:
1 2 3
Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.
Example 3:
1 2 3 4
Input: "pwwkew" Output: 3 Explanation: 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.
分析:
这题是一个最长字符子串的问题。我个人想法使用一个字典来记录某字符出现次数。
我们用 i、j 指向头部,不断增加 j 的大小,不断增加字典中字符的次数,如果出现某一个字符串出现次数大于两次,就不断移动 i 直到没有重复字符串为止。最后 maxlen 不停更新为最大的值。
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)
1 2 3
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:
1
string convert(string s, int numRows);
Example 1:
1 2
Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2:
1 2 3 4 5 6 7 8
Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation:
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
1 2
Input: 123 Output: 321
Example 2:
1 2
Input: -123 Output: -321
Example 3:
1 2
Input: 120 Output: 21
Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
分析:
感觉自己也是挺赖的,用了 to_string 转字符串,然后还用了 stoi 转会 int。可能第一遍刷的时候确实只是在解决问题,却不是最好的解决问题。
classSolution { public: intreverse(int x){ int i = 0; int temp = x; string int_string = to_string(x); int result = 0; while (temp != 0) { if ((temp % 10) == 0) { ++i; temp = temp / 10; } else { break; } } if (x >= 0) { std::reverse(int_string.begin(), int_string.end() - i); } elseif (x < 0) { std::reverse(int_string.begin() + 1, int_string.end() - i); } stringresult_string(int_string,0,int_string.size()-i); try { result = std::stoi(result_string); } catch(std::out_of_range err) { return0; } return result; } };
8. String to Integer (atoi)
Medium
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
Only the space character ' ' is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1:
1 2
Input: "42" Output: 42
Example 2:
1 2 3 4
Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42.
Example 3:
1 2 3
Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
1 2 3 4
Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
1 2 3 4
Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned.
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.
1 2
'.' Matches any single character. '*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.
Example 1:
1 2 3 4 5
Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa".
Example 2:
1 2 3 4 5
Input: s = "aa" p = "a*" Output: true Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:
1 2 3 4 5
Input: s = "ab" p = ".*" Output: true Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:
1 2 3 4 5
Input: s = "aab" p = "c*a*b" Output: true Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
Example 5:
1 2 3 4
Input: s = "mississippi" p = "mis*is*p*." Output: false
Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
classSolution { public: intmaxArea(vector<int>& height){ int i = 0,j = (height.size()-1); int min_height = 0; int water = 0; while(i < j) { min_height = min(height[i],height[j]); water = max(water,min_height * (j-i)); while(height[i] <= min_height && i<j) { ++i; } while(height[j] <= min_height && i<j) { --j; } } return water; } };
12. Integer to Roman
Medium
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
1 2 3 4 5 6 7 8
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
1 2
Input: 3 Output: "III"
Example 2:
1 2
Input: 4 Output: "IV"
Example 3:
1 2
Input: 9 Output: "IX"
Example 4:
1 2 3
Input: 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3.
Example 5:
1 2 3
Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
classSolution { public: stringintToRoman(int num){ string result; for(;num >= 1000;num = num - 1000,result += "M"); for(;num >= 500;) { num = num - 500; if(num / 100 == 4) { result += "CM"; num = num - 400; } else { result += "D"; } } for(;num >= 100;) { if(num / 100 == 4) { result += "CD"; num = num - 400; } else { num = num - 100; result += "C"; }
} for(;num >= 50;) { num = num - 50; if(num / 10 == 4) { result += "XC"; num = num - 40; } else { result += "L"; } } for(;num >= 10;) { if(num / 10 == 4) { result += "XL"; num = num - 40; } else { result += "X"; num -= 10; } } for(;num >= 1;) { if(num >= 5) { if(num % 5 == 4) { result += "IX"; num -= 9; } else { num = num % 5; result += "V"; } } else { if(num == 4) { result += "IV"; break; } else { result += "I"; num--; } } } return result; } };
13. Roman to Integer
Easy
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
1 2 3 4 5 6 7 8
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
1 2
Input: "III" Output: 3
Example 2:
1 2
Input: "IV" Output: 4
Example 3:
1 2
Input: "IX" Output: 9
Example 4:
1 2 3
Input: "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3.
Example 5:
1 2 3
Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Given an array nums of n integers, are there elements a, b, c in numssuch that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
1 2 3 4 5 6 7
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
分析:
这题我们需要将数组进行排序来减小时间复杂度
然后我们固定一个位置,将整个问题变成一个子问题。子问题就是在子数组里面找到对应的和为要求值的位置。然后不断增加 i 即可。
while (front < back && num[back] == triplet[2]) back--; } }
while (i + 1 < num.size() && num[i + 1] == num[i]) i++;
} return res;
} };
16. 3Sum Closest
Medium
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
1 2 3
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
for(auto i:digits) { temp_number_letter.push_back(number_letter[ (i - '0') - 2]); }
for(auto i:temp_number_letter) { total_size *= i.size(); } for( int i = 0 ; i < total_size ; ++i ) { string loop_temp; int m = 0,n = i;
for(auto r_iter : temp_number_letter) { //n = n % temp_number_letter[r_iter].size(); //loop_temp += temp_number_letter[r_iter][n]; m = n % r_iter.size(); n = n / r_iter.size(); loop_temp += r_iter[m]; } result.push_back((loop_temp)); } sort(result.begin(),result.end()); return result; } };
18. 4Sum
Medium
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
The solution set must not contain duplicate quadruplets.
Example:
1 2 3 4 5 6 7 8
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]