diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index c0890f75..94614242 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -154,6 +154,23 @@ var canJump = function(nums) { }; ``` +### TypeScript + +```typescript +function canJump(nums: number[]): boolean { + let farthestIndex: number = 0; + let cur: number = 0; + while (cur <= farthestIndex) { + farthestIndex = Math.max(farthestIndex, cur + nums[cur]); + if (farthestIndex >= nums.length - 1) return true; + cur++; + } + return false; +}; +``` + + + -----------------------
diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index b31cbae3..4f585a3c 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -268,6 +268,18 @@ const maxProfit = (prices) => { }; ``` +TypeScript: + +```typescript +function maxProfit(prices: number[]): number { + let resProfit: number = 0; + for (let i = 1, length = prices.length; i < length; i++) { + resProfit += Math.max(prices[i] - prices[i - 1], 0); + } + return resProfit; +}; +``` + C: ```c