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: update go solutions to lcof problems
- Loading branch information
Showing
18 changed files
with
248 additions
and
250 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
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 |
---|---|---|
@@ -1,34 +1,33 @@ | ||
type CQueue struct { | ||
Stack1 []int | ||
Stack2 []int | ||
Stack1 []int | ||
Stack2 []int | ||
} | ||
|
||
// 入队都往S1压入,弹出时判定S2是否为空,S2非空则弹出S2顶,否则,S1的元素从栈顶依次入S2 | ||
//再从S2弹出 | ||
|
||
func Constructor() CQueue { | ||
return CQueue{Stack1:[]int{},Stack2:[]int{}} | ||
return CQueue{Stack1: []int{}, Stack2: []int{}} | ||
} | ||
|
||
|
||
func (this *CQueue) AppendTail(value int) { | ||
this.Stack1 = append(this.Stack1, value) | ||
func (this *CQueue) AppendTail(value int) { | ||
this.Stack1 = append(this.Stack1, value) | ||
} | ||
|
||
|
||
func (this *CQueue) DeleteHead() int { | ||
if len(this.Stack1) == 0 && len(this.Stack2) == 0 { | ||
return -1 | ||
} | ||
if len(this.Stack2) > 0 { | ||
res := this.Stack2[len(this.Stack2)-1] | ||
this.Stack2 = this.Stack2[0:len(this.Stack2)-1] | ||
return res | ||
} | ||
for len(this.Stack1) > 0 { | ||
this.Stack2 = append(this.Stack2,this.Stack1[len(this.Stack1)-1]) | ||
this.Stack1 = this.Stack1[0:len(this.Stack1)-1] | ||
} | ||
res := this.Stack2[len(this.Stack2)-1] | ||
this.Stack2 = this.Stack2[0:len(this.Stack2)-1] | ||
return res | ||
if len(this.Stack1) == 0 && len(this.Stack2) == 0 { | ||
return -1 | ||
} | ||
if len(this.Stack2) > 0 { | ||
res := this.Stack2[len(this.Stack2)-1] | ||
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1] | ||
return res | ||
} | ||
for len(this.Stack1) > 0 { | ||
this.Stack2 = append(this.Stack2, this.Stack1[len(this.Stack1)-1]) | ||
this.Stack1 = this.Stack1[0 : len(this.Stack1)-1] | ||
} | ||
res := this.Stack2[len(this.Stack2)-1] | ||
this.Stack2 = this.Stack2[0 : len(this.Stack2)-1] | ||
return res | ||
} |
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
func numWays(n int) int { | ||
if n == 0 { | ||
return 1 | ||
} | ||
if n <= 2 { | ||
return n | ||
} | ||
a := make([]int,n) | ||
a[0] = 1 | ||
a[1] = 2 | ||
for i := 2; i<n; i++ { | ||
a[i] = (a[i-1]+a[i-2])%1000000007 | ||
} | ||
return a[n-1] | ||
if n == 0 { | ||
return 1 | ||
} | ||
if n <= 2 { | ||
return n | ||
} | ||
a := make([]int, n) | ||
a[0] = 1 | ||
a[1] = 2 | ||
for i := 2; i < n; i++ { | ||
a[i] = (a[i-1] + a[i-2]) % 1000000007 | ||
} | ||
return a[n-1] | ||
} |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
func minArray(nums []int) int { | ||
l,r := 0,len(nums)-1 | ||
for l < r { | ||
mid := l + (r-l)>>1 | ||
if nums[mid] > nums[r] { | ||
l = mid + 1 | ||
} else if nums[mid] <nums[r] { | ||
r = mid //r 本身不需要被排除 | ||
} else { | ||
r-- | ||
} | ||
} | ||
return nums[l] | ||
l, r := 0, len(nums)-1 | ||
for l < r { | ||
mid := l + (r-l)>>1 | ||
if nums[mid] > nums[r] { | ||
l = mid + 1 | ||
} else if nums[mid] < nums[r] { | ||
r = mid //r 本身不需要被排除 | ||
} else { | ||
r-- | ||
} | ||
} | ||
return nums[l] | ||
} |
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
Oops, something went wrong.