Here are the solutions for the remaining problems on the list:
Reverse Linked List
Approach: Iteratively reverse the pointers of the linked list nodes.
codecakes’s gists
This file contains hidden or 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
| Hi = (name) => { | |
| x = Object.create(Hi.prototype); | |
| x.name = name | |
| return x; | |
| } | |
| Hi.prototype = { | |
| name: null, | |
| greet: function () { | |
| return `Hello ${this.name}`; |
This file contains hidden or 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
| """ | |
| Input | |
| s = | |
| "pineapplepenapple" | |
| wordDict = | |
| ["apple","pen","applepen","pine","pineapple"] | |
| """ | |
| # This works |
Here are the solutions for the remaining problems on the list:
Approach: Iteratively reverse the pointers of the linked list nodes.
This file contains hidden or 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
| # top-down: Larger Number TLE | |
| class Solution: | |
| def minimumTotal(self, triangle: List[List[int]]) -> int: | |
| if len(triangle) == 1: | |
| return triangle[0][0] | |
| res = tuple() | |
| level = 0 | |
| curr_list = triangle[level] | |
| next_level = level + 1 |
This file contains hidden or 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
| # This is one way | |
| class Solution: | |
| def fourSum(self, nums: List[int], target: int) -> List[List[int]]: | |
| nums.sort() | |
| result = [] | |
| for idx, num in enumerate(nums): | |
| for res3list in self.threeSum(nums[idx+1:], target-num): | |
| if [num] + res3list not in result: | |
| result += [[num] + res3list] | |
| return result |
This file contains hidden or 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
| # naive solution | |
| class Solution: | |
| POS_NUM = 1000 | |
| def removeDuplicates(self, nums: List[int]) -> int: | |
| start = 0 | |
| n = len(nums) | |
| end = n - 1 | |
| for start in iter(lambda: start, end): | |
| if start > end: | |
| break |
This file contains hidden or 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
| NATO_BAG = { | |
| 'A': 'Alpha', | |
| 'B': 'Bravo', | |
| 'C': 'Charlie', | |
| 'D': 'Delta', | |
| 'E': 'Echo', | |
| 'F': 'Foxtrot', | |
| 'G': 'Golf', | |
| 'H': 'Hotel', | |
| 'I': 'India', |
This file contains hidden or 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
| # Definition for a binary tree node. | |
| # | |
| # defmodule TreeNode do | |
| # @type t :: %__MODULE__{ | |
| # val: integer, | |
| # left: TreeNode.t() | nil, | |
| # right: TreeNode.t() | nil | |
| # } | |
| # defstruct val: 0, left: nil, right: nil | |
| # end |
This file contains hidden or 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
| class Solution: | |
| def subsets(self, nums: List[int]) -> List[List[int]]: | |
| def do_generate(result, slate, subset): | |
| result += [slate] | |
| for idx, num in enumerate(subset): | |
| new_subset = subset[idx+1:] | |
| do_generate(result, slate + [num], new_subset) | |
| return result | |
| return do_generate([], [], nums) | |
This file contains hidden or 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
| def find_combinations(n, k): | |
| """ | |
| Args: | |
| n(int32) | |
| k(int32) | |
| Returns: | |
| list_list_int32 | |
| """ | |
| array = list(range(1,n+1)) | |
| result = set() |