2 <= nums.length <= 104 -109 <= nums[i] <= 109 -109 <= target <= 109 Only one valid answer exists.
Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: vector<int> twoSum(vector<int>& nums, int target){ int size = nums.size(); vector<int> ret; for(int i = 0; i < size; i++) { for(int j = i + 1; j < size; j++) { if(nums[i] + nums[j] == target) { ret.push_back(i); ret.push_back(j); } } } return ret; } };
Runtime: 40 ms
Memory Usage: 8.9 MB
2. Add Two Numbers
Intro
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 contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Given a string s, find the length of the longest substring without repeating characters.
Example 1:
1 2 3
Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.
Example 2:
1 2 3
Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.
Example 3:
1 2 3 4
Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Example 4:
1 2
Input: s = "" Output: 0
Constraints:
0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.
classSolution { public: intlengthOfLongestSubstring(string s){ int ch_map[127] = {0}; int out = 0; int sum = 0; int length = s.length(); for(int i = 0; i < length; i++) {
Given a signed 32-bit integer x, return xwith its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
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, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 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.
Example 1:
1 2 3
Input: s = "III" Output: 3 Explanation: III = 3.
Example 2:
1 2 3
Input: s = "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3.
Example 3:
1 2 3
Input: s = "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
int ans = 0; int len = s.size(); for(int i = 0; i < len-1; i++){ if(table[s[i]] >= table[s[i+1]]){ ans += table[s[i]]; }else{ ans += -table[s[i]]; } } ans += table[s[len-1]]; return ans; } };
Runtime: 10 ms
Memory Usage: 8.3 MB
14
Intro
easy
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".