def indexes(word:String)={
val charToInts = new mutable.HashMap[Char,SortedSet[Int]]()
var i =0
word.foreach(char =>{
charToInts.get(char) match {
case Some(set) => charToInts(char)=set+i
case None => charToInts(char)=SortedSet(i)
}
i+=1
})
charToInts
}
2. 重复前一个练习,这次用字符到列表的不可变映射。
def indexes(word:String)={
var charToInts = new collection.immutable.HashMap[Char,SortedSet[Int]]()
var i = 0
word.foreach(char=>{
charToInts.get(char) match {
case Some(set) => set+i
ca
1. 编写一个函数,给定字符串,产出一个包含所有字符的下标的映射。举例来说:indexes(“Mississippi”)应返回一个映射,让’M’对应集{0},'i’对应集{1,4,7,10},依此类推。使用字符到可变集的映射。另外,你如何保证集是经过排序的?def indexes(word:String)={ val charToInts = new mutable.HashMap[Cha...