Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
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.
解题方案
思路 **- 时间复杂度: O(1)**- 空间复杂度: O(N)**
思路:通过将数字转成数组,然后翻转再转回数字
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * @param {number}x * @return {number} */ var reverse = function(x) { var num = parseInt(x.toString().split('').reverse().join('')) if(num > Math.pow(2, 31)) { return0 } if(x < 0){ return num*(-1) } else { return num } };
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 5
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 5
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 5
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.
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121 Output: true Example 2:
Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3:
Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up:
Coud you solve it without converting the integer to a string?
解题方案
思路 1 **- 时间复杂度: O(log2 N)**- 空间复杂度: O(1)**
不使用字符串,使用除法分别从首尾获得数字,最后对比是否相同
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * @param {number}x * @return {boolean} */ var isPalindrome = function(x) { if(x<0||x!==0&&x%10===0) returnfalse; var reverse = 0; while (x>reverse){ reverse = reverse*10 +x%10; x = Math.floor(x/10); } return reverse === x||Math.floor(reverse/10) === x; };
思路 2 **- 时间复杂度: O(N)**- 空间复杂度: O(N)**
转化为字符串,reverse字符串
代码:
1 2 3
var isPalindrome = function(x) { return x.toString().split('').reverse().join('')==x.toString()?true: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.
1
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.
1 2 3 4
Example:
Input: [1,8,6,2,5,4,8,3,7] Output: 49
解题方案
对撞指针 **- 时间复杂度: O(N)**- 空间复杂度: O(1)**
从数组两层对向查找,直到找到最大乘积
代码:
1 2 3 4 5 6 7 8 9 10 11 12
var maxArea = function (list) { let i = 0,j = list.length -1,result = 0; while(i < j){ result = Math.max(result ,(j - i ) * Math.min(list[i],list[j])) if(list[i] < list[j]){ i++; }else{ j--; } } return result; };
暴力解法 **- 时间复杂度: O(N²)**- 空间复杂度: O(1)**
代码
1 2 3 4 5 6 7 8 9 10 11
var maxArea = function (list) { let result = 0; for(let i = 0,len = list.length; i<len; i++){ for(let j = i+1; j<len; j++){ let x = (j - i); let y = Math.min(list[i],list[j]); result = Math.max(result,x*y) } } return result; };
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.
let match = function (result,num) { let obj = getMap(); while (result.num >= num){ let n = parseInt(result.num /num); result.num = result.num % num; result.str = result.str + obj[num].repeat(n); } };
/** * @param {number}num * @return {string} */ var intToRoman = function (num) { if(num < 1 || num > 3999) throwError('error'); let obj = getMap(); if(num in obj) return obj[num]; let result = { str:'', num }; match(result,1000); match(result,900); match(result,500); match(result,400); match(result,100); match(result,90); match(result,50); match(result,40); match(result,10); match(result,9); match(result,5); match(result,4); match(result,1);
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 nums such 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:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
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:
Given array nums = [-1, 2, 1, -4], and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).