From 198391decc2b47f0b5df97a17ada21b34e5d3eeb Mon Sep 17 00:00:00 2001 From: Mavis Date: Wed, 19 Nov 2025 13:56:47 +0800 Subject: [PATCH 01/20] add 2154 --- javascript/LeetCode/Array/2154.js | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 javascript/LeetCode/Array/2154.js diff --git a/javascript/LeetCode/Array/2154.js b/javascript/LeetCode/Array/2154.js new file mode 100644 index 0000000..d09247f --- /dev/null +++ b/javascript/LeetCode/Array/2154.js @@ -0,0 +1,35 @@ +/** + * 2154. Keep Multiplying Found Values by Two + * + * 初次先尋找original是否在nums內,若不在直接回傳original + * 若在,則original * 2 = 新的original (重複此步驟直到找不到original為止) + * + * @param {number[]} nums + * @param {number} original + * @return {number} + */ +var findFinalValue = function(nums, original) { + nums.sort((a,b) => a - b); + for(let i = 0;i < nums.length;++i) { + if(nums.includes(original)){ + original *= 2; + } + } + return original; + + // solution 2 + // nums.sort((a,b) => a - b); + // for(const a of nums){ + // if(a === original){ + // original*=2; + // } + // } + // return original; +}; +// let nums = [5,3,6,1,12], original = 3; +// 24 +// let nums = [2,7,9], original = 4; +// 4 +let nums = [8,19,4,2,15,3], original = 2; +// 16 +console.log(findFinalValue(nums,original)); \ No newline at end of file From 064ae0b232e0f486e8fc833864f68fefbbfa6255 Mon Sep 17 00:00:00 2001 From: Mavis Date: Fri, 21 Nov 2025 14:46:10 +0800 Subject: [PATCH 02/20] add 1930 --- javascript/LeetCode/String/1930.js | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 javascript/LeetCode/String/1930.js diff --git a/javascript/LeetCode/String/1930.js b/javascript/LeetCode/String/1930.js new file mode 100644 index 0000000..294d1f9 --- /dev/null +++ b/javascript/LeetCode/String/1930.js @@ -0,0 +1,63 @@ +/** + * 1930. Unique Length-3 Palindromic Subsequences + * + * 回傳屬於s迴文的子字串 + * + * @param {string} s + * @return {number} + */ +var countPalindromicSubsequence = function(s) { + // 只能有3個字母,第一個字母和最後一個字母是一樣的,唯獨中間字母不同 = 迴文 + // 會有一個字母出現至少2次 + + // solution 1. + // let ans = 0; + // let set = new Set(s); + // for(const char of set) { + // let start = s.indexOf(char); + // let end = s.lastIndexOf(char); + + // if(start < end){ + // ans += new Set(s.slice(start + 1, end)).size; + // } + // } + // return ans; + + // solution 2. + let map = new Map(); + let ans =0; + for(let i = 0;i < s.length;++i) { + if (!map.has(s[i])) { + map.set(s[i], []); + } + map.get(s[i]).push(i); + } + console.log(map) + for(const [char,index] of map) { + const start = index[0]; + const end = index[index.length - 1]; + if (end - start <= 1) { + continue; + } + + const set = new Set(); + for (let i = start + 1; i < end; i++) { + set.add(s[i]); + } + ans += set.size; + } + return ans; +}; +// let s = "aabca"; +/** + * 3 + * The 3 palindromic subsequences of length 3 are: + * "aba" (subsequence of "aabca") + * "aaa" (subsequence of "aabca") + * "aca" (subsequence of "aabca") + */ +// let s = "uuuuu"; +// 1 +let s ="ckafnafqo" +// 4 +console.log(countPalindromicSubsequence(s)); \ No newline at end of file From 62b6059269222fc36cd15a6c9f12139347601e9c Mon Sep 17 00:00:00 2001 From: Mavis Date: Mon, 24 Nov 2025 15:31:01 +0800 Subject: [PATCH 03/20] add solutions of 4 problems --- javascript/LeetCode/Array/3190.js | 20 +++++++++ javascript/LeetCode/String/28.js | 75 +++++-------------------------- javascript/LeetCode/String/58.js | 34 ++++++++++++++ javascript/LeetCode/math/9.js | 17 +++++++ 4 files changed, 82 insertions(+), 64 deletions(-) create mode 100644 javascript/LeetCode/Array/3190.js create mode 100644 javascript/LeetCode/String/58.js create mode 100644 javascript/LeetCode/math/9.js diff --git a/javascript/LeetCode/Array/3190.js b/javascript/LeetCode/Array/3190.js new file mode 100644 index 0000000..08c268f --- /dev/null +++ b/javascript/LeetCode/Array/3190.js @@ -0,0 +1,20 @@ +/** + * 3190. Find Minimum Operations to Make All Elements Divisible by Three + * + * 計算能將所有元素被3整除的步驟為幾,一次操作能將元素+1或-1。 + * + * @param {number[]} nums + * @return {number} + */ +var minimumOperations = function(nums) { + let answer = 0; + for(let i = 0;i < nums.length;++i) { + if(nums[i] % 3 !== 0){ + answer++; + } + } + return answer; +}; +let nums = [1,2,3,4]; +// 3 +console.log(minimumOperations(nums)); \ No newline at end of file diff --git a/javascript/LeetCode/String/28.js b/javascript/LeetCode/String/28.js index 51a685c..30ae897 100644 --- a/javascript/LeetCode/String/28.js +++ b/javascript/LeetCode/String/28.js @@ -1,70 +1,17 @@ - -/** - * 28. Implement strStr() - * Difficulty:Easy(459延伸題) - * - * Implement strStr(). - * - * Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. - * - * Clarification: - * What should we return when needle is an empty string? This is a great question to ask during an interview. - * For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf(). - * - * --------------------------------------------------- - * Input: haystack and needle are string - * Output:在haystack中第一個出現的needle index值 - * 若haystack沒有needle,return -1; - * 或needle是空的,return 0 - * ------------------------------------------------------ - * Example 1: - * Input: haystack = "hello", needle = "ll" - * Output: 2 - * - * Example 2: - * Input: haystack = "aaaaa", needle = "bba" - * Output: -1 - * - * Example 3: - * Input: haystack = "", needle = "" - * Output: 0 - * - * Constraints: - * 0 <= haystack.length, needle.length <= 5 * 104 - * haystack and needle consist of only lower-case English characters. - */ /** + * 28. Find the Index of the First Occurrence in a String + + * 回傳needle第一次出現在haystack的index,找不到回傳-1 + * * @param {string} haystack * @param {string} needle * @return {number} */ -var strStr = function (haystack, needle) { - /** - * haystack中: - * 有needle,return haystack index - * 沒有,return -1 - * $haystack或$needle是空,retunr 0 - * - * $haystack、$needle 都是英文小寫 - */ - - if (needle === "") { - return 0; - } - for (let i = 0; i < haystack.length; i++) { - - // Return part of string: - // for JS: string.substr(start,end) - // for PHP: substr(string,start,end) - let slice = haystack.substr(i, needle.length); - if (slice === needle) { - return i; - } - } - return -1; - +var strStr = function(haystack, needle) { + return haystack.indexOf(needle); }; -let h = "hello"; -let nee = "ll"; -// 2 -console.log(strStr(h, nee)); \ No newline at end of file +// let haystack = "leetcode", needle = "leeto"; +// -1 +let haystack = "sadbutsad", needle = "sad"; +// 0 +console.log(strStr(haystack,needle)); \ No newline at end of file diff --git a/javascript/LeetCode/String/58.js b/javascript/LeetCode/String/58.js new file mode 100644 index 0000000..34f7940 --- /dev/null +++ b/javascript/LeetCode/String/58.js @@ -0,0 +1,34 @@ +/** + * 58. Length of Last Word + * + * 取得最後一個單字的長度,參數內會有空白格 + * + * @param {string} s + * @return {number} + */ +var lengthOfLastWord = function(s) { + // solution 1. + let temp = []; + let splitToArr = s.split(" "); + for(let i = 0;i < splitToArr.length;++i) { + if(splitToArr[i] !== ""){ + temp.push(splitToArr[i]); + } + } + return temp[temp.length - 1].length; + + // solution 2. +// let ans = 0; +// let trim = s.trim(); +// for(let i = trim.length - 1;i >= 0;i--){ +// if(trim[i] !== " "){ +// ans++; +// }else if(ans > 0){ +// break; +// } +// } +// return ans; +}; +let s = " fly me to the moon "; +// 4 +console.log(lengthOfLastWord(s)) \ No newline at end of file diff --git a/javascript/LeetCode/math/9.js b/javascript/LeetCode/math/9.js new file mode 100644 index 0000000..083284e --- /dev/null +++ b/javascript/LeetCode/math/9.js @@ -0,0 +1,17 @@ +/** + * 9. Palindrome Number + * + * 檢查x是否是迴文(倒著念也一樣) + * @param {number} x + * @return {boolean} + */ +var isPalindrome = function(x) { + // 數值不適合顛倒,轉字串 + let toStr = x.toString().split("").reverse(); + // console.log(toStr.join("")) + + return x.toString() === toStr.join(""); +}; +let x = -121; +// false +console.log(isPalindrome(x)) \ No newline at end of file From e30ca7903c5bac38c42ea8e220f01a6d7844255d Mon Sep 17 00:00:00 2001 From: Mavis Date: Tue, 25 Nov 2025 14:03:12 +0800 Subject: [PATCH 04/20] practice --- javascript/LeetCode/Array/169.js | 23 +++++++++++++++++++++++ javascript/index.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 javascript/LeetCode/Array/169.js diff --git a/javascript/LeetCode/Array/169.js b/javascript/LeetCode/Array/169.js new file mode 100644 index 0000000..69c8648 --- /dev/null +++ b/javascript/LeetCode/Array/169.js @@ -0,0 +1,23 @@ +/** + * 169. Majority Element + * + * 找出majority element = 出現次數大於 nums.length / 2 + * @param {number[]} nums + * @return {number} + */ +var majorityElement = function(nums) { + let map = new Map(); + let ans = 0; + for(const i of nums){ + map.has(i) ? map.set(i,map.get(i)+1) : map.set(i,1); + } + for(const [key,value] of map) { + if(value > (nums.length / 2)){ + ans = key; + } + } + return ans; +}; +let nums = [2,2,1,1,1,2,2]; +// 2 +console.log(majorityElement(nums)); \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index 2d5a115..d78da8b 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1086,3 +1086,32 @@ var findXSum = function(nums, k, x) { // For subarray [2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3. // console.log(findXSum(nums,k,x)); +/** + * 1015. Smallest Integer Divisible by K + * + * 找出最小能被k整除了數字,且該數字只有1位數 + * @param {number} k + * @return {number} + */ +var smallestRepunitDivByK = function(k) { + +}; +let k = 2; +// -1 +// console.log(smallestRepunitDivByK(k)); + +/** + * 69. Sqrt(x) + * + * @param {number} x + * @return {number} + */ +var mySqrt = function(x) { + // 不能使用內建的pow() or sqrt() + // rounded down to the nearest integer. + +}; +let x = 8; +// 2 +// console.log(mySqrt(x)); + From 4e7997d374b738282cc57acb68374eab8f01f639 Mon Sep 17 00:00:00 2001 From: Mavis Date: Wed, 26 Nov 2025 14:44:22 +0800 Subject: [PATCH 05/20] use JS and Python to practice --- javascript/LeetCode/String/168.js | 20 ++++++++++++++++++++ python/index.py | 16 +++++++++++++++- python/leetcode/math/168.py | 24 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 javascript/LeetCode/String/168.js create mode 100644 python/leetcode/math/168.py diff --git a/javascript/LeetCode/String/168.js b/javascript/LeetCode/String/168.js new file mode 100644 index 0000000..55ed841 --- /dev/null +++ b/javascript/LeetCode/String/168.js @@ -0,0 +1,20 @@ +/** + * 168. Excel Sheet Column Title + * + * 給定一個整數 columnNumber,傳回其在 Excel 表格中對應的列標題。 + * @param {number} columnNumber + * @return {string} + */ +var convertToTitle = function(columnNumber) { + let ans = ""; + let baseASCIICode = "A".charCodeAt(); + while(columnNumber > 0){ + let offset = (columnNumber - 1) % 26; + ans = String.fromCharCode(baseASCIICode + offset) + ans; + columnNumber = parseInt((parseInt(columnNumber - 1) / 26)); + } + return ans; +}; +let columnNumber = 701; +// "ZY"; +console.log(convertToTitle(columnNumber)); diff --git a/python/index.py b/python/index.py index f532269..183ecb1 100644 --- a/python/index.py +++ b/python/index.py @@ -265,4 +265,18 @@ def replaceDigits(s: str) -> str: return res s = "a1c1e1" # Output: "abcdef" -# print(replaceDigits(s)) \ No newline at end of file +# print(replaceDigits(s)) + + +def add(x:int): + return x + 3 + +def sub(a:int): + return a - 1 + +n = { + "add": add, + "sub": sub +} +print(n["add"](5)) +print(n["sub"](6)) \ No newline at end of file diff --git a/python/leetcode/math/168.py b/python/leetcode/math/168.py new file mode 100644 index 0000000..ced705a --- /dev/null +++ b/python/leetcode/math/168.py @@ -0,0 +1,24 @@ +def convertToTitle(columnNumber: int) -> str: + ''' + 168. Excel Sheet Column Title + + :param columnNumber: 給定一個整數 columnNumber,傳回其在 Excel 表格中對應的列標題。 + :type columnNumber: int + :return: 回傳對應的列 + :rtype: str + ''' + ans = "" + # ASCII + # ord('alphabet') => number + # chr(number) => alphabet + baseChar = ord('A') + while(columnNumber > 0): + offset = (columnNumber - 1) % 26 + # print(offset) + ans = str(chr(baseChar + offset)) + ans + + columnNumber = int((int(columnNumber - 1) / 26)) + return ans +columnNumber = 701 +# ZY +print(convertToTitle(columnNumber)) \ No newline at end of file From 83f19f11e98bba188cc3668d29d6ad298d8c6aec Mon Sep 17 00:00:00 2001 From: Mavis Date: Thu, 27 Nov 2025 14:43:35 +0800 Subject: [PATCH 06/20] learn python --- python/exercise.py | 6 +++++- python/index.py | 12 ------------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/python/exercise.py b/python/exercise.py index 78dd450..afe159f 100644 --- a/python/exercise.py +++ b/python/exercise.py @@ -237,4 +237,8 @@ def checkExist(arr:list,item:int): array = [1,2,3,4] item = 3 -checkExist(array,item) \ No newline at end of file +checkExist(array,item) + + +r = lambda a: "True" if a > 5 else "False" +print(r(1)) \ No newline at end of file diff --git a/python/index.py b/python/index.py index 183ecb1..d005e7f 100644 --- a/python/index.py +++ b/python/index.py @@ -268,15 +268,3 @@ def replaceDigits(s: str) -> str: # print(replaceDigits(s)) -def add(x:int): - return x + 3 - -def sub(a:int): - return a - 1 - -n = { - "add": add, - "sub": sub -} -print(n["add"](5)) -print(n["sub"](6)) \ No newline at end of file From e76e5c5d2832bd53471b59a8dd95eaeabfcacbff Mon Sep 17 00:00:00 2001 From: Mavis Date: Fri, 28 Nov 2025 13:44:15 +0800 Subject: [PATCH 07/20] practice in Python --- python/index.py | 3 +++ python/leetcode/list/136.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 python/leetcode/list/136.py diff --git a/python/index.py b/python/index.py index d005e7f..13b764b 100644 --- a/python/index.py +++ b/python/index.py @@ -268,3 +268,6 @@ def replaceDigits(s: str) -> str: # print(replaceDigits(s)) +a = [12,6,3] +b = map(lambda x: x * 2, a) +print(list(b)) \ No newline at end of file diff --git a/python/leetcode/list/136.py b/python/leetcode/list/136.py new file mode 100644 index 0000000..5f13412 --- /dev/null +++ b/python/leetcode/list/136.py @@ -0,0 +1,21 @@ +def singleNumber(nums: List[int]) -> int: + ''' + 136. Single Number + ''' + # 找出只出現一次的元素 + # ans = 0 + # for key,value in Counter(nums).items(): + # if(value == 1): + # ans = key + # return ans + + # solution 2 + ans = 0 + for i in nums: + ans ^= i + return ans + + +nums = [4,1,2,1,2] +# 4 +print(singleNumber(nums)) \ No newline at end of file From 27e845927f1265e633453e3b73e8e83787960e43 Mon Sep 17 00:00:00 2001 From: Mavis Date: Mon, 1 Dec 2025 15:17:12 +0800 Subject: [PATCH 08/20] add new solution --- javascript/LeetCode/Array/219.js | 12 ++++++++++++ javascript/index.js | 22 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/javascript/LeetCode/Array/219.js b/javascript/LeetCode/Array/219.js index f41e4a5..913180d 100644 --- a/javascript/LeetCode/Array/219.js +++ b/javascript/LeetCode/Array/219.js @@ -76,6 +76,18 @@ var containsNearbyDuplicate = function (nums, k) { toMap.set(nums[i], i); } return false; + + // solution 3. + // i & j => nums[i] == nums[j] & abs(i - j) <= k. + // nums[i]跟nums[j]一樣 + // let map = new Map(); + // for(let i = 0;i < nums.length;++i){ + // if(map.has(nums[i]) && i - map.get(nums[i]) <= k){ + // return true + // } + // map.set(nums[i],i); + // } + // return false; }; const nums = [1, 2, 3, 1], k = 3; // return true //Input: nums = [1,0,1,1], k = 1 return true diff --git a/javascript/index.js b/javascript/index.js index d78da8b..59206fd 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1096,7 +1096,7 @@ var findXSum = function(nums, k, x) { var smallestRepunitDivByK = function(k) { }; -let k = 2; +// let k = 2; // -1 // console.log(smallestRepunitDivByK(k)); @@ -1115,3 +1115,23 @@ let x = 8; // 2 // console.log(mySqrt(x)); +/** + * @param {number[]} nums + * @param {number} k + * @return {boolean} + */ +var containsNearbyDuplicate = function(nums, k) { + // i & j => nums[i] == nums[j] & abs(i - j) <= k. + // nums[i]跟nums[j]一樣 + let map = new Map(); + for(let i = 0;i < nums.length;++i){ + if(map.has(nums[i]) && i - map.get(nums[i]) <= k){ + return true + } + map.set(nums[i],i); + } + return false; +}; +let nums = [1,2,3,1,2,3], k = 2 + // false +console.log(containsNearbyDuplicate(nums,k)) \ No newline at end of file From e3e1ff65699afe0c3140453422c5fa389b500e65 Mon Sep 17 00:00:00 2001 From: Mavis Date: Tue, 2 Dec 2025 14:02:42 +0800 Subject: [PATCH 09/20] add 3731 in JS --- javascript/LeetCode/Array/3731.js | 20 ++++++++++++++++++++ javascript/index.js | 20 -------------------- 2 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 javascript/LeetCode/Array/3731.js diff --git a/javascript/LeetCode/Array/3731.js b/javascript/LeetCode/Array/3731.js new file mode 100644 index 0000000..3f1e742 --- /dev/null +++ b/javascript/LeetCode/Array/3731.js @@ -0,0 +1,20 @@ +/** + * 3731. Find Missing Elements + * + * 找出參數陣列中缺少的數字,並以陣列型態回傳 + * @param {number[]} nums + * @return {number[]} + */ +var findMissingElements = function(nums) { + let ans = []; + let min = Math.min(...nums),max = Math.max(...nums); + for(let i = min;i <= max;++i){ + if(nums.includes(i) === false){ + ans.push(i); + } + } + return ans; +}; +let nums = [5,1]; +// [2,3,4] +console.log(findMissingElements(nums)); \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index 59206fd..3cc6270 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1115,23 +1115,3 @@ let x = 8; // 2 // console.log(mySqrt(x)); -/** - * @param {number[]} nums - * @param {number} k - * @return {boolean} - */ -var containsNearbyDuplicate = function(nums, k) { - // i & j => nums[i] == nums[j] & abs(i - j) <= k. - // nums[i]跟nums[j]一樣 - let map = new Map(); - for(let i = 0;i < nums.length;++i){ - if(map.has(nums[i]) && i - map.get(nums[i]) <= k){ - return true - } - map.set(nums[i],i); - } - return false; -}; -let nums = [1,2,3,1,2,3], k = 2 - // false -console.log(containsNearbyDuplicate(nums,k)) \ No newline at end of file From 99a26a7e44e6a259a97b8dcb6047d9b2e679fd82 Mon Sep 17 00:00:00 2001 From: Mavis Date: Wed, 3 Dec 2025 14:48:08 +0800 Subject: [PATCH 10/20] learn python --- python/index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/index.py b/python/index.py index 13b764b..a5115f3 100644 --- a/python/index.py +++ b/python/index.py @@ -269,5 +269,5 @@ def replaceDigits(s: str) -> str: a = [12,6,3] -b = map(lambda x: x * 2, a) +b = map(str,a) print(list(b)) \ No newline at end of file From 20a300df530cf01e22c7cdc360de532374555daa Mon Sep 17 00:00:00 2001 From: Mavis Date: Fri, 5 Dec 2025 13:55:48 +0800 Subject: [PATCH 11/20] add JS solutions --- javascript/LeetCode/Array/1184.js | 33 ++++++++++++++++++++++++++ javascript/LeetCode/math/3432.js | 39 +++++++++++++++++++++---------- javascript/index.js | 1 - 3 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 javascript/LeetCode/Array/1184.js diff --git a/javascript/LeetCode/Array/1184.js b/javascript/LeetCode/Array/1184.js new file mode 100644 index 0000000..57f805f --- /dev/null +++ b/javascript/LeetCode/Array/1184.js @@ -0,0 +1,33 @@ +/** + * 1184. Distance Between Bus Stops + * + * 從圓中(distance)找出從start到destination最短的距離 + * + * @param {number[]} distance + * @param {number} start + * @param {number} destination + * @return {number} + */ +var distanceBetweenBusStops = function(distance, start, destination) { + let res = 0,total = distance.reduce((a,b)=>a + b); + if(start > destination){ + let temp = start; + start = destination; + destination = temp; + } + for(let i = 0;i < distance.length;++i) { + if(i >= start && i < destination){ + res += distance[i] + } + } + return Math.min(res,total - res); +}; +// [0,1,2,3] +// let distance = [1,2,3,4], start = 0, destination = 2; +// ans.3 => 2+1 +// 7 => 1+2+4 +// Distance between 0 and 2 is 3 or 7, minimum is 3. + +let distance = [7,10,1,12,11,14,5,0], start = 7, destination = 2; +// 17 +console.log(distanceBetweenBusStops(distance,start,destination)); \ No newline at end of file diff --git a/javascript/LeetCode/math/3432.js b/javascript/LeetCode/math/3432.js index 6040cb5..92333a4 100644 --- a/javascript/LeetCode/math/3432.js +++ b/javascript/LeetCode/math/3432.js @@ -44,22 +44,37 @@ */ var countPartitions = function (nums) { // 左右皆不斷更新 - let right = 0; - let left = 0; - // 加總後的值 - let sum = 0; - // 操作次數 + // let right = 0; + // let left = 0; + // // 加總後的值 + // let sum = 0; + // // 操作次數 + // let count = 0; + // for (let i = 0; i < nums.length; i++) { + // sum += nums[i]; + // } + // for (let i = 0; i < nums.length - 1; i++) { + // left += nums[i]; + // // 總值 - 左邊 = 右邊 + // right = sum - left; + + // // 檢查兩邊是否都是偶數 + // if ((left % 2) === (right % 2)) { + // count++; + // } + // } + // return count; + + // solution 2. + let sum = nums.reduce((a,b) => a + b); let count = 0; - for (let i = 0; i < nums.length; i++) { - sum += nums[i]; - } - for (let i = 0; i < nums.length - 1; i++) { + let right = 0, left = 0; + for(let i = 0;i < nums.length;++i) { + // 左邊的元素會越來越多,右邊則會變少 left += nums[i]; - // 總值 - 左邊 = 右邊 right = sum - left; - // 檢查兩邊是否都是偶數 - if ((left % 2) === (right % 2)) { + if(right % 2 === 0 && left % 2 === 0){ count++; } } diff --git a/javascript/index.js b/javascript/index.js index 3cc6270..e877587 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1114,4 +1114,3 @@ var mySqrt = function(x) { let x = 8; // 2 // console.log(mySqrt(x)); - From 1b36d6f58f15cbdf83e5607ac8d53254da598d5f Mon Sep 17 00:00:00 2001 From: Mavis Date: Mon, 8 Dec 2025 15:40:25 +0800 Subject: [PATCH 12/20] add practice --- python/index.py | 9 +++++---- python/leetcode/math/1925:.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 python/leetcode/math/1925:.py diff --git a/python/index.py b/python/index.py index a5115f3..fc53370 100644 --- a/python/index.py +++ b/python/index.py @@ -267,7 +267,8 @@ def replaceDigits(s: str) -> str: # Output: "abcdef" # print(replaceDigits(s)) - -a = [12,6,3] -b = map(str,a) -print(list(b)) \ No newline at end of file +def startswith(w): + return w.startswith("a") +li = ['apple','orange','pineapple','grape'] +res = filter(startswith,li) +print(list(res)) \ No newline at end of file diff --git a/python/leetcode/math/1925:.py b/python/leetcode/math/1925:.py new file mode 100644 index 0000000..f0cfe61 --- /dev/null +++ b/python/leetcode/math/1925:.py @@ -0,0 +1,30 @@ +from math import sqrt +class So: + ''' + So 的 Docstring + + ''' + def countTriples(self, n: int) -> int: + ''' + 1925. Count Square Sum Triples + + triple = a 二次方 + b 二次方 = c 二次方 + + :param self: 說明 + :param n: 數值n + :type n: int + :return: 說明 + :rtype: int + ''' + ans = 0 + for a in range(1,n + 1): + for b in range(1,n + 1): + pairs = int(sqrt(a**2 + b**2 + 1)) + if pairs <= n and pairs **2 == a**2 + b**2: + ans += 1 + return ans + +ans = So() +n = 5 +# 2 +print(ans.countTriples(n)) \ No newline at end of file From c8550fa77f0e0c4854898e8b688debf501cb2608 Mon Sep 17 00:00:00 2001 From: Mavis Date: Tue, 9 Dec 2025 15:08:52 +0800 Subject: [PATCH 13/20] practice,not done --- javascript/index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/javascript/index.js b/javascript/index.js index e877587..a9ca201 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1114,3 +1114,54 @@ var mySqrt = function(x) { let x = 8; // 2 // console.log(mySqrt(x)); + + + +/** + * 3583. Count Special Triplets + * + * special triplet = index i,j,k + * 0 <= i < j < k < nums.length + * nums[i] === nums[j] * 2 + * nums[k] === nums[j] * 2 + * return it modulo 10的9次方 + 7. + * + * @param {number[]} nums + * @return {number} + */ +var specialTriplets = function(nums) { + let ans = 0; + // j as the middle of the triplet. + // For each j, you only need: + // how many values equal to 2 * nums[j] appear before j + // how many appear after j + // Then the contribution from index j is just: + // leftCount * rightCount + // let j = Math.floor(nums[nums.length % 2 ]); + // // console.log(j) + // for(let i = 0;i < nums.length;++i) { + + // } + + let left = new Map(); + let right = new Map(); + for(let i = 0;i < nums.length;++i) { + + } +}; +let nums = [8,4,2,8,4]; +/** + * 2 + * + * There are exactly two special triplets: + * (i, j, k) = (0, 1, 3) + * nums[0] = 8, nums[1] = 4, nums[3] = 8 + * nums[0] = nums[1] * 2 = 4 * 2 = 8 + * nums[3] = nums[1] * 2 = 4 * 2 = 8 + * + * (i, j, k) = (1, 2, 4) + * nums[1] = 4, nums[2] = 2, nums[4] = 4 + * nums[1] = nums[2] * 2 = 2 * 2 = 4 + * nums[4] = nums[2] * 2 = 2 * 2 = 4 + */ +console.log(specialTriplets(nums)); \ No newline at end of file From 48503bba42be1e9dd9821195e9d60a79d0890572 Mon Sep 17 00:00:00 2001 From: Mavis Date: Wed, 10 Dec 2025 15:35:48 +0800 Subject: [PATCH 14/20] practice,not done --- javascript/index.js | 58 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/javascript/index.js b/javascript/index.js index a9ca201..6ad633f 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1111,7 +1111,7 @@ var mySqrt = function(x) { // rounded down to the nearest integer. }; -let x = 8; +// let x = 8; // 2 // console.log(mySqrt(x)); @@ -1143,11 +1143,20 @@ var specialTriplets = function(nums) { // } - let left = new Map(); - let right = new Map(); + /** + * j = middle index + * 在j之前,檢查nums[i] === nums[j] * 2 的有幾個 + * 在j之後,檢查nums[k] === nums[j] * 2 的有幾個 + */ + // let left = new Map(); + // let right = new Map(); + let j = Math.round(nums.length % 2); for(let i = 0;i < nums.length;++i) { - + if(nums[i] === nums[j] * 2 && i < j){ + ans++; + } } + console.log(ans) }; let nums = [8,4,2,8,4]; /** @@ -1164,4 +1173,43 @@ let nums = [8,4,2,8,4]; * nums[1] = nums[2] * 2 = 2 * 2 = 4 * nums[4] = nums[2] * 2 = 2 * 2 = 4 */ -console.log(specialTriplets(nums)); \ No newline at end of file +// console.log(specialTriplets(nums)); + + +/** + * 1779. Find Nearest Point That Has the Same X or Y Coordinate + * + * x & y = current location + * A point is valid if it shares the same x-coordinate or the same y-coordinate as your location. + * 沒有回傳-1 + * + * The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2). + * + * @param {number} x + * @param {number} y + * @param {number[][]} points + * @return {number} + */ +var nearestValidPoint = function(x, y, points) { + + // abs(x-validPoint[0]) or abs(y-validPoint[1]) . + let totalDistance = x + y; + let ans = []; + for(const p of points) { + let prev = parseInt(p[0]),next = parseInt(p[1]); + if(prev === x || next === y){ + let distance = Math.abs(x - prev) + Math.abs(y - next); + ans.push(distance); + } + + } + console.log(ans) +}; +let x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]; +// 2 +// Of all the points, only [3,1], [2,4] and [4,4] are valid. +// Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. +// [2,4] has the smallest index, so return 2. +// let x = 3, y = 4, points = [[3,4]]; +// 0 +console.log(nearestValidPoint(x,y,points)); \ No newline at end of file From e5dab3ebb806dd4eb372f8f0148b4ee28b286a64 Mon Sep 17 00:00:00 2001 From: Mavis Date: Thu, 11 Dec 2025 14:39:53 +0800 Subject: [PATCH 15/20] add 1779 in JS --- javascript/LeetCode/Array/1779.js | 43 ++++++++++++++++++++++ javascript/index.js | 37 ------------------- python/index.py | 23 +++++++++--- python/leetcode/math/{1925:.py => 1925.py} | 0 4 files changed, 61 insertions(+), 42 deletions(-) create mode 100644 javascript/LeetCode/Array/1779.js rename python/leetcode/math/{1925:.py => 1925.py} (100%) diff --git a/javascript/LeetCode/Array/1779.js b/javascript/LeetCode/Array/1779.js new file mode 100644 index 0000000..81541c7 --- /dev/null +++ b/javascript/LeetCode/Array/1779.js @@ -0,0 +1,43 @@ +/** + * 1779. Find Nearest Point That Has the Same X or Y Coordinate + * + * x & y = current location + * A point is valid if it shares the same x-coordinate or the same y-coordinate as your location. + * 傳回與目前位置曼哈頓距離最小的有效點的索引(從 0 開始索引)。 + * 如果存在多個有效點,則傳回索引最小的有效點。如果沒有有效點,則傳回 -1。 + * + * The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2). + * + * @param {number} x + * @param {number} y + * @param {number[][]} points + * @return {number} + */ +var nearestValidPoint = function(x, y, points) { + let ans = -1; + let smallest = Infinity; + for(let i = 0;i < points.length;++i) { + let prev = Math.abs(x - parseInt(points[i][0])); + let next = Math.abs(y - parseInt(points[i][1])); + if(prev * next === 0 && (prev + next) < smallest){ + smallest = prev + next; + ans = i; + } + + } + return ans; +}; +// let x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]; +// 2 +// Of all the points, only [3,1], [2,4] and [4,4] are valid. +// Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. +// [2,4] has the smallest index, so return 2. + +// let x = 3, y = 4, points = [[3,4]]; +// 0 +// let x = 3, y = 4, points = [[2,3]]; +// -1 + +let x = 5, y = 1, points = [[1,1],[6,2],[1,5],[3,1]]; +// 3 +console.log(nearestValidPoint(x,y,points)); \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index 6ad633f..f650f59 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1176,40 +1176,3 @@ let nums = [8,4,2,8,4]; // console.log(specialTriplets(nums)); -/** - * 1779. Find Nearest Point That Has the Same X or Y Coordinate - * - * x & y = current location - * A point is valid if it shares the same x-coordinate or the same y-coordinate as your location. - * 沒有回傳-1 - * - * The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2). - * - * @param {number} x - * @param {number} y - * @param {number[][]} points - * @return {number} - */ -var nearestValidPoint = function(x, y, points) { - - // abs(x-validPoint[0]) or abs(y-validPoint[1]) . - let totalDistance = x + y; - let ans = []; - for(const p of points) { - let prev = parseInt(p[0]),next = parseInt(p[1]); - if(prev === x || next === y){ - let distance = Math.abs(x - prev) + Math.abs(y - next); - ans.push(distance); - } - - } - console.log(ans) -}; -let x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]; -// 2 -// Of all the points, only [3,1], [2,4] and [4,4] are valid. -// Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. -// [2,4] has the smallest index, so return 2. -// let x = 3, y = 4, points = [[3,4]]; -// 0 -console.log(nearestValidPoint(x,y,points)); \ No newline at end of file diff --git a/python/index.py b/python/index.py index fc53370..57383b5 100644 --- a/python/index.py +++ b/python/index.py @@ -267,8 +267,21 @@ def replaceDigits(s: str) -> str: # Output: "abcdef" # print(replaceDigits(s)) -def startswith(w): - return w.startswith("a") -li = ['apple','orange','pineapple','grape'] -res = filter(startswith,li) -print(list(res)) \ No newline at end of file +class So: + def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: + ans = -1 + smallest = float('inf') + for i in range(0,len(points)): + prev = x - int(points[i][0]) + nextOne = y - int(points[1][0]) + if (prev * nextOne) == 0 and abs(prev + nextOne) < smallest: + smallest = abs(prev + nextOne) + ans = i + return ans + +c = So() +x = 3 +y = 4 +points = [[1,2],[3,1],[2,4],[2,3],[4,4]] +# 2 +print(c.nearestValidPoint(x,y,points)) diff --git a/python/leetcode/math/1925:.py b/python/leetcode/math/1925.py similarity index 100% rename from python/leetcode/math/1925:.py rename to python/leetcode/math/1925.py From e30d37f0740b1362c088991672c3b6ab98d0ccbe Mon Sep 17 00:00:00 2001 From: Mavis Date: Fri, 12 Dec 2025 13:55:05 +0800 Subject: [PATCH 16/20] learn python decorator --- python/index.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/python/index.py b/python/index.py index 57383b5..850a37c 100644 --- a/python/index.py +++ b/python/index.py @@ -284,4 +284,17 @@ def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: y = 4 points = [[1,2],[3,1],[2,4],[2,3],[4,4]] # 2 -print(c.nearestValidPoint(x,y,points)) +# print(c.nearestValidPoint(x,y,points)) + +def showResult(func): + def wrap(): + print("run this one first") + func() + print("run this at the end") + return wrap + +@showResult +def say(): + print("then run this function") + +say() From 729401dac08d8d18e8685052556aaf1b1766898d Mon Sep 17 00:00:00 2001 From: Mavis Date: Wed, 17 Dec 2025 14:06:47 +0800 Subject: [PATCH 17/20] practice to solve 3573 --- javascript/index.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/javascript/index.js b/javascript/index.js index f650f59..c1d8afc 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1176,3 +1176,40 @@ let nums = [8,4,2,8,4]; // console.log(specialTriplets(nums)); +/** + * 3573. Best Time to Buy and Sell Stock V + * + * 正常交易:i天買,j天賣(i < j),利潤:proces[j] - prices[i] + * 炒短線:i天賣,j天買回來(i < j),利潤:prices[i] - prices[j] + * 不能同一天買賣單張股票 + * 回傳在交易k次後最多能賺多少 + * + * @param {number[]} prices 第i天的股價 + * @param {number} k 最多只能交易k次 + * @return {number} + */ +var maximumProfit = function(prices, k) { + const firstPrice = prices[0]; + const dp = Array(k + 1).fill(null).map(() => ({ + maxProfit: 0, + buyHold: -firstPrice, + sellHold: firstPrice + })); + for(let i = 0;i < prices.length;++i) { + const current = prices[i]; + for(let j = k;j > 0;--j) { + const prevProfit = dp[j - 1].maxProfit; + dp[j].maxProfit = Math.max(dp[j].maxProfit, dp[j].buyHold + current, dp[j].sellHold - current); + dp[j].buyHold = Math.max(dp[j].buyHold, prevProfit - current); + dp[j].sellHold = Math.max(dp[j].sellHold, prevProfit + current); + } + } + return dp[k].maxProfit; +}; +let prices = [1,7,9,8,2], k = 2; +// 14 +// can make $14 of profit through 2 transactions: +// A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9. +// A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2. +// 9 - 1 = 8 ; 8 - 2 = 6; 8 + 6 = 14 +console.log(maximumProfit(prices,k)) \ No newline at end of file From df93db33e082f58d5ab8d2c4d3d1d42d21821f33 Mon Sep 17 00:00:00 2001 From: Mavis Date: Thu, 18 Dec 2025 14:33:34 +0800 Subject: [PATCH 18/20] add 3573 & 1859 --- javascript/LeetCode/Array/3573.js | 37 ++++++++++++++++++++++++++++ javascript/LeetCode/String/1859.js | 28 +++++++++++++++++++++ javascript/index.js | 39 ++---------------------------- 3 files changed, 67 insertions(+), 37 deletions(-) create mode 100644 javascript/LeetCode/Array/3573.js create mode 100644 javascript/LeetCode/String/1859.js diff --git a/javascript/LeetCode/Array/3573.js b/javascript/LeetCode/Array/3573.js new file mode 100644 index 0000000..68fcefe --- /dev/null +++ b/javascript/LeetCode/Array/3573.js @@ -0,0 +1,37 @@ +/** + * 3573. Best Time to Buy and Sell Stock V + * + * 正常交易:i天買,j天賣(i < j),利潤:proces[j] - prices[i] + * 炒短線:i天賣,j天買回來(i < j),利潤:prices[i] - prices[j] + * 不能同一天買賣單張股票 + * 回傳在交易k次後最多能賺多少 + * + * @param {number[]} prices 第i天的股價 + * @param {number} k 最多只能交易k次 + * @return {number} + */ +var maximumProfit = function(prices, k) { + const firstPrice = prices[0]; + const dp = Array(k + 1).fill(null).map(() => ({ + maxProfit: 0, + buyHold: -firstPrice, + sellHold: firstPrice + })); + for(let i = 0;i < prices.length;++i) { + const current = prices[i]; + for(let j = k;j > 0;--j) { + const prevProfit = dp[j - 1].maxProfit; + dp[j].maxProfit = Math.max(dp[j].maxProfit, dp[j].buyHold + current, dp[j].sellHold - current); + dp[j].buyHold = Math.max(dp[j].buyHold, prevProfit - current); + dp[j].sellHold = Math.max(dp[j].sellHold, prevProfit + current); + } + } + return dp[k].maxProfit; +}; +let prices = [1,7,9,8,2], k = 2; +// 14 +// can make $14 of profit through 2 transactions: +// A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9. +// A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2. +// 9 - 1 = 8 ; 8 - 2 = 6; 8 + 6 = 14 +// console.log(maximumProfit(prices,k)) diff --git a/javascript/LeetCode/String/1859.js b/javascript/LeetCode/String/1859.js new file mode 100644 index 0000000..d9d5956 --- /dev/null +++ b/javascript/LeetCode/String/1859.js @@ -0,0 +1,28 @@ +/** + * 1859. Sorting the Sentence + * + * 字串句子中每個單字最後面都會有個數字,利用該數字重新將句子做排序,但結果每個單字不能有數字。 + * + * @param {string} s + * @return {string} + */ +var sortSentence = function(s) { + let res = ""; + let splitS = s.split(" "); + let map = new Map(); + for(let i = 0;i < splitS.length;++i){ + // 取得每個字的最後一個數字,並把它放入new map => key = int,value = str + const lastInt = parseInt(splitS[i].substring(splitS[i].length - 1)); + map.set(lastInt,splitS[i].substring(0,splitS[i].length - 1)); + } + // sort map + var mapAsc = new Map([...map.entries()].sort()); + for(const [key,value] of mapAsc) { + res += " "+value; + } + return res.trim() +}; +let s = "is2 sentence4 This1 a3"; +// Output: "This is a sentence" +// Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers. +console.log(sortSentence(s)); \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index c1d8afc..aff51ae 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1176,40 +1176,5 @@ let nums = [8,4,2,8,4]; // console.log(specialTriplets(nums)); -/** - * 3573. Best Time to Buy and Sell Stock V - * - * 正常交易:i天買,j天賣(i < j),利潤:proces[j] - prices[i] - * 炒短線:i天賣,j天買回來(i < j),利潤:prices[i] - prices[j] - * 不能同一天買賣單張股票 - * 回傳在交易k次後最多能賺多少 - * - * @param {number[]} prices 第i天的股價 - * @param {number} k 最多只能交易k次 - * @return {number} - */ -var maximumProfit = function(prices, k) { - const firstPrice = prices[0]; - const dp = Array(k + 1).fill(null).map(() => ({ - maxProfit: 0, - buyHold: -firstPrice, - sellHold: firstPrice - })); - for(let i = 0;i < prices.length;++i) { - const current = prices[i]; - for(let j = k;j > 0;--j) { - const prevProfit = dp[j - 1].maxProfit; - dp[j].maxProfit = Math.max(dp[j].maxProfit, dp[j].buyHold + current, dp[j].sellHold - current); - dp[j].buyHold = Math.max(dp[j].buyHold, prevProfit - current); - dp[j].sellHold = Math.max(dp[j].sellHold, prevProfit + current); - } - } - return dp[k].maxProfit; -}; -let prices = [1,7,9,8,2], k = 2; -// 14 -// can make $14 of profit through 2 transactions: -// A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9. -// A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2. -// 9 - 1 = 8 ; 8 - 2 = 6; 8 + 6 = 14 -console.log(maximumProfit(prices,k)) \ No newline at end of file + + From c77af33b8adb121b9fdb24bcee118c871fe195d3 Mon Sep 17 00:00:00 2001 From: Mavis Date: Fri, 19 Dec 2025 14:23:32 +0800 Subject: [PATCH 19/20] add new solution --- javascript/LeetCode/Array/3151.js | 27 +++++++++++++++++++++++++++ javascript/index.js | 30 ++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 javascript/LeetCode/Array/3151.js diff --git a/javascript/LeetCode/Array/3151.js b/javascript/LeetCode/Array/3151.js new file mode 100644 index 0000000..aed5cc4 --- /dev/null +++ b/javascript/LeetCode/Array/3151.js @@ -0,0 +1,27 @@ +/** + * 3151. Special Array I + * + * 檢查陣列任兩個相鄰元素的奇偶性是否都不同。若是回傳true + * @param {number[]} nums + * @return {boolean} + */ +var isArraySpecial = function(nums) { + if(nums.length === 1){ + return true; + } + for(let i = 0;i < nums.length;++i) { + let previous = nums[i]; + let next = nums[i + 1]; + if(previous % 2 !== next % 2){ + continue; + }else{ + return false; + } + } + return true; +}; +// let nums = [4,3,1,6]; +// false +let nums = [2,1]; +// true +console.log(isArraySpecial(nums)) \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index aff51ae..0521741 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1158,7 +1158,7 @@ var specialTriplets = function(nums) { } console.log(ans) }; -let nums = [8,4,2,8,4]; +// let nums = [8,4,2,8,4]; /** * 2 * @@ -1175,6 +1175,28 @@ let nums = [8,4,2,8,4]; */ // console.log(specialTriplets(nums)); - - - +/** + * 2119. A Number After a Double Reversal + * + * 將參數nums反轉2次後,檢查是否還是跟原參數一樣 + * @param {number} num + * @return {boolean} + */ +var isSameAfterReversals = function(num) { + // 可能遇到的狀況:數字反轉後,前面是0 + let countReverseTimes = 0; + // let nStr = 0; + let copyNum = num; + let toStr = num.toString().split(""); + for(let i = 0;i < toStr.length;++i) { + if(countReverseTimes !== 2){ + toStr = parseInt(toStr.reverse().join("")); + countReverseTimes++; + } + } + console.log(toStr) + return toStr === copyNum; +}; +let num = 526; +// false +console.log(isSameAfterReversals(num)); \ No newline at end of file From 68551d6ce31fa960a2e460499c4803c5ed70f886 Mon Sep 17 00:00:00 2001 From: Mavis Date: Mon, 22 Dec 2025 14:23:51 +0800 Subject: [PATCH 20/20] add 2 problems --- javascript/LeetCode/math/2119.js | 24 +++++++++++++++++++ javascript/LeetCode/math/69.js | 17 +++++++++++++ javascript/index.js | 41 -------------------------------- 3 files changed, 41 insertions(+), 41 deletions(-) create mode 100644 javascript/LeetCode/math/2119.js create mode 100644 javascript/LeetCode/math/69.js diff --git a/javascript/LeetCode/math/2119.js b/javascript/LeetCode/math/2119.js new file mode 100644 index 0000000..e610fed --- /dev/null +++ b/javascript/LeetCode/math/2119.js @@ -0,0 +1,24 @@ +/** + * 2119. A Number After a Double Reversal + * + * 將參數nums反轉2次後,檢查是否還是跟原參數一樣 + * @param {number} num + * @return {boolean} + */ +var isSameAfterReversals = function(num) { + // 可能遇到的狀況:數字反轉後,前面是0 + let countReverseTimes = 0; + let copyNum = num; + + while(countReverseTimes !== 2){ + let reverse = copyNum.toString().split("").reverse().join(""); + copyNum = parseInt(reverse); + countReverseTimes++; + } + return copyNum === num; +}; +let num = 526; +// true +// let num = 1800; +// false +console.log(isSameAfterReversals(num)); \ No newline at end of file diff --git a/javascript/LeetCode/math/69.js b/javascript/LeetCode/math/69.js new file mode 100644 index 0000000..1f01843 --- /dev/null +++ b/javascript/LeetCode/math/69.js @@ -0,0 +1,17 @@ +/** + * 69. Sqrt(x) + * + * @param {number} x + * @return {number} + */ +var mySqrt = function(x) { + // 不能使用內建的pow() or sqrt() + // rounded down to the nearest integer. + if (x < 0) { + return; + } + return Math.floor(x ** 0.5); +}; +let x = 8; +// 2 +console.log(mySqrt(x)); \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index 0521741..b9f992c 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1100,22 +1100,6 @@ var smallestRepunitDivByK = function(k) { // -1 // console.log(smallestRepunitDivByK(k)); -/** - * 69. Sqrt(x) - * - * @param {number} x - * @return {number} - */ -var mySqrt = function(x) { - // 不能使用內建的pow() or sqrt() - // rounded down to the nearest integer. - -}; -// let x = 8; -// 2 -// console.log(mySqrt(x)); - - /** * 3583. Count Special Triplets @@ -1175,28 +1159,3 @@ var specialTriplets = function(nums) { */ // console.log(specialTriplets(nums)); -/** - * 2119. A Number After a Double Reversal - * - * 將參數nums反轉2次後,檢查是否還是跟原參數一樣 - * @param {number} num - * @return {boolean} - */ -var isSameAfterReversals = function(num) { - // 可能遇到的狀況:數字反轉後,前面是0 - let countReverseTimes = 0; - // let nStr = 0; - let copyNum = num; - let toStr = num.toString().split(""); - for(let i = 0;i < toStr.length;++i) { - if(countReverseTimes !== 2){ - toStr = parseInt(toStr.reverse().join("")); - countReverseTimes++; - } - } - console.log(toStr) - return toStr === copyNum; -}; -let num = 526; -// false -console.log(isSameAfterReversals(num)); \ No newline at end of file