forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luzhipeng
committed
Apr 7, 2019
1 parent
ab37a5a
commit d61d358
Showing
13 changed files
with
606 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* @lc app=leetcode id=139 lang=javascript | ||
* | ||
* [139] Word Break | ||
* | ||
* https://leetcode.com/problems/word-break/description/ | ||
* | ||
* algorithms | ||
* Medium (34.45%) | ||
* Total Accepted: 317.8K | ||
* Total Submissions: 913.9K | ||
* Testcase Example: '"leetcode"\n["leet","code"]' | ||
* | ||
* Given a non-empty string s and a dictionary wordDict containing a list of | ||
* non-empty words, determine if s can be segmented into a space-separated | ||
* sequence of one or more dictionary words. | ||
* | ||
* Note: | ||
* | ||
* | ||
* The same word in the dictionary may be reused multiple times in the | ||
* segmentation. | ||
* You may assume the dictionary does not contain duplicate words. | ||
* | ||
* | ||
* Example 1: | ||
* | ||
* | ||
* Input: s = "leetcode", wordDict = ["leet", "code"] | ||
* Output: true | ||
* Explanation: Return true because "leetcode" can be segmented as "leet | ||
* code". | ||
* | ||
* | ||
* Example 2: | ||
* | ||
* | ||
* Input: s = "applepenapple", wordDict = ["apple", "pen"] | ||
* Output: true | ||
* Explanation: Return true because "applepenapple" can be segmented as "apple | ||
* pen apple". | ||
* Note that you are allowed to reuse a dictionary word. | ||
* | ||
* | ||
* Example 3: | ||
* | ||
* | ||
* Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] | ||
* Output: false | ||
* | ||
* | ||
*/ | ||
/** | ||
* @param {string} s | ||
* @param {string[]} wordDict | ||
* @return {boolean} | ||
*/ | ||
var wordBreak = function(s, wordDict) { | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* @lc app=leetcode id=226 lang=javascript | ||
* | ||
* [226] Invert Binary Tree | ||
* | ||
* https://leetcode.com/problems/invert-binary-tree/description/ | ||
* | ||
* algorithms | ||
* Easy (57.14%) | ||
* Total Accepted: 311K | ||
* Total Submissions: 540.6K | ||
* Testcase Example: '[4,2,7,1,3,6,9]' | ||
* | ||
* Invert a binary tree. | ||
* | ||
* Example: | ||
* | ||
* Input: | ||
* | ||
* | ||
* 4 | ||
* / \ | ||
* 2 7 | ||
* / \ / \ | ||
* 1 3 6 9 | ||
* | ||
* Output: | ||
* | ||
* | ||
* 4 | ||
* / \ | ||
* 7 2 | ||
* / \ / \ | ||
* 9 6 3 1 | ||
* | ||
* Trivia: | ||
* This problem was inspired by this original tweet by Max Howell: | ||
* | ||
* Google: 90% of our engineers use the software you wrote (Homebrew), but you | ||
* can’t invert a binary tree on a whiteboard so f*** off. | ||
* | ||
*/ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {TreeNode} | ||
*/ | ||
var invertTree = function(root) { | ||
if (!root) return root; | ||
// 递归 | ||
// const left = root.left; | ||
// const right = root.right; | ||
// root.right = invertTree(left); | ||
// root.left = invertTree(right); | ||
// 我们用stack来模拟递归 | ||
// 本质上递归是利用了执行栈,执行栈也是一种栈 | ||
// 其实这里使用队列也是一样的,因为这里顺序不重要 | ||
|
||
const stack = [root]; | ||
let current = null; | ||
while ((current = stack.shift())) { | ||
const left = current.left; | ||
const right = current.right; | ||
current.right = left; | ||
current.left = right; | ||
if (left) { | ||
stack.push(left); | ||
} | ||
if (right) { | ||
stack.push(right); | ||
} | ||
} | ||
return root; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* @lc app=leetcode id=416 lang=javascript | ||
* | ||
* [416] Partition Equal Subset Sum | ||
* | ||
* https://leetcode.com/problems/partition-equal-subset-sum/description/ | ||
* | ||
* algorithms | ||
* Medium (39.97%) | ||
* Total Accepted: 79.7K | ||
* Total Submissions: 198.5K | ||
* Testcase Example: '[1,5,11,5]' | ||
* | ||
* Given a non-empty array containing only positive integers, find if the array | ||
* can be partitioned into two subsets such that the sum of elements in both | ||
* subsets is equal. | ||
* | ||
* Note: | ||
* | ||
* | ||
* Each of the array element will not exceed 100. | ||
* The array size will not exceed 200. | ||
* | ||
* | ||
* | ||
* | ||
* Example 1: | ||
* | ||
* | ||
* Input: [1, 5, 11, 5] | ||
* | ||
* Output: true | ||
* | ||
* Explanation: The array can be partitioned as [1, 5, 5] and [11]. | ||
* | ||
* | ||
* | ||
* | ||
* Example 2: | ||
* | ||
* | ||
* Input: [1, 2, 3, 5] | ||
* | ||
* Output: false | ||
* | ||
* Explanation: The array cannot be partitioned into equal sum subsets. | ||
* | ||
* | ||
* | ||
* | ||
*/ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var canPartition = function(nums) { | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* @lc app=leetcode id=494 lang=javascript | ||
* | ||
* [494] Target Sum | ||
* | ||
* https://leetcode.com/problems/target-sum/description/ | ||
* | ||
* algorithms | ||
* Medium (44.86%) | ||
* Total Accepted: 89.3K | ||
* Total Submissions: 198.5K | ||
* Testcase Example: '[1,1,1,1,1]\n3' | ||
* | ||
* | ||
* You are given a list of non-negative integers, a1, a2, ..., an, and a | ||
* target, S. Now you have 2 symbols + and -. For each integer, you should | ||
* choose one from + and - as its new symbol. | ||
* | ||
* | ||
* Find out how many ways to assign symbols to make sum of integers equal to | ||
* target S. | ||
* | ||
* | ||
* Example 1: | ||
* | ||
* Input: nums is [1, 1, 1, 1, 1], S is 3. | ||
* Output: 5 | ||
* Explanation: | ||
* | ||
* -1+1+1+1+1 = 3 | ||
* +1-1+1+1+1 = 3 | ||
* +1+1-1+1+1 = 3 | ||
* +1+1+1-1+1 = 3 | ||
* +1+1+1+1-1 = 3 | ||
* | ||
* There are 5 ways to assign symbols to make the sum of nums be target 3. | ||
* | ||
* | ||
* | ||
* Note: | ||
* | ||
* The length of the given array is positive and will not exceed 20. | ||
* The sum of elements in the given array will not exceed 1000. | ||
* Your output answer is guaranteed to be fitted in a 32-bit integer. | ||
* | ||
* | ||
*/ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} S | ||
* @return {number} | ||
*/ | ||
var findTargetSumWays = function(nums, S) { | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* @lc app=leetcode id=518 lang=javascript | ||
* | ||
* [518] Coin Change 2 | ||
* | ||
* https://leetcode.com/problems/coin-change-2/description/ | ||
* | ||
* algorithms | ||
* Medium (41.57%) | ||
* Total Accepted: 39.7K | ||
* Total Submissions: 94.6K | ||
* Testcase Example: '5\n[1,2,5]' | ||
* | ||
* You are given coins of different denominations and a total amount of money. | ||
* Write a function to compute the number of combinations that make up that | ||
* amount. You may assume that you have infinite number of each kind of | ||
* coin. | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* Example 1: | ||
* | ||
* | ||
* Input: amount = 5, coins = [1, 2, 5] | ||
* Output: 4 | ||
* Explanation: there are four ways to make up the amount: | ||
* 5=5 | ||
* 5=2+2+1 | ||
* 5=2+1+1+1 | ||
* 5=1+1+1+1+1 | ||
* | ||
* | ||
* Example 2: | ||
* | ||
* | ||
* Input: amount = 3, coins = [2] | ||
* Output: 0 | ||
* Explanation: the amount of 3 cannot be made up just with coins of 2. | ||
* | ||
* | ||
* Example 3: | ||
* | ||
* | ||
* Input: amount = 10, coins = [10] | ||
* Output: 1 | ||
* | ||
* | ||
* | ||
* | ||
* Note: | ||
* | ||
* You can assume that | ||
* | ||
* | ||
* 0 <= amount <= 5000 | ||
* 1 <= coin <= 5000 | ||
* the number of coins is less than 500 | ||
* the answer is guaranteed to fit into signed 32-bit integer | ||
* | ||
* | ||
*/ | ||
/** | ||
* @param {number} amount | ||
* @param {number[]} coins | ||
* @return {number} | ||
*/ | ||
var change = function(amount, coins) { | ||
|
||
}; | ||
|
Oops, something went wrong.