From 76302371792a1754f47370191c2119e417a430fa Mon Sep 17 00:00:00 2001 From: Sivanesh Logandurai Date: Mon, 14 Nov 2022 18:13:36 +0000 Subject: [PATCH 1/4] two sum --- two-sum.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 two-sum.js diff --git a/two-sum.js b/two-sum.js new file mode 100644 index 0000000000000..ebbaf5bb33234 --- /dev/null +++ b/two-sum.js @@ -0,0 +1,51 @@ +// Given an array of integers nums and an integer target, return +// indices of the two numbers such that they add up to target. +// You may assume that each input would have exactly one solution, +// and you may not use the same element twice. +// You can return the answer in any order. + + +// Example 1: +// Input: nums = [2,7,11,15], target = 9 +// Output: [0,1] +// Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. + +// Example 2: +// Input: nums = [3,2,4], target = 6 +// Output: [1,2] + +// Example 3: +// Input: nums = [3,3], target = 6 +// Output: [0,1] + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ + var twoSum = function(nums, target) { + const direct = nums.map((n,i) => n === target ? i : -1).filter(n => n != -1); + if (direct.length > 1) return direct; + + let result = []; + let found = false; + + + for (i = 0; i < nums.length; i++) { + for (j = i+1; j < nums.length; j++) { + if (nums[i]+nums[j] === target) { + found = true; + result = [i,j]; + break; + } else { + + } + } + if (found) break; + } + + console.log(result); + return result; +}; + +twoSum([0,4,4,0], 0) \ No newline at end of file From e4a09838aa7506e7fe982a4fb9f19a2269f635ed Mon Sep 17 00:00:00 2001 From: Sivanesh Logandurai Date: Mon, 14 Nov 2022 18:21:26 +0000 Subject: [PATCH 2/4] two sum --- two-sum.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/two-sum.js b/two-sum.js index ebbaf5bb33234..342370f5c41a5 100644 --- a/two-sum.js +++ b/two-sum.js @@ -37,9 +37,7 @@ found = true; result = [i,j]; break; - } else { - - } + } } if (found) break; } From 18be6d470b36f180bbe069515a3e7df8a2b9b85c Mon Sep 17 00:00:00 2001 From: Sivanesh Logandurai Date: Mon, 14 Nov 2022 18:22:36 +0000 Subject: [PATCH 3/4] two sum commets --- two-sum.js | 1 + 1 file changed, 1 insertion(+) diff --git a/two-sum.js b/two-sum.js index 342370f5c41a5..e60781e9ff5e1 100644 --- a/two-sum.js +++ b/two-sum.js @@ -24,6 +24,7 @@ * @return {number[]} */ var twoSum = function(nums, target) { + /** To check if the target is 0 and if we have any 0s in the list */ const direct = nums.map((n,i) => n === target ? i : -1).filter(n => n != -1); if (direct.length > 1) return direct; From b2f0e622a7391092adf0d6561d88cf8c503b30cf Mon Sep 17 00:00:00 2001 From: Sivanesh Logandurai Date: Tue, 15 Nov 2022 18:37:03 +0000 Subject: [PATCH 4/4] palindrome --- palindrome.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 palindrome.js diff --git a/palindrome.js b/palindrome.js new file mode 100644 index 0000000000000..6cd7ed755cf3e --- /dev/null +++ b/palindrome.js @@ -0,0 +1,71 @@ +// Given an integer x, return true if x is a palindrome, and false otherwise. + +// Example 1: +// Input: x = 121 +// Output: true +// Explanation: 121 reads as 121 from left to right and from right to left. + +// Example 2: +// Input: x = -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: x = 10 +// Output: false +// Explanation: Reads 01 from right to left. Therefore it is not a palindrome. + +/** + * @param {number} x + * @return {boolean} + */ +var isPalindrome = function(x) { + const s = x.toString(); + const len = s.length; + const mid = Math.floor(len / 2); + const lenidx = len - 1; + let isPalindrome = true; + for (let i = 0; i < mid; i++) { + if (s[i] !== s[lenidx-i]) { + isPalindrome = false; + break + }; + } + return isPalindrome; +}; + +/** + * @param {number} x + * @return {boolean} + */ + var isPalindromeNoString = function(x) { + if (x < 0) return false; + places = []; + for (let i = 1; i <= x; i=i*10) { + m = Math.floor(x / i % 10); + places.push(m); + } + console.log(places); + const len = places.length; + const mid = Math.floor(len / 2); + const lenidx = len - 1; + let isPalindrome = true; + for (let i = 0; i < mid; i++) { + if (places[i] !== places[lenidx-i]) { + isPalindrome = false; + break + }; + } + + return isPalindrome; +}; + + +// console.log(isPalindrome(-121)); + +console.log(isPalindromeNoString(121)); +console.log(isPalindromeNoString(10)); + + + + \ No newline at end of file