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
| func uniquePaths(m int, n int) int { | |
| // we are going to memorize all previous steps | |
| moves := make([][]int, n) | |
| for i, _ := range moves { | |
| moves[i] = make([]int, m) | |
| } | |
| // we are already located at the top-left corner. | |
| // There is only one way to be at this cell. |
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
| // https://leetcode.com/problems/network-delay-time | |
| const inf = (1 << 31) - 1 | |
| type Edge struct { | |
| vertex int | |
| time int | |
| } | |
| func networkDelayTime(times [][]int, N int, K int) int { |
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
| // https://leetcode.com/problems/linked-list-cycle/ | |
| /** | |
| * Definition for singly-linked list. | |
| * type ListNode struct { | |
| * Val int | |
| * Next *ListNode | |
| * } | |
| */ | |
| func hasCycle(head *ListNode) bool { | |
| hare := head |
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
| // https://leetcode.com/problems/binary-tree-paths | |
| /** | |
| * Definition for a binary tree node. | |
| * type TreeNode struct { | |
| * Val int | |
| * Left *TreeNode | |
| * Right *TreeNode | |
| * } | |
| */ |
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
| // https://leetcode.com/problems/reconstruct-itinerary | |
| // MUC -> [LHR] | |
| // JFK -> [MUC] | |
| // SFO -> [SJC] | |
| // LHR -> [SFO] | |
| // | |
| // JFK -> [SFO, ATL] | |
| // SFO -> [ATL] | |
| // ATL -> [JFK,SFO] |
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
| // https://leetcode.com/problems/reverse-words-in-a-string | |
| func reverseWords(s string) string { | |
| words := "" | |
| chars := []rune(s) | |
| anchor := 0 | |
| for i := 0; i < len(chars); i++ { | |
| if chars[i] == rune(' ') { | |
| if anchor < i { |
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
| // https://leetcode.com/problems/symmetric-tree/ | |
| /** | |
| * Definition for a binary tree node. | |
| * type TreeNode struct { | |
| * Val int | |
| * Left *TreeNode | |
| * Right *TreeNode | |
| * } | |
| */ |
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
| // https://leetcode.com/problems/intersection-of-two-arrays-ii | |
| import "sort" | |
| func intersect(nums1 []int, nums2 []int) []int { | |
| sort.Ints(nums1) | |
| sort.Ints(nums2) | |
| if len(nums1) > len(nums2) { | |
| nums1, nums2 = nums2, nums1 |
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
| // https://leetcode.com/problems/insert-delete-getrandom-o1 | |
| import "math/rand" | |
| type RandomizedSet struct { | |
| table map[int]int | |
| dict []int | |
| } | |
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
| // https://leetcode.com/problems/implement-queue-using-stacks | |
| // -> [][][][] -> | |
| // <-> [][][][] | |
| // <-> [][][][] | |
| type MyQueue struct { | |
| front int | |
| ordered []int | |
| reversed []int | |
| } |