我有以下代码用于读取2个整数:
Array(N, Q) = readLine.split(" ").map(_.toInt)
为此,我收到以下错误:
error: value update is not a member of object Array
如果我这样做
val Array(N, Q) = readLine.split(" ").map(_.toInt)
我明白了:
error: not found: value N
如果我之前申报:
val N, Q
我明白了:
!error: '=' expected but ';' found.
那么如何同时读取这些整数呢?
答案 0 :(得分:5)
不要将变量名称大写。
let myString = "aaabbb"
var myOutput: Int?
let numChars = myString.characters.count
if numChars%2 == 0 {
let midIndex = myString.startIndex.advancedBy(numChars/2)
let myLeftString = myString.substringToIndex(midIndex)
let myRightString = myString.substringFromIndex(midIndex)
let numDifferentLetters = 26 // asymptotic behaviour: assume n > numDifferentLetters
var letterOccurencesLhs = [Int](count: numDifferentLetters, repeatedValue: 0) // < n
var letterOccurencesRhs = letterOccurencesLhs // swift: arr value type, < n
// n/2
for charAscii in myLeftString.utf8 {
letterOccurencesLhs[Int(charAscii)-96] += 1
}
// n/2
for charAscii in myRightString.utf8 {
letterOccurencesRhs[Int(charAscii)-96] += 1
}
// check order of compliance for each letter, w.r.t. numDiffLetters, < n
var mySum = 0
for i in 0 ..< numDifferentLetters {
if letterOccurencesLhs[i] * letterOccurencesRhs[i] > 0 {
mySum += min(letterOccurencesLhs[i], letterOccurencesRhs[i])
}
}
myOutput = numChars/2-mySum
}
else {
myOutput = -1
}
print("Output: \(myOutput!)") // 3 for "aaabbb"
// 2 for "aaabbbbbbbab"
// Complexity: O(n)
Scala模式匹配以大写字母开头的特殊情况标识符。
相关问题: