- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是一个函数的实现,它返回按字典顺序排列的下一个排列。这在欧拉问题之一中很有用。
它是为处理字符串而编写的(我需要它)。但是,它应该适用于任何可比较值的索引序列。我尝试通过将 String 的两次出现更改为 IndexedSeq[Char] 来概括它,但这会出现错误:
euler-lib.scala:26: error: type mismatch;
found : IndexedSeq[Char]
required: String
((n.slice(pivot+1, successor):+ n(pivot)) + n.drop(successor+1)).reverse
^
// return the lexographically next permutation to the one passed as a parameter
// pseudo-code from an article on StackOverflow
def nextPermutation(n:String):String = {
// 1. scan the array from right-to-left
//1.1. if the current element is less than its right-hand neighbor,
// call the current element the pivot,
// and stop scanning
// (We scan left-to-right and return the last such).
val pivot = n.zip(n.tail).lastIndexWhere{ case (first, second) => first < second }
//1.2. if the left end is reached without finding a pivot,
// reverse the array and return
// (the permutation was the lexicographically last, so its time to start over)
if (pivot < 0) return n.reverse
//2. scan the array from right-to-left again,
// to find the rightmost element larger than the pivot
// (call that one the successor)
val successor = n.lastIndexWhere{_ > n(pivot)}
//3. swap the pivot and the successor, and
//4. reverse the portion of the array to the right of where the pivot was found
return (n.take(pivot) :+ n(successor)) +
((n.slice(pivot+1, successor):+ n(pivot)) + n.drop(successor+1)).reverse
}
最佳答案
方法+
在 IndexedSeq
用于生成包含一个附加给定元素的新序列,但您想生成一个包含附加序列的序列。方法是++
因此你的最后一行必须是这样的:
(n.take(pivot) :+ n(successor)) ++
((n.slice(pivot+1, successor):+ n(pivot)) ++ n.drop(successor+1)).reverse
String
的奇怪编译器消息预期是因为
+
的签名不匹配,因此用于字符串连接的显式转换开始(这种转换是存在的,因为它允许您编写类似
List(8) + " Test"
的内容)。
A
您将需要另一种类型
CC[X] <: SeqLike[X,CC[X]]
表示序列。通常
C <: SeqLike[A,C]
就足够了,但类型推断器不喜欢那个(在调用该方法时,您总是需要传递
A
和
C
的类型)。
CanBuildFrom[CC[A],A,CC[A]]
需要的参数,例如由
reverse
方法。该参数用于从另一种序列类型构建一种序列类型 - 只需搜索站点即可查看集合 API 如何使用它的一些示例。
import collection.SeqLike
import collection.generic.CanBuildFrom
def nextPermutation[A, CC[X] <: SeqLike[X,CC[X]]](n: CC[A])(
implicit ord: Ordering[A], bf: CanBuildFrom[CC[A],A,CC[A]]): CC[A] = {
import ord._
// call toSeq to avoid having to require an implicit CanBuildFrom for (A,A)
val pivot = n.toSeq.zip(n.tail.toSeq).lastIndexWhere{
case (first, second) => first < second
}
if (pivot < 0) {
n.reverse
}
else {
val successor = n.lastIndexWhere{_ > n(pivot)}
(n.take(pivot) :+ n(successor)) ++
((n.slice(pivot+1, successor):+ n(pivot)) ++ n.drop(successor+1)).reverse
}
}
Vector[Int]
如果您将一个传递给方法和一个
List[Double]
如果你把它传递给方法。那么
String
呢? ?这些不是实际的序列,但它们可以隐式转换为
Seq[Char]
.有可能改变该方法的定义,期望一些可以隐式转换为
Seq[A]
的类型。但话又说回来,类型推断无法可靠地工作——或者至少我无法让它可靠地工作。作为一个简单的解决方法,您可以为
String
定义一个额外的方法。 s:
def nextPermutation(s: String): String =
nextPermutation[Char,Seq](s.toSeq).mkString
关于scala - 概括一个 "next permutation"函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4284971/
Numpy documentation在 np.random.permutation建议所有新代码使用 np.random.default_rng()来自随机生成器包。我在文档中看到,Random G
是否有任何已知的算法如何有效地生成具有附加限制的任何随机多集排列。 例子: 我有多个项目,例如:{1,1,1,2,2,3,3,3} ,以及一组限制性的集合,例如 { {3} , {1,2} , {1,
您将如何生成列表 b(1,6,8,3,9,5) 的所有可能排列包括不同长度的?例子: List a = [1,2,3] generateperms(a) 1,2,3 3,1,2 3,2,1 1,3,2
我不确定如何在限制范围内解决这个问题。 将“单词”视为大写字母 A-Z 的任何序列(不仅限于“字典单词”)。对于至少有两个不同字母的单词,还有其他单词由相同的字母组成但顺序不同(例如,STATIONA
题目地址:https://leetcode.com/problems/permutations/description/ 题目描述 Given a collection of distinct n
一行中有 n 个项目。我们必须在不能选择两个连续项目的限制下找到可以选择项目的方式数。 我试图用递归关系来做,但无法达到任何。请帮我解决问题。 最佳答案 在网上搜索后,我得到了上述问题的解决方案。 假
创建列表或集合的排列非常简单。我需要将函数应用于列表中所有元素的所有子集的每个元素,按照它们出现的顺序。例如: apply f [x,y] = { [x,y], [f x, y], [x, f y],
我遇到了一个看似简单的问题,有人可以帮忙吗? 我有两个列表 a和 b .我可以将列表的元素称为 a[i][j]其中 0
我正在将一个程序从 matlab 翻译成 Python。 matlab代码使用permute方法: B = PERMUTE(A,ORDER) rearranges the dimensions of
谜题 穷举一个数组中各个元素的排列 策略 减而治之、递归 JavaScript解 复制代码 代码如下:
题目地址:https://leetcode.com/problems/next-permutation/description/ 题目描述 Implement next permutation,
从我关于"Using SIMD AVX SSE for tree traversal" ive的另一个问题中,我得到了这个试图进行基准测试的代码。之前我没有对SIMD做任何事情,所以我对这种排列方式有
这段代码为我提供了长度为 x 的 n 值的所有可能组合,总和为 n。 function GETall_distri_pres($n_valeurs, $x_entrees, $combi_presen
如果我想找到列表的排列,我知道排列的数量由多项系数给出。例如,“MISSISSIPPI”有 11 个字母,“S”出现 4 次,“I”出现 4 次,“P”出现两次,“M”出现一次。因此“MISSISSI
我有一个伪代码,我已经将其翻译成java代码,但是每当我运行该代码时,我都会得到一个空的数组列表,但它应该给我一个随机的整数列表。这是伪代码: Algorithm 1. RandPerm(N) Inp
我想计算适合弗里德曼检验的分块设计的所有排列。考虑以下示例: thedata p dim(p) [1] 1295 12 R> head(p) [,1] [,2] [,3] [,4]
这与其说是编程问题,不如说是数学问题,但我认为这里的很多人都非常擅长数学! :) 我的问题是:给定一个 9 x 9 的网格(81 个单元格),其中必须包含数字 1 到 9,每个数字恰好出现 9 次,可
我有这个变量:message = "Hello World"我构建了一个对其进行洗牌的函数: def encrypt3(message,key): random.seed(key) l
以下程序使用 itertools.permutations 从列表中构造一个 URL。 def url_construct_function(): for i in range(1, len(
我有 36 个人和 6 张 table 。我想围绕每张 table 组成 6 个小组。然后再组成 6 个其他组,再组成 6 个其他组……直到每个人都遇到每个人,但没有人遇到两次。 到目前为止,我想出了
我是一名优秀的程序员,十分优秀!