通知设置 新通知
Windows11 上安装linux子系统之后,运行程序提示 stdlib.h 文件缺失,怎么解决?
回复C语言 • zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 2128 次浏览 • 2021-10-11 10:11
在执行geth --help命令时,提示错误:`GLIBC_2.14' not found
回复区块链 • zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 2828 次浏览 • 2021-08-12 21:37
Elasticsearch如何查找两个圆形区域的差集中的文档?
回复ElasticSearch • zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 3666 次浏览 • 2021-02-20 19:15
#英文技术文章翻译# Nitro框架(原Go Micro)介绍文档
GoLang • zkbhj 发表了文章 • 0 个评论 • 2171 次浏览 • 2020-11-26 11:37
Nitro (formerly known as Go Micro) is a future framework for distributed app development, IoT, edge and p2p.
Nitro(原来以Go Micro为大家所熟知)是一个面向未来的适用于分布式应用、IoT(物联网)边缘计算和P2P的框架。
Overview 综述
Nitro will provide the core requirements for distributed app development, IoT, edge and p2p including RPC and Event driven communication. The Nitro mantra is in-memory defaults with a pluggable architecture. Blaze with pure in-memory development and swap out as needed to go multi-process or multi-host.
Nitro将会为分布式应用、IoT(物联网)边缘计算和P2P的应用开发提供核心所需要的功能包括RPC和事件驱动通信。Nitro是一个默认内存式的可插拔的架构。纯内存式编程并且可以随时根据需要切换到go 的多进程或多线程模式上。
Note: Nitro is currently undergoing a complete rewrite and is considered unstable for use.
提示:Nitro目前正在经历全面的重写并且在真正使用到正式项目中时需要深思熟虑它的安全性问题。
Features 特性
Now focusing on dapps, IoT, edge and p2p. Features include:
现在(框架)只专注于分布式应用、物联网、边缘计算和P2P。特性包括:
Lightweight RPC based communicationsEvent broadcasting and notificationsCRDT Data synchronisation and storageConsensus protocol and execution engineWebAssembly target compilation supportUnique randomized token generation aka BLSP2P gossip networking defined in user space
基于轻量级的RPC通信事件驱动和消息通知CRDT数据同步和存储(CRDT是Conflict-free Replicated Data Type的简称,也称为a passive synchronisation,即免冲突的可复制的数据类型)一致的协议和执行引擎实验性的编译器支持唯一token生成器BLS用户空间自定义P2P网络
Future 未来
In the future there's the potential to launch a live network based on Nitro. More on that soon.
在未来开启一个基于Nitro的在线网络服务是可能的。很快就会实现了。
FAQ QA环节
What happened to Go Micro?
Go Micro发生了什么?
Go Micro has now been renamed to Nitro. Go Micro moved back to being a personal project and no longer lives under the organisation github.com/micro. The company is now doubling down on Micro itself and has pulled in the needed interfaces to consolidate a Server, Framework and CLI into one tool. Go Micro is now no longer maintained by a company. Yet it continued to create confusion even as a personal repo. So for that reason, we're renaming to Nitro. Go Micro V2 has been archived at microhq/go-micro and the plugins at microhq/plugins.
Go Micro现在已经被重新命名为Nitro。Go Micro项目已经变成了一个个人项目并且不再是一个团队(github.com/micro.)负责的项目。公司正在加倍使用Micro并且引入了必要的接口使之成为一个服务、框架和CLI的工具(上云提供商业服务,见 m3o.com)。Go Micro现在已经不再被任何公司维护。但是使用Go Micro作为名字的话依然会产生困惑,即使作为一个个人回收的项目。基于上面的原因,我们考虑把它重新命名为Nitro。Go Micro V2版本已经被归档在 microhq/go-micro ,插件归档在microhq/plugins。What's the new direction of Nitro?Nitro新的方向是什么?
Nitro will now focus on distributed app development using the Go standard library. It will continue to define abstractions for distributed systems but will only do so without external dependencies. In this manner the hope is Nitro can be picked up with minimal overhead for all sorts of new applications that have a low memory or low resource footprint. The assumption is there are places which would like to use distributed apps just as embedded systems or web assembly, unikernels, and related targets that would benefit from a framework that defined these as primitives for such use.
Nitro从现在开始将聚焦在用Go标准库来开发分布式应用。框架会持续为分布式系统定义抽象但是仅限于在没有外部依赖的情况下。通过这种方式,我们希望Nitro可以以最小的开销为那些内存或资源占有率低的程序使用。假设有些场景下更喜欢用分布式应用,像是嵌入式系统或者实验性系统,unikernels,和其他目标系统,他们将会从这个被定义为专为这些应用而生的基础框架中受益。
How do Nitro and Micro now differ?
现在Nitro和Micro有什么不同?
Micro is a platform for cloud native development. A complete experience that includes a server, framework and multi-language clients. Beyond that it also include environments, multi-tenancy and many more features which push it towards being a hosted Micro as a Service offering. It is a complete platform.
Micro是一个为云原生应用开发的平台框架。是一个完整的经验包括服务端,框架和多种语言客户端。除了上面提到的它也包括环境,多客端和未来更多的特性,这些特性把它推向成为一个提供服务的Micro托管产品。它是一个完整的平台框架。
Nitro is more of a embeddable framework for distributed app development and now once again a purely personal project maintained by me and perhaps others who still find use for it commercially or noncommercially. It's of sentimental value and something I'd like to carry on for personal projects such as things related to edge, IoT, embedded systems, p2p, web assembly, etc.
Nitro是一个为分布式应用提供的可嵌入式的框架,并且再一次成为一个完全的个人项目被我维护,也许会有人会发现把它并把它用于商业用途或非商业用途。这个框架充满了感情色彩以及我可能希望和喜欢的一些东西带入到这个个人项目中,比如像是一些跟边缘计算、物联网、嵌入式系统、P2P和实验性的东西等等。
I used Go Micro to build microservices. What should I do now?
我曾经用Go Micro来构建微服务应用。我现在应该做些什么?
You should quite honestly go look at Micro and then consider the hosted offering at m3o.com which starts as a purely free dev environment in the cloud. Micro continues to address many of the concerns and requirements you had if not more. It is likely you managed metrics, tracing, logging and much other boilerplate that needed to be plugged in. Micro will now take this complete platform story approach and help you in that journey e.gyou're probably running managed kubernetes on a major cloud provider with many other things. We're doing that for you instead as a company and platform team.
你应该非常诚恳的看一看 Micro这个项目然后考虑m3o.com提供的托管产品,这个托管产品开始会在云上提供一个完全免费的开发环境。Micro将会继续设法解决你有的问题和需求如果不太多的话。就像你管理的应用性能,链路追踪,日志和许多其他的组件包被引入的。Micro会采用这种完整的平台托管方式来帮助你解决迁移过程中你可能遇到的问题,比如管理在一个核心的云服务提供上运行的项目或者其他一些事情。我们代替公司或者平台团队为你做这些事情。
I want to use Go Micro version 2.0 for my company. Can I still do that?
我想使用Go Micro 2.0版本为我们公司的项目。我可以一直这么做吗?
Yes. Go Micro 2.0 is still Apache 2.0 licensed which means you can still freely use it for everything you were using before. If you're a new user you can do the same. These things are using go modules so you're import path is simply github.com/micro/go-micro/v2 as it was before. Because GitHub handles redirects this should not break. Please continue to use it if you like, but my own support for 2.0 is now end of life.
当然。Go Micro 2.0 将一直是一个Apache 2.0 授权的,这意味着你可以一直免费使用它来做任何事情就像你以前用它一样。如果你是一个新用户,你也可以这么做。可以用go modules来做这些事,所以你可以通过import 这个路径 github.com/micro/go-micro/v2 就像以前一样。因为GitHub上面的链接不会被失效掉。如果你喜欢,请继续使用它。但是我从现在开始不会再继续支持和维护这个2.0版本了。
Why has the license changed from Apache 2.0 to Polyform Noncommercial?
为什么授权许可从Apache 2.0变成了Polyform Noncommercia?
Go Micro was largely a solo maintained effort for the entirety of its lifetime. It has enabled the creation of a company called Micro Services, Inc. which now focuses on Micro as a Service and has consolidated any interfaces here into a service library in that project. For the most part, Go Micro was underfunded and in some ways under appreciated. In version 3.0, going back to something of a personal project of more than 6 years I have made the hard decision to relicense as a noncommercial project.
Go Micro 很大程度上在它的整个生命周期中都是一个单独维护的项目。它使一家名为Micro Services的公司成立成为可能,这家公司目前主要聚焦在Micro作为一个服务来提供,然后把所有的接口都整合成一个服务库。在很大程度上,Go Micro项目资金不足且在一定程度上没有得到重视。在3.0版本中,追忆这个超过6年的个人项目的一些事情的情况下我做了一个艰难的决定就是重新作为一个非商业项目获得授权。
查看全部
Nitro (formerly known as Go Micro) is a future framework for distributed app development, IoT, edge and p2p.
Nitro(原来以Go Micro为大家所熟知)是一个面向未来的适用于分布式应用、IoT(物联网)边缘计算和P2P的框架。
Overview 综述
Nitro will provide the core requirements for distributed app development, IoT, edge and p2p including RPC and Event driven communication. The Nitro mantra is in-memory defaults with a pluggable architecture. Blaze with pure in-memory development and swap out as needed to go multi-process or multi-host.
Nitro将会为分布式应用、IoT(物联网)边缘计算和P2P的应用开发提供核心所需要的功能包括RPC和事件驱动通信。Nitro是一个默认内存式的可插拔的架构。纯内存式编程并且可以随时根据需要切换到go 的多进程或多线程模式上。
Note: Nitro is currently undergoing a complete rewrite and is considered unstable for use.
提示:Nitro目前正在经历全面的重写并且在真正使用到正式项目中时需要深思熟虑它的安全性问题。
Features 特性
Now focusing on dapps, IoT, edge and p2p. Features include:
现在(框架)只专注于分布式应用、物联网、边缘计算和P2P。特性包括:
- Lightweight RPC based communications
- Event broadcasting and notifications
- CRDT Data synchronisation and storage
- Consensus protocol and execution engine
- WebAssembly target compilation support
- Unique randomized token generation aka BLS
- P2P gossip networking defined in user space
- 基于轻量级的RPC通信
- 事件驱动和消息通知
- CRDT数据同步和存储(CRDT是Conflict-free Replicated Data Type的简称,也称为a passive synchronisation,即免冲突的可复制的数据类型)
- 一致的协议和执行引擎
- 实验性的编译器支持
- 唯一token生成器BLS
- 用户空间自定义P2P网络
Future 未来
In the future there's the potential to launch a live network based on Nitro. More on that soon.
在未来开启一个基于Nitro的在线网络服务是可能的。很快就会实现了。
FAQ QA环节
What happened to Go Micro?
Go Micro发生了什么?
Go Micro has now been renamed to Nitro. Go Micro moved back to being a personal project and no longer lives under the organisation github.com/micro. The company is now doubling down on Micro itself and has pulled in the needed interfaces to consolidate a Server, Framework and CLI into one tool. Go Micro is now no longer maintained by a company. Yet it continued to create confusion even as a personal repo. So for that reason, we're renaming to Nitro. Go Micro V2 has been archived at microhq/go-micro and the plugins at microhq/plugins.
Go Micro现在已经被重新命名为Nitro。Go Micro项目已经变成了一个个人项目并且不再是一个团队(github.com/micro.)负责的项目。公司正在加倍使用Micro并且引入了必要的接口使之成为一个服务、框架和CLI的工具(上云提供商业服务,见 m3o.com)。Go Micro现在已经不再被任何公司维护。但是使用Go Micro作为名字的话依然会产生困惑,即使作为一个个人回收的项目。基于上面的原因,我们考虑把它重新命名为Nitro。Go Micro V2版本已经被归档在 microhq/go-micro ,插件归档在microhq/plugins。What's the new direction of Nitro?Nitro新的方向是什么?
Nitro will now focus on distributed app development using the Go standard library. It will continue to define abstractions for distributed systems but will only do so without external dependencies. In this manner the hope is Nitro can be picked up with minimal overhead for all sorts of new applications that have a low memory or low resource footprint. The assumption is there are places which would like to use distributed apps just as embedded systems or web assembly, unikernels, and related targets that would benefit from a framework that defined these as primitives for such use.
Nitro从现在开始将聚焦在用Go标准库来开发分布式应用。框架会持续为分布式系统定义抽象但是仅限于在没有外部依赖的情况下。通过这种方式,我们希望Nitro可以以最小的开销为那些内存或资源占有率低的程序使用。假设有些场景下更喜欢用分布式应用,像是嵌入式系统或者实验性系统,unikernels,和其他目标系统,他们将会从这个被定义为专为这些应用而生的基础框架中受益。
How do Nitro and Micro now differ?
现在Nitro和Micro有什么不同?
Micro is a platform for cloud native development. A complete experience that includes a server, framework and multi-language clients. Beyond that it also include environments, multi-tenancy and many more features which push it towards being a hosted Micro as a Service offering. It is a complete platform.
Micro是一个为云原生应用开发的平台框架。是一个完整的经验包括服务端,框架和多种语言客户端。除了上面提到的它也包括环境,多客端和未来更多的特性,这些特性把它推向成为一个提供服务的Micro托管产品。它是一个完整的平台框架。
Nitro is more of a embeddable framework for distributed app development and now once again a purely personal project maintained by me and perhaps others who still find use for it commercially or noncommercially. It's of sentimental value and something I'd like to carry on for personal projects such as things related to edge, IoT, embedded systems, p2p, web assembly, etc.
Nitro是一个为分布式应用提供的可嵌入式的框架,并且再一次成为一个完全的个人项目被我维护,也许会有人会发现把它并把它用于商业用途或非商业用途。这个框架充满了感情色彩以及我可能希望和喜欢的一些东西带入到这个个人项目中,比如像是一些跟边缘计算、物联网、嵌入式系统、P2P和实验性的东西等等。
I used Go Micro to build microservices. What should I do now?
我曾经用Go Micro来构建微服务应用。我现在应该做些什么?
You should quite honestly go look at Micro and then consider the hosted offering at m3o.com which starts as a purely free dev environment in the cloud. Micro continues to address many of the concerns and requirements you had if not more. It is likely you managed metrics, tracing, logging and much other boilerplate that needed to be plugged in. Micro will now take this complete platform story approach and help you in that journey e.gyou're probably running managed kubernetes on a major cloud provider with many other things. We're doing that for you instead as a company and platform team.
你应该非常诚恳的看一看 Micro这个项目然后考虑m3o.com提供的托管产品,这个托管产品开始会在云上提供一个完全免费的开发环境。Micro将会继续设法解决你有的问题和需求如果不太多的话。就像你管理的应用性能,链路追踪,日志和许多其他的组件包被引入的。Micro会采用这种完整的平台托管方式来帮助你解决迁移过程中你可能遇到的问题,比如管理在一个核心的云服务提供上运行的项目或者其他一些事情。我们代替公司或者平台团队为你做这些事情。
I want to use Go Micro version 2.0 for my company. Can I still do that?
我想使用Go Micro 2.0版本为我们公司的项目。我可以一直这么做吗?
Yes. Go Micro 2.0 is still Apache 2.0 licensed which means you can still freely use it for everything you were using before. If you're a new user you can do the same. These things are using go modules so you're import path is simply github.com/micro/go-micro/v2 as it was before. Because GitHub handles redirects this should not break. Please continue to use it if you like, but my own support for 2.0 is now end of life.
当然。Go Micro 2.0 将一直是一个Apache 2.0 授权的,这意味着你可以一直免费使用它来做任何事情就像你以前用它一样。如果你是一个新用户,你也可以这么做。可以用go modules来做这些事,所以你可以通过import 这个路径 github.com/micro/go-micro/v2 就像以前一样。因为GitHub上面的链接不会被失效掉。如果你喜欢,请继续使用它。但是我从现在开始不会再继续支持和维护这个2.0版本了。
Why has the license changed from Apache 2.0 to Polyform Noncommercial?
为什么授权许可从Apache 2.0变成了Polyform Noncommercia?
Go Micro was largely a solo maintained effort for the entirety of its lifetime. It has enabled the creation of a company called Micro Services, Inc. which now focuses on Micro as a Service and has consolidated any interfaces here into a service library in that project. For the most part, Go Micro was underfunded and in some ways under appreciated. In version 3.0, going back to something of a personal project of more than 6 years I have made the hard decision to relicense as a noncommercial project.
Go Micro 很大程度上在它的整个生命周期中都是一个单独维护的项目。它使一家名为Micro Services的公司成立成为可能,这家公司目前主要聚焦在Micro作为一个服务来提供,然后把所有的接口都整合成一个服务库。在很大程度上,Go Micro项目资金不足且在一定程度上没有得到重视。在3.0版本中,追忆这个超过6年的个人项目的一些事情的情况下我做了一个艰难的决定就是重新作为一个非商业项目获得授权。
LeetCode刷题Day2:两数相加
LeetCode • zkbhj 发表了文章 • 0 个评论 • 1626 次浏览 • 2020-09-22 20:57
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807LeetCode
首先发一下我的思路已经给出的最初答案(大部分情况是正确的,但是超出数字范围就会和预期结果不一致),总之先把分析思路分享出来。
思路一:常规思路
分别循环两个链表,因为高位是个位,所以不断乘以10的n次方,n代表循环次数,从0开始,直到循环结束,就得到了两个无符号整数。所以,这就要求对输入数字有范围限制的要求,否则数字太大,超出整数的最大范围,得到的结果会因为发生溢出而不符合预期。
然后将两个整数进行加法运算,得到最终结果对应的int数字。
然后再将这个整数转化成字符串,循环字符串,得到字符对应的ASCII码点,然后减去48即可“间接“得到对应的数字。并按序生成链表。
func myPow(a,n int) int {
result := int(1)
for i:= n; i >0 ; i >>= 1 {
if i&1 != 0 {
result *=a
}
a *=a
}
return result
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var a,b,c,i,j int
p1 := l1
for p1!= nil {
a = a + p1.Val * myPow(10,i)
i++
p1 = p1.Next
}
p2 := l2
for p2!= nil {
b = b + p2.Val * myPow(10,j)
j++
p2 = p2.Next
}
c = a + b
str := strconv.Itoa(c)
headNode := new(ListNode)
current := headNode
current.Next = nil
for _,one := range str {
preNode := current
current.Val = int(one-48)
current = new(ListNode)
current.Next = preNode
}
return current.Next
}
思路一:高级思路(学习)
对应位置的数字相加,跟10取模,即可得到该位置上十进制数相加之后的数字,然后和10进行整除,如果两个数相加小于10,则整除之后是0,否则是1,即要进位的1,所以直到两个链表都循环完,或者最后没有进位时,停止循环,最终返回。
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
headList := new(ListNode)
head := headList
num := 0
for (l1 != nil || l2 != nil || num > 0) {
headList.Next = new(ListNode)
headList = headList.Next
if l1 != nil {
num = num + l1.Val
l1 = l1.Next
}
if l2 != nil {
num = num + l2.Val
l2 = l2.Next
}
headList.Val = (num) % 10
num = num / 10
}
return head.Next
}
查看全部
两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807LeetCode
首先发一下我的思路已经给出的最初答案(大部分情况是正确的,但是超出数字范围就会和预期结果不一致),总之先把分析思路分享出来。
思路一:常规思路
分别循环两个链表,因为高位是个位,所以不断乘以10的n次方,n代表循环次数,从0开始,直到循环结束,就得到了两个无符号整数。所以,这就要求对输入数字有范围限制的要求,否则数字太大,超出整数的最大范围,得到的结果会因为发生溢出而不符合预期。
然后将两个整数进行加法运算,得到最终结果对应的int数字。
然后再将这个整数转化成字符串,循环字符串,得到字符对应的ASCII码点,然后减去48即可“间接“得到对应的数字。并按序生成链表。
func myPow(a,n int) int {
result := int(1)
for i:= n; i >0 ; i >>= 1 {
if i&1 != 0 {
result *=a
}
a *=a
}
return result
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var a,b,c,i,j int
p1 := l1
for p1!= nil {
a = a + p1.Val * myPow(10,i)
i++
p1 = p1.Next
}
p2 := l2
for p2!= nil {
b = b + p2.Val * myPow(10,j)
j++
p2 = p2.Next
}
c = a + b
str := strconv.Itoa(c)
headNode := new(ListNode)
current := headNode
current.Next = nil
for _,one := range str {
preNode := current
current.Val = int(one-48)
current = new(ListNode)
current.Next = preNode
}
return current.Next
}
思路一:高级思路(学习)
对应位置的数字相加,跟10取模,即可得到该位置上十进制数相加之后的数字,然后和10进行整除,如果两个数相加小于10,则整除之后是0,否则是1,即要进位的1,所以直到两个链表都循环完,或者最后没有进位时,停止循环,最终返回。
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
headList := new(ListNode)
head := headList
num := 0
for (l1 != nil || l2 != nil || num > 0) {
headList.Next = new(ListNode)
headList = headList.Next
if l1 != nil {
num = num + l1.Val
l1 = l1.Next
}
if l2 != nil {
num = num + l2.Val
l2 = l2.Next
}
headList.Val = (num) % 10
num = num / 10
}
return head.Next
}
LeetCode刷题Day1:两数之和
LeetCode • zkbhj 发表了文章 • 0 个评论 • 1626 次浏览 • 2020-09-22 20:55
今天是Day1,从第一道题目开始:
两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]LeetCode
思路一:暴力遍历
也是最简单最原始的未经优化的方案思路。
首先明确题目要求,可以假设每种输入只会对应一个答案,即一定会有且只有一对符合要求的数据。
所以得到数组的长度,然后从第一个开始进行循环,将目标值target减去当前被循环的值,得到与之可以配对的值,然后在数组中进行寻找,如果找到,就返回2个符合要求的值的索引,否则继续下一次寻找,直到找到。
最外层循环的条件中 x&y异或之后如果为1,说明已经找到对应的结果,就不再循环。时间复杂度是O(n²),空间复杂度是O(1)。
func twoSum(nums []int, target int) []int {
len := len(nums)
var x,y int
for i:=0; i < len && x|y == 0; i++ {
findNum := target - nums[i]
for j:=0; j < len; j++ {
if findNum == nums[j] {
x = i;
y = j;
}
}
}
return []int{x,y}
}
思路二:进一步提升效率,可以利用哈希表
从第一个思路中,可以看到其实在判断一个值在一个“表”中是否存在的时候,我们采用了遍历的方式,这样的话时间复杂度就是O(n),但是大家肯定都知道,哈希表是最擅长做这个事情的。它可以把时间复杂度降到O(1)。所以,这样思路就很清晰了,我们需要先把传过来的数组生成对应的哈希结构,也就是Go里面的map字典,这样我们就可以快速的找到某值在还是不在了。我们把数组的value作为map的key,index作为map的value。代码实现如下。
func twoSum(nums []int, target int) []int {
var hashMap map[int]int
len := len(nums)
var x,y,found int
//初始化字典
hashMap = make(map[int]int)
for i:=0; i < len; i++ {
hashMap[nums[i]] = i
}
for j:=0; j < len && found == 0; j++ {
findNum := target - nums[j]
v, ok := hashMap[findNum]
if ok && v != j {
found = 1
x = j
y = v
}
}
return []int{x,y}
}
今天这道题的难度级别是简单,但是带我回顾了Go语言的数组、map的相关知识和用法,以及哈希表这一知识点。 查看全部
今天是Day1,从第一道题目开始:
两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]LeetCode
思路一:暴力遍历
也是最简单最原始的未经优化的方案思路。
首先明确题目要求,可以假设每种输入只会对应一个答案,即一定会有且只有一对符合要求的数据。
所以得到数组的长度,然后从第一个开始进行循环,将目标值target减去当前被循环的值,得到与之可以配对的值,然后在数组中进行寻找,如果找到,就返回2个符合要求的值的索引,否则继续下一次寻找,直到找到。
最外层循环的条件中 x&y异或之后如果为1,说明已经找到对应的结果,就不再循环。时间复杂度是O(n²),空间复杂度是O(1)。
func twoSum(nums []int, target int) []int {
len := len(nums)
var x,y int
for i:=0; i < len && x|y == 0; i++ {
findNum := target - nums[i]
for j:=0; j < len; j++ {
if findNum == nums[j] {
x = i;
y = j;
}
}
}
return []int{x,y}
}
思路二:进一步提升效率,可以利用哈希表
从第一个思路中,可以看到其实在判断一个值在一个“表”中是否存在的时候,我们采用了遍历的方式,这样的话时间复杂度就是O(n),但是大家肯定都知道,哈希表是最擅长做这个事情的。它可以把时间复杂度降到O(1)。所以,这样思路就很清晰了,我们需要先把传过来的数组生成对应的哈希结构,也就是Go里面的map字典,这样我们就可以快速的找到某值在还是不在了。我们把数组的value作为map的key,index作为map的value。代码实现如下。
func twoSum(nums []int, target int) []int {
var hashMap map[int]int
len := len(nums)
var x,y,found int
//初始化字典
hashMap = make(map[int]int)
for i:=0; i < len; i++ {
hashMap[nums[i]] = i
}
for j:=0; j < len && found == 0; j++ {
findNum := target - nums[j]
v, ok := hashMap[findNum]
if ok && v != j {
found = 1
x = j
y = v
}
}
return []int{x,y}
}
今天这道题的难度级别是简单,但是带我回顾了Go语言的数组、map的相关知识和用法,以及哈希表这一知识点。