Last active
November 13, 2016 06:22
-
-
Save sj82516/788e1cee2775f4dfd0dc3650a612ed9a to your computer and use it in GitHub Desktop.
LeetCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} num | |
| * @return {string} | |
| */ | |
| var intToRoman = function(num) { | |
| var romanNums = ['I','V','X','L','C','D','M']; | |
| var ans = ""; | |
| var nums = []; | |
| for(var v=(num%10); num>0; i++,v=(num%10)){ | |
| nums.push(v); | |
| num -= v; | |
| num /= 10; | |
| } | |
| var len = nums.length; | |
| for(var i=0; i<len; i++){ | |
| var currNum = nums[i]; | |
| var romanPos = 2*i; | |
| var currRoman = ""; | |
| if(currNum==4 || currNum==9){ | |
| currRoman = romanNums[romanPos]+currRoman; | |
| currNum +=1; | |
| } | |
| if(currNum == 10){ | |
| currRoman = currRoman+romanNums[romanPos+2]; | |
| currNum -=10; | |
| } | |
| if(currNum>=5){ | |
| currRoman = currRoman+romanNums[romanPos+1]; | |
| currNum -=5; | |
| } | |
| if(currNum <= 3){ | |
| for(; currNum>0; currNum--){ | |
| currRoman = currRoman + romanNums[romanPos]; | |
| } | |
| } | |
| ans = currRoman + ans; | |
| } | |
| return ans; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} A | |
| * @param {number[]} B | |
| * @param {number[]} C | |
| * @param {number[]} D | |
| * @return {number} | |
| */ | |
| //先兩倆合併在兩倆合併,降低時間複雜度 | |
| //別忘了要計算重複數 | |
| var fourSumCount = function(A, B, C, D) { | |
| var arr1 = {}; | |
| var arr2 = {}; | |
| var ans = 0; | |
| for(var i in A){ | |
| for(var j in B){ | |
| arr1[A[i]+B[j]] = arr1[A[i]+B[j]]!==undefined? arr1[A[i]+B[j]]+1:1; | |
| } | |
| } | |
| for(var i in C){ | |
| for(var j in D){ | |
| arr2[C[i]+D[j]] = arr2[C[i]+D[j]]!==undefined? arr2[C[i]+D[j]]+1:1; | |
| } | |
| } | |
| for(var i in arr1){ | |
| ans = arr2[-1*parseInt(i)]!==undefined?ans+arr1[i]*arr2[-1*parseInt(i)]:ans; | |
| } | |
| return ans; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment