【发布时间】:2017-12-12 22:13:55
【问题描述】:
我正在解决一些琐碎的问题来学习 Scala。这是我想出的
def isUnique(str: String): Boolean = {
if (str.length > 128) return false
val uniqueChars = new Array[Boolean](128)
!(str.map(c => addChar(c, uniqueChars)).find(identity).isDefined)
}
def addChar(ch: Char, uniqueChars: Array[Boolean]): Boolean = {
if (uniqueChars(ch)) return true else {
uniqueChars(ch) = true;
return false
}
是这样吗?
请注意,此时我并不关心逻辑或优化。我只需要学习 Scala 的做法。
[编辑] 假设我们不想使用 string distinct 方法。我只需要验证Scala的函数风格。
【问题讨论】:
-
str.length == str.distinct.length -
糟糕!这更直接:
str == str.distinct -
@jwvh 谢谢...我不知道这种方法。你能在问题中看到我的编辑吗?假设我们不想使用 distinct。
标签: scala functional-programming