diff --git a/solution/001.Two Sum/Solution.js b/solution/001.Two Sum/Solution.js new file mode 100644 index 0000000000000..ff5d195926f06 --- /dev/null +++ b/solution/001.Two Sum/Solution.js @@ -0,0 +1,11 @@ +const twoSum = function(nums, target) { + const map = {}; + + for (let i = 0; i < nums.length; i++) { + if (map[nums[i]] !== undefined) { + return [map[nums[i]], i] + } else { + map[target - nums[i]] = i + } + } +}; \ No newline at end of file diff --git a/solution/007.Reverse Integer/Solution.js b/solution/007.Reverse Integer/Solution.js new file mode 100644 index 0000000000000..a7f5626e0e0a1 --- /dev/null +++ b/solution/007.Reverse Integer/Solution.js @@ -0,0 +1,15 @@ +const reverse1 = function(x){ + let s = String(x); + let isNegative = false; + if(s[0] === '-'){ + isNegative = true; + } + s = parseInt(s.split('').reverse().join('')); + return isNegative ? (s > Math.pow(2,31) ? 0 : -s) : (s > Math.pow(2,31) - 1 ? 0 : s); +} + +const reverse = function(x){ + let result = parseInt(x.toString().split('').reverse().join('')); + if(result > Math.pow(2,31) - 1 || -result < Math.pow(-2,31)) return 0; + return x > 0 ? result: -result; +} \ No newline at end of file diff --git a/solution/014.Longest Common Prefix/Solution.js b/solution/014.Longest Common Prefix/Solution.js new file mode 100644 index 0000000000000..12b315f04fb8a --- /dev/null +++ b/solution/014.Longest Common Prefix/Solution.js @@ -0,0 +1,11 @@ +const longestCommonPrefix = function(strs){ + if(strs.length === 0) return ''; + for(let j = 0; j < strs[0].length; j++){ + for(let i = 0; i < strs.length; i++){ + if(strs[0][j] !== strs[i][j]){ + return strs[0].substring(0,j); + } + } + } + return strs[0]; +} \ No newline at end of file