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/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/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/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 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/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/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/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/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/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/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/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 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/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/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/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/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 diff --git a/javascript/index.js b/javascript/index.js index 2d5a115..b9f992c 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1086,3 +1086,76 @@ 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)); + + +/** + * 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) { + + // } + + /** + * 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]; +/** + * 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)); + 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 f532269..850a37c 100644 --- a/python/index.py +++ b/python/index.py @@ -265,4 +265,36 @@ def replaceDigits(s: str) -> str: return res s = "a1c1e1" # Output: "abcdef" -# print(replaceDigits(s)) \ No newline at end of file +# print(replaceDigits(s)) + +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)) + +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() 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 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 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