aquayi / leetcode-in-go Goto Github PK
View Code? Open in Web Editor NEWGo Solution for LeetCode algorithms problems, 100% coverage.
License: MIT License
Go Solution for LeetCode algorithms problems, 100% coverage.
License: MIT License
0001
有偿答疑,点击后,扫码付费
这样写貌似好理解点
func swapPairsGood(head *ListNode) *ListNode {
// 新建空头结点指向第一个结点
p := &ListNode{}
p.Next = head
head = p
for p.Next != nil && p.Next.Next != nil{
p1 := p.Next
p2 := p.Next.Next
p3 := p.Next.Next.Next
// 交换
p1.Next = p3
p2.Next = p1
p.Next = p2
p = p.Next.Next
}
return head.Next
}
可以使用反射简化测试用例:
https://github.com/Chyroc/algorithms-go/blob/master/test/test_test.go#L11-L62
实现在这里: https://github.com/Chyroc/algorithms-go/blob/master/test/run_case.go
看到你的项目里面的测试写的特别的长。。
如果不改的话,可以直接close我
在51-n皇后问题中皇后攻击位置id1,id2计算公式不明白是怎么得出的。可以帮忙解释下吗?
你好,上次看了你的readme之后,觉得你加在readme的leetcode badge的创意非常好,但是需要手动添加,这边我做了一个自动的。
[![LeetCode 排名](https://leetcode-badge.chyroc.cn/?name=aQuaYi&leetcode_badge_style=aQuaYi-{{.accepted_submission}}-blue.svg)](https://leetcode.com/aQuaYi/)
这边还有其他的风格,项目地址:https://github.com/Chyroc/leetcode-badge
`func trim(s string) string {
for i := range s {
if s[i] < '0' || '9' < s[i] {
return s[:i]
}
}
return s
}`
不理解return s[:i]这行,如果遇见非数字字符就直接返回前面的序列,那后面的数字字符序列不要了吗?
而你的代码有两次遍历nums
for i := 0; i < len(nums); i++ {
for 0 <= nums[i]-1 && nums[i]-1 < len(nums) && nums[i] != nums[nums[i]-1] {
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
}
}
for k := range nums {
if nums[k] != k+1 {
return k + 1
}
}
第一次遍历就是0(n)了,再加上第二次遍历,是否不符合题意?
{
[]int{6, 3, 9, 0, 5, 6},
[]int{2, 2, 5, 2, 1, 4, 4, 5, 7, 8, 9, 3, 1, 6, 9, 7, 0},
23,
[]int{6, 3, 9, 2, 2, 5, 2, 1, 4, 4, 5, 7, 8, 9, 3, 1, 6, 9, 7, 0, 5, 6, 0},
},
这个测试测试用例
Line 98: Char 37: cannot use param_1 (type *leetcode.ListNode) as type *ListNode in argument to __helper__ (solution.go)
Line 98: Char 37: cannot use param_2 (type *leetcode.ListNode) as type *ListNode in argument to __helper__ (solution.go)
1009
算法不是很理解。
转成数组的想法固然可以,但不是题目的本意,这是链表的做法
func sortedChild(head, tail *ListNode) *TreeNode {
if head == tail {
return nil
}
if head.Next == tail {
//如果只剩这一个元素了
return &TreeNode{Val: head.Val}
}
mid, tmp := head, head
//一个前进一个位置,一个前进2个位置,最后mid会到中间
for tmp != tail && tmp.Next != tail {
mid = mid.Next
tmp = tmp.Next.Next
}
return &TreeNode{
Val: mid.Val,
Left: sortedChild(head, mid),
Right: sortedChild(mid.Next, tail),
}
}
func sortedListToBST(head *ListNode) *TreeNode {
return sortedChild(head, nil)
}
func intersection(nums1 []int, nums2 []int) []int {
appear := map[int]struct{}{} // 记录 nums1 中出现的数字,value 用 struct{} 不占内存
for _, num := range nums1 {
appear[num] = struct{}{}
}
ans := []int{}
for _, num := range nums2 {
if _, ok := appear[num]; ok {
ans = append(ans, num)
delete(appear, num) // 确保结果中元素唯一
}
}
return ans
}
24题目。用递归, 空间复杂度是不是不符合要求
Hi, @aQuaYi. Your solution doesn't pass all the latest test cases(51/55).
Input:
"abc"
"cabcabca"
Output:
-1
Expected:
4
Mind me to pull a request?
这道题为什么不能用go实现,没想明白,望指教
这样写是不是更明了
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
// 头结点
head := &ListNode{}
current := head
carry := 0
for l1 != nil || l2 != nil || carry > 0 {
sum := carry
if l1 != nil {
sum += l1.Val
l1 = l1.Next
}
if l2 != nil {
sum += l2.Val
l2 = l2.Next
}
carry = sum / 10
current.Next = &ListNode{Val: sum % 10}
current = current.Next
}
return head.Next
}
看到您readme上说,免费的算法题,难道LeetCode上不是所有的算法题都可以免费刷吗?
了解不多,希望解答一下。
0008.string-to-integer-atoi
有偿答疑,点击后,扫码付费
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.