mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1177 from xuerbujia/master
添加(0141.环形链表.md)和 添加(0189.轮转数组.md):增加go版本
This commit is contained in:
@ -106,6 +106,21 @@ class Solution:
|
|||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
func hasCycle(head *ListNode) bool {
|
||||||
|
if head==nil{
|
||||||
|
return false
|
||||||
|
} //空链表一定不会有环
|
||||||
|
fast:=head
|
||||||
|
slow:=head //快慢指针
|
||||||
|
for fast.Next!=nil&&fast.Next.Next!=nil{
|
||||||
|
fast=fast.Next.Next
|
||||||
|
slow=slow.Next
|
||||||
|
if fast==slow{
|
||||||
|
return true //快慢指针相遇则有环
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### JavaScript
|
### JavaScript
|
||||||
|
@ -124,6 +124,19 @@ class Solution:
|
|||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
func rotate(nums []int, k int) {
|
||||||
|
l:=len(nums)
|
||||||
|
index:=l-k%l
|
||||||
|
reverse(nums)
|
||||||
|
reverse(nums[:l-index])
|
||||||
|
reverse(nums[l-index:])
|
||||||
|
}
|
||||||
|
func reverse(nums []int){
|
||||||
|
l:=len(nums)
|
||||||
|
for i:=0;i<l/2;i++{
|
||||||
|
nums[i],nums[l-1-i]=nums[l-1-i],nums[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## JavaScript
|
## JavaScript
|
||||||
|
Reference in New Issue
Block a user