diff --git a/jungmyunggi/Maximum Average Subarray i.js b/jungmyunggi/Maximum Average Subarray i.js new file mode 100644 index 0000000..52b0967 --- /dev/null +++ b/jungmyunggi/Maximum Average Subarray i.js @@ -0,0 +1,16 @@ +function findMaxAverage(nums, k) { + let max = -Infinity; + let temp = 0; + + for (let i = 0; i < k; i++) { + temp += nums[i]; + } + max = temp; + + for (let i = k; i < nums.length; i++) { + temp = temp - nums[i-k] + nums[i]; + max = Math.max(max,temp) + } + + return max/k; +} \ No newline at end of file diff --git a/jungmyunggi/Path Sum.js b/jungmyunggi/Path Sum.js new file mode 100644 index 0000000..3af5132 --- /dev/null +++ b/jungmyunggi/Path Sum.js @@ -0,0 +1,21 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} targetSum + * @return {boolean} + */ +var hasPathSum = function(root, targetSum) { + if(!root) return false; + if(!root.left && !root.right){ + return root.val === targetSum; + } + return hasPathSum(root.left, targetSum-root.val)||hasPathSum(root.right, targetSum-root.val); + +}; \ No newline at end of file diff --git a/jungmyunggi/Restore IP Addresses.js b/jungmyunggi/Restore IP Addresses.js new file mode 100644 index 0000000..a344994 --- /dev/null +++ b/jungmyunggi/Restore IP Addresses.js @@ -0,0 +1,37 @@ +/** + * @param {string} s + * @return {string[]} + */ +var restoreIpAddresses = function(s) { + const n = s.length; + if(n >=13){ + return []; + } + if(n <= 3){ + return []; + } + const result = []; + function isVaild(splitString){ + if(Number(splitString) === 0 && splitString.length === 1) return true; + if(splitString[0] === '0') return false; + if(Number(splitString) <= 255) return true; + return false; +} + + for(let i1 = 1; i1 < n - 2; i1++){ + const part1 = s.slice(0,i1); + if(isVaild(part1) === false) continue; + for(let i2 = i1 + 1; i2 < n - 1; i2++){ + const part2 = s.slice(i1,i2); + if(isVaild(part2) === false) continue; + for(let i3 = i2 + 1; i3 < n; i3++){ + const part3 = s.slice(i2,i3); + if(isVaild(part3) === false) continue; + const part4 = s.slice(i3); + if(isVaild(part4) === false) continue; + result.push(part1+"."+part2+"."+part3+"."+part4); + } + } + } + return result; +};