diff --git a/src/array/two-sum.js b/src/array/two-sum.js index 5ecac6f..6346649 100644 --- a/src/array/two-sum.js +++ b/src/array/two-sum.js @@ -34,4 +34,29 @@ const twoSum = (nums, target) => { return null; }; +/** + * Brute Force + */ + +/** + * + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +/** + * + */ +const twoSumBrute = function (nums, target) { + for (let i = 0; i < nums.length - 1; i++) { + for (let nextIndex = i + 1; nextIndex < nums.length; nextIndex++) { + if (target === nums[i] + nums[nextIndex]) { + return [i, nextIndex]; + } + } + } +}; + +console.log(twoSumBrute([3, 2, 4], 6)); // [0,1] + export { twoSum }; diff --git a/src/string/valid-palindrome-ii.js b/src/string/valid-palindrome-ii.js index 9add85a..6450585 100644 --- a/src/string/valid-palindrome-ii.js +++ b/src/string/valid-palindrome-ii.js @@ -44,4 +44,19 @@ const isPalindromic = (s, i, j) => { return true; }; +/** + * Using RegExp + */ + +var isPalindrome = function(s) { + s = s.replace(/[^0-9a-z]/gi, ''); + return ( + s + .split('') + .reverse() + .join('') + .toLowerCase() == s.toLowerCase() + ); +}; + export { validPalindrome };