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/maximum-product-subarray | |
| func maxProduct(nums []int) int { | |
| numsLen := len(nums) | |
| if numsLen == 0 { | |
| return 0 | |
| } | |
| if numsLen == 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
| // https://leetcode.com/problems/find-minimum-in-rotated-sorted-array | |
| func findMin(nums []int) int { | |
| n := len(nums) | |
| i, j := 0, n | |
| minItem := (1 << 31) - 1 | |
| for i < j { | |
| h := (i + j) >> 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
| // https://leetcode.com/problems/binary-search-tree-iterator | |
| /** | |
| * 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/largest-number | |
| import "sort" | |
| func largestNumber(nums []int) string { | |
| strs := make([]string, len(nums)) | |
| for i, x := range nums { | |
| strs[i] = strconv.Itoa(x) | |
| } |
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-right-side-view | |
| /** | |
| * 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/remove-nth-node-from-end-of-list | |
| /** | |
| * Definition for singly-linked list. | |
| * type ListNode struct { | |
| * Val int | |
| * Next *ListNode | |
| * } | |
| */ |
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/number-of-islands | |
| func numIslands(grid [][]byte) int { | |
| numbers := 0 | |
| for i, row := range grid { | |
| for j, el := range row { | |
| if el == byte('1') { | |
| findIsland(grid, i, j) | |
| numbers++ |
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/bitwise-and-of-numbers-range | |
| func rangeBitwiseAnd(m int, n int) int { | |
| result := 0 | |
| // Algorithm is quite simple: | |
| // | |
| // Declare order as 2^i, it is going to look in this way: 1, 10, 100, 1000 ... | |
| // | |
| // Lets consider an example: 101 and 111, diff = 7 - 5 = 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
| // https://leetcode.com/problems/course-schedule | |
| const ( | |
| free = byte(0) | |
| observed = byte(1) | |
| visited = byte(2) | |
| ) | |
| func canFinish(numCourses int, prerequisites [][]int) bool { | |
| if len(prerequisites) == 0 { |
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/course-schedule | |
| func canFinish(numCourses int, prerequisites [][]int) bool { | |
| if len(prerequisites) == 0 { | |
| return true | |
| } | |
| graph := make([][]int, numCourses) | |
| inDegree := make([]int, numCourses) | |