leecode刷题--两数之和

1.两数之和

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} target
 4  * @return {number[]}
 5  */
 6 var twoSum = function(nums, target) {
 7     for(let i=0;i<nums.length;i++){
 8         let rest = target - nums[i]
 9         nums[i] = NaN
10         if(nums.includes(rest)){
11             let index = nums.indexOf(rest)
12             return [i,index]
13         }
14     }
15 };

 

2.回文数

 1 /**
 2  * @param {number} x
 3  * @return {boolean}
 4  */
 5 var isPalindrome = function(x) {
 6     if(x===0) return true
 7     if(x<0 || x%10===0) return false
 8     let str = x.toString()
 9     if(str.split('').reverse().join('')===str){
10         return true
11     }else{
12         return false
13     }
14 };

 

3.罗马数字转整形

 1 /**
 2  * @param {string} s
 3  * @return {number}
 4  */
 5 var romanToInt = function(s) {
 6     let total = 0
 7     let romanArr = s.split('')
 8     for (let i=0;i<romanArr.length;i++) {
 9         if(romanArr[i]+romanArr[i+1] === 'IV'){
10             total += 4
11             i++
12             continue
13         }
14         if(romanArr[i]+romanArr[i+1] === 'IX'){
15             total += 9
16             i++
17             continue
18         }
19         if(romanArr[i]+romanArr[i+1] === 'XL'){
20             total += 40
21             i++
22             continue
23         }
24         if(romanArr[i]+romanArr[i+1] === 'XC'){
25             total += 90
26             i++
27             continue
28         }
29         if(romanArr[i]+romanArr[i+1] === 'CD'){
30             total += 400
31             i++
32             continue
33         }
34         if(romanArr[i]+romanArr[i+1] === 'CM'){
35             total += 900
36             i++
37             continue
38         }
39         if(romanArr[i] === 'I'){
40             total += 1
41             continue
42         }   
43         if(romanArr[i] === 'V'){
44             total += 5
45             continue
46         }  
47         if(romanArr[i] === 'X')
48         {
49             total += 10
50             continue
51         }  
52         if(romanArr[i] === 'L'){
53             total += 50
54             continue
55         }  
56         if(romanArr[i] === 'C'){
57             total += 100
58             continue
59         }  
60         if(romanArr[i] === 'D'){
61             total += 500
62             continue
63         }  
64         if(romanArr[i] === 'M'){
65             total += 1000
66             continue
67         }  
68     }
69     return total
70 };

 

4.最长公共前缀

 1 /**
 2  * @param {string[]} strs
 3  * @return {string}
 4  */
 5 var longestCommonPrefix = function(strs) {
 6     let index = -1
 7     let minLength = 300
 8     let res = ""
 9     const newArr = []
10     strs.forEach(item =>{
11         if(minLength>item.length) minLength = item.length
12         newArr.push(item.split(''))
13     })
14     for(let i=0;i<minLength;i++){
15         if(newArr.every(item=>{return item[i] === newArr[0][i]})) {
16             index = i
17         }else{
18             break
19         }
20     }
21     if(index === -1){
22         return ""
23     }else{
24         for(let j=0;j<=index;j++){
25             res += newArr[0][j]
26         }
27         return res
28     }
29 };

 

上一篇:leecode两数相加


下一篇:leecode初级算法-删除排序数组中的重复项