forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add golang solution to lc problems: No.54,154,189
- Loading branch information
1 parent
844c328
commit fc6e686
Showing
9 changed files
with
227 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
func spiralOrder(matrix [][]int) []int { | ||
if len(matrix) == 0 { | ||
return []int{} | ||
} | ||
|
||
m, n := len(matrix), len(matrix[0]) | ||
ans := make([]int, 0, m*n) | ||
|
||
top, bottom, left, right := 0, m-1, 0, n-1 | ||
for left <= right && top <= bottom { | ||
for i := left; i <= right; i++ { | ||
ans = append(ans, matrix[top][i]) | ||
} | ||
for i := top + 1; i <= bottom; i++ { | ||
ans = append(ans, matrix[i][right]) | ||
} | ||
if left < right && top < bottom { | ||
for i := right - 1; i >= left; i-- { | ||
ans = append(ans, matrix[bottom][i]) | ||
} | ||
for i := bottom - 1; i > top; i-- { | ||
ans = append(ans, matrix[i][left]) | ||
} | ||
} | ||
top++ | ||
bottom-- | ||
left++ | ||
right-- | ||
} | ||
|
||
return ans | ||
} |
This file contains 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 file contains 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
17 changes: 17 additions & 0 deletions
17
solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/Solution.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
func findMin(nums []int) int { | ||
left, right := 0, len(nums)-1 | ||
for left+1 < right { | ||
mid := int(uint(left+right) >> 1) | ||
if nums[mid] > nums[right] { | ||
left = mid | ||
} else if nums[mid] < nums[right] { | ||
right = mid | ||
} else { | ||
right-- | ||
} | ||
} | ||
if nums[left] < nums[right] { | ||
return nums[left] | ||
} | ||
return nums[right] | ||
} |
This file contains 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 file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
func rotate(nums []int, k int) { | ||
n := len(nums) | ||
k %= n | ||
|
||
reverse(nums, 0, n-1) | ||
reverse(nums, 0, k-1) | ||
reverse(nums, k, n-1) | ||
} | ||
|
||
func reverse(nums []int, i, j int) { | ||
for i < j { | ||
nums[i], nums[j] = nums[j], nums[i] | ||
i++ | ||
j-- | ||
} | ||
} |