File tree Expand file tree Collapse file tree
longest-substring-without-repeating-characters Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // TC: O(n)
2+ // SC: O(m)
3+ func lengthOfLongestSubstring (s string ) int {
4+ charIndex := map [byte ]int {}
5+
6+ maxLen := 0
7+ left := 0
8+ for right := 0 ; right < len (s ); right ++ {
9+ if idx , ok := charIndex [s [right ]]; ok && idx >= left {
10+ left = idx + 1
11+ }
12+ charIndex [s [right ]] = right
13+ if right - left + 1 > maxLen {
14+ maxLen = right - left + 1
15+ }
16+ }
17+
18+ return maxLen
19+ }
Original file line number Diff line number Diff line change 1+ // TC: O(m * n)
2+ // SC: O(m * n)
3+ func numIslands (grid [][]byte ) int {
4+ rows , cols := len (grid ), len (grid [0 ])
5+ islands := 0
6+
7+ var dfs func (r , c int )
8+ dfs = func (r , c int ) {
9+ if r < 0 || c < 0 ||
10+ r >= rows || c >= cols ||
11+ grid [r ][c ] == '0' {
12+ return
13+ }
14+ grid [r ][c ] = '0'
15+ dfs (r + 1 , c )
16+ dfs (r - 1 , c )
17+ dfs (r , c + 1 )
18+ dfs (r , c - 1 )
19+ }
20+
21+ for r := 0 ; r < rows ; r ++ {
22+ for c := 0 ; c < cols ; c ++ {
23+ if grid [r ][c ] == '1' {
24+ dfs (r , c )
25+ islands ++
26+ }
27+ }
28+ }
29+
30+ return islands
31+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * type ListNode struct {
4+ * Val int
5+ * Next *ListNode
6+ * }
7+ */
8+
9+ // TC: O(n)
10+ // SC: O(1)
11+ func reverseList (head * ListNode ) * ListNode {
12+ var prev * ListNode
13+ cur := head
14+
15+ for cur != nil {
16+ temp := cur .Next
17+ cur .Next = prev
18+ prev = cur
19+ cur = temp
20+ }
21+ return prev
22+ }
Original file line number Diff line number Diff line change 1+ // TC: O(m * n)
2+ // SC: O(1)
3+ func setZeroes (matrix [][]int ) {
4+ rowNum , colNum := len (matrix ), len (matrix [0 ])
5+
6+ firstRowIsZero := false
7+
8+ for r := 0 ; r < rowNum ; r ++ {
9+ for c := 0 ; c < colNum ; c ++ {
10+ if matrix [r ][c ] == 0 {
11+ matrix [0 ][c ] = 0
12+ if r > 0 {
13+ matrix [r ][0 ] = 0
14+ } else {
15+ firstRowIsZero = true
16+ }
17+ }
18+ }
19+ }
20+
21+ for r := 1 ; r < rowNum ; r ++ {
22+ for c := 1 ; c < colNum ; c ++ {
23+ if matrix [r ][0 ] == 0 || matrix [0 ][c ] == 0 {
24+ matrix [r ][c ] = 0
25+ }
26+ }
27+ }
28+
29+ if matrix [0 ][0 ] == 0 { // first column is zero
30+ for r := 0 ; r < rowNum ; r ++ {
31+ matrix [r ][0 ] = 0
32+ }
33+ }
34+
35+ if firstRowIsZero {
36+ for c := 0 ; c < colNum ; c ++ {
37+ matrix [0 ][c ] = 0
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ // TC: O(m * n)
2+ // SC: O(m * n)
3+ func uniquePaths (m int , n int ) int {
4+ dp := make ([][]int , m + 1 )
5+ for i := range dp {
6+ dp [i ] = make ([]int , n + 1 )
7+ }
8+ dp [m - 1 ][n - 1 ] = 1
9+
10+ for i := m - 1 ; i >= 0 ; i -- {
11+ for j := n - 1 ; j >= 0 ; j -- {
12+ dp [i ][j ] += dp [i + 1 ][j ] + dp [i ][j + 1 ]
13+ }
14+ }
15+
16+ return dp [0 ][0 ]
17+ }
You can’t perform that action at this time.
0 commit comments