Skip to content

Commit

Permalink
feat: add golang solution to lc problems: No.54,154,189
Browse files Browse the repository at this point in the history
  • Loading branch information
maolonglong committed Aug 2, 2021
1 parent 844c328 commit fc6e686
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 2 deletions.
37 changes: 37 additions & 0 deletions solution/0000-0099/0054.Spiral Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,43 @@ var spiralOrder = function (matrix) {
};
```

### **Go**

```go
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
}
```

### **...**

```
Expand Down
37 changes: 37 additions & 0 deletions solution/0000-0099/0054.Spiral Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,43 @@ var spiralOrder = function (matrix) {
};
```

### **Go**

```go
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
}
```

### **...**

```
Expand Down
32 changes: 32 additions & 0 deletions solution/0000-0099/0054.Spiral Matrix/Solution.go
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ var findMin = function (nums) {
};
```

### **Go**

```go
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]
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ var findMin = function (nums) {
};
```

### **Go**

```go
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]
}
```

### **...**

```
Expand Down
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]
}
23 changes: 22 additions & 1 deletion solution/0100-0199/0189.Rotate Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<pre>
<strong>输入:</strong>nums = [-1,-100,3,99], k = 2
<strong>输出:</strong>[3,99,-1,-100]
<strong>解释:</strong>
<strong>解释:</strong>
向右旋转 1 步: [99,-1,-100,3]
向右旋转 2 步: [3,99,-1,-100]</pre>

Expand Down Expand Up @@ -136,6 +136,27 @@ var rotate = function (nums, k) {
};
```

### **Go**

```go
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--
}
}
```

### **...**

```
Expand Down
23 changes: 22 additions & 1 deletion solution/0100-0199/0189.Rotate Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rotate 3 steps to the right: [5,6,7,1,2,3,4]
<pre>
<strong>Input:</strong> nums = [-1,-100,3,99], k = 2
<strong>Output:</strong> [3,99,-1,-100]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
</pre>
Expand Down Expand Up @@ -112,6 +112,27 @@ var rotate = function (nums, k) {
};
```

### **Go**

```go
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--
}
}
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions solution/0100-0199/0189.Rotate Array/Solution.go
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--
}
}

0 comments on commit fc6e686

Please sign in to comment.