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. | |
| * type TreeNode struct { | |
| * Val int | |
| * Left *TreeNode | |
| * Right *TreeNode | |
| * } | |
| */ | |
| func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode { | |
| if root == nil || p == nil { |
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. | |
| * type TreeNode struct { | |
| * Val int | |
| * Left *TreeNode | |
| * Right *TreeNode | |
| * } | |
| */ | |
| type LevelNode struct { |
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
| const undefined = (1 << 31) - 1 | |
| func majorityElement(nums []int) int { | |
| candidate := undefined | |
| count := 0 | |
| for _, num := range nums { | |
| if count == 0 { | |
| candidate = num | |
| } |
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. | |
| * type TreeNode struct { | |
| * Val int | |
| * Left *TreeNode | |
| * Right *TreeNode | |
| * } | |
| */ | |
| func isValidBST(root *TreeNode) bool { | |
| return checkIsValid(root, -1 << 32, (1 << 32) - 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
| /** | |
| * Definition for a binary tree node. | |
| * type TreeNode struct { | |
| * Val int | |
| * Left *TreeNode | |
| * Right *TreeNode | |
| * } | |
| */ | |
| type OrderNode struct { |
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. | |
| * type TreeNode struct { | |
| * Val int | |
| * Left *TreeNode | |
| * Right *TreeNode | |
| * } | |
| */ | |
| func isSubtree(s *TreeNode, t *TreeNode) bool { | |
| queue := []*TreeNode{s} |
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
| const inf = (1 << 31) - 1 | |
| type pair struct { | |
| id int | |
| level int | |
| } | |
| func ladderLength(beginWord string, endWord string, wordList []string) int { | |
| graph := make(map[string][]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
| // a1 + a2 + a3 + .. + an = n * (a1 + an) / 2 | |
| // 1 + 2 + 3 + 4 + 5 | |
| // 5 * (1 + 5) / 2 = 10 | |
| func arrangeCoins(n int) int { | |
| i, j := 1, n + 1 | |
| for i < j { | |
| h := int(uint64(i + j) >> 1) | |
| sum := (h * (1 + h)) / 2 |
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
| var directions = [4][2]int{ | |
| {0, 1}, | |
| {1, 0}, | |
| {0, -1}, | |
| {-1, 0}, | |
| } | |
| func findWords(board [][]byte, words []string) []string { | |
| rows := len(board) | |
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 numSquares(n int) int { | |
| // we are going to keep the minumum number of squares required to get the corresponding number | |
| // index here represents the desire number | |
| nums := make([]int, n + 1) | |
| // we calculate the minumum number of squares for each number from 1 to n | |
| for i := 1; i <= n; i++ { | |
| // as we have to get the minumum, at the first step we use INF as undefined number | |
| num := (1 << 31) - 1 | |