- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我最近开始尝试使用 Golang。我正在尝试编写一个程序来计算给定 slice 的反转次数,但我遇到了一个问题。
我正在尝试使用基于 MergeSort 的代码对 slice 进行排序,但我的代码似乎无法正确对 slice 进行排序。我假设必须对最后的 slice 进行排序才能使反转计数正常工作,但我不知道该怎么做。我可以在这个问题上得到一些帮助吗?
func InversionCount(a []int) int {
if len(a) <= 1 {
return 0
}
mid := len(a) / 2
left := a[:mid]
right := a[mid:]
leftCount := InversionCount(left)
rightCount := InversionCount(right)
res := make([]int, 0, len(right)+len(left)) //temp slice to hold the sorted left side and right side
iCount := mergeCount(left, right, &res)
a = res //Copies the result back into the original slice
fmt.Println(a) //Why hasn't "a" been sorted?
return iCount + leftCount + rightCount
}
func mergeCount(left, right []int, res *[]int) int {
count := 0
for len(left) > 0 || len(right) > 0 {
if len(left) == 0 {
*res = append(*res, right...)
break
}
if len(right) == 0 {
*res = append(*res, left...)
break
}
if left[0] <= right[0] {
*res = append(*res, left[0])
left = left[1:]
} else { //Inversion has been found
count += 1
*res = append(*res, right[0])
right = right[1:]
}
}
return count
}
func main() {
s := []int{4, 3, 2, 1}
fmt.Print(InversionCount(s))
}
这是我的代码的链接:http://play.golang.org/p/QSJyg_qadD
最佳答案
你需要更换线路
count += 1
与
count += len(left)
要点是在 mergeCount
中的任何一点 left[0] > right[0]
,那么因为 left
已经排序, 所有 left
中剩余的东西相对于 right[0]
倒置。如果这样做,您将得到正确的计数。
您的排序不起作用是另一个问题。如果您有兴趣解决这个问题,我建议您取出所有计数逻辑并尝试修复排序。如果您仍然卡住,它可能值得自己的问题。
一个问题是 InversionCount(arr)
不会导致 arr
被排序,所以
a = res //Copies the result back into the original slice
fmt.Println(a) //Why hasn't "a" been sorted?
这不是您想要的,因为早些时候当您将 mergeCount
应用于 left
和 right
时,那些子数组left
和 right
未按 InversionCount
排序。一个更简单的例子:
package main
import "fmt"
func Foo(a []int) {
a = []int{1, 2, 3, 4}
}
func main() {
a := []int{4, 3, 2, 1}
Foo(a)
fmt.Println(a) // [4, 3, 2, 1]
}
关于recursion - 戈朗 : Counting Inversions Sorting issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25321111/
我正在阅读有关依赖倒置和解耦的理论,但我看不出两者之间的区别。 依赖倒置谈论解耦功能组件,以便较高级别的组件不依赖于较低级别的组件。 解耦谈论同样的事情以及如何实现它。但随后我们的 IoC 容器让事情
我仍在学习网页设计的诀窍,并且我无法理解一些事情是如何工作的...... 我创建了一个函数来切换导航栏标题背景颜色,我发现以下内容是正确的,但不确定原因: 作品: $(".navbar.navbar-
我正在使用 Autofac。我想根据应用于构造函数参数的属性注入(inject)不同的依赖项实现。例如: class CustomerRepository { public CustomerR
我试图绘制生存函数的倒数,因为我得到的数据实际上是随着时间的推移事件比例的增加。我可以生成 Kaplan-Meier 生存图,但我想生成这些的“相反”图。我可以使用以下 fun="cloglog" 获
我只是在阅读控制反转 (IOC) 并且让我烦恼的是它似乎使内存管理变得痛苦。当然,似乎 ioc 主要用于垃圾收集环境(Net、Java、Scripting),而我担心的是非 gc 设置。 我在这里担心
我正在创建一对多的关系。所以,我有一个 parent 和一个 child 。级联属性设置为全部。 我想知道,如果我们考虑以下代码: Parent p = (Parent) session.load(P
我最近读了Ayende's blog post关于使用 XML 配置的自动注册。我想做他所做的,但他的代码片段对我不起作用。我的容器对象上不存在 Register 方法。 这是他的代码: var co
我正在尝试删除一些日志记录依赖项,并偶然发现了 CaSTLe Windsor 的日志记录工具。但是,我对是否应该使用它持怀疑态度。 public class MyClass { public
我想在给定的 Windsor 容器中重新定义一个(默认)实现。这是 OverWrite 的用途吗?不过没用。 container.Register( Compo
似乎每个人都在转向 IoC 容器。我已经尝试“摸索”了一段时间,尽管我不想成为一个在高速公路上走错路的司机,但它仍然没有通过常识对我的考验。让我解释一下,如果我的论点有缺陷,请纠正/启发我: 我的理解
组件驱动开发术语开始得到广泛使用,尤其是。与控制反转有关。 这是什么? 它解决了什么问题? 什么时候合适,什么时候不合适? 最佳答案 What is it? 我认为您答案中的定义很好地涵盖了这个问题。
一个?? b 如果 a 不是 null => 返回 a。 否则(a 为null)=> 返回b。 我想模拟类似它的逆的东西(据我所知,没有运算符可以做到这一点): 如果 a 为 null => 返回 a
一个?? b 如果 a 不是 null => 返回 a。 否则(a 为null)=> 返回b。 我想模拟类似它的逆的东西(据我所知,没有运算符可以做到这一点): 如果 a 为 null => 返回 a
最近我正在查看社区领导者在他们的开源实现中提供的一些源代码。其中一个项目利用了 IOC。这是示例假设代码: public class Class1 { private ISomeInterfa
我熟悉许多代码覆盖工具,它们告诉我测试覆盖了多少行/分支/等的百分比,甚至告诉我代码的哪些部分覆盖率较差。 是否有任何工具可以做相反的事情——也就是说,给定一段代码,它能告诉我哪些测试触及它吗?通
我正在使用 MySQL 来存储财务资料,并使用这些数据来构建每个帐户的所有交易记录等。出于性能原因 - 并防止用户被庞大的表格所淹没 - 我对结果进行了分页。 现在,作为收银机的一部分,我显示帐户的运
我正在尝试用 Swift 编写一个算法来计算三角形的角度。引用检查 https://www.mathsisfun.com/algebra/trig-solving-sss-triangles.html
在 STL 中或一般情况下是否存在一种“反向”关联容器?例如,我想要一个容器,其中同一元素由一组键共享。 假设我的 key 是 int , 那么我会有例如: container.at(3) -> so
一些 IOC 容器具有基于约定的所谓自动连接,例如,IProductRepository 映射到 ProductRepository,而无需您进行任何手动连接。 Ninject 有这种东西吗? 最佳答
我正在处理遗留代码。 我有同一个类的不同方法,它们将不同的参数传递给依赖项的构造函数。我正在尝试介绍一些基本的 IoC 用法。现在我有 StructureMap 像这样传递我的参数: var thin
我是一名优秀的程序员,十分优秀!