P1: 单词表示成词向量,再做比较
object P1 {
def countCharacters(words: Array[String], chars: String): Int = {
def toVec(s:String):Array[Int] = {
val A = Array.fill(26)(0)
s foreach {ch => A(ch - 'a') += 1}
A
}
def f(A:Array[Int])(s:String):Int = {
if((A zip toVec(s)) forall {case (x,y) => x >= y}) s.length else 0
}
words map f(toVec(chars)) sum
}
}
p2: 按层求和,如果和大于已知的,就更新,并更新层数。
object P2 {
type T = TreeNode
def maxLevelSum(root: TreeNode): Int = {
def getSum(l:List[T]):Int = l.map(_.value) sum
def next(l:List[T]):List[T] = l.flatMap (x => List(x.left, x.right).filter(_!=null))
def f(l:List[T],cur:Int, max_sum:Int = Int.MinValue, idx:Int =0 ):Int = l match {
case Nil => idx
case _ =>
if(getSum(l) > max_sum) f(next(l), cur+1, getSum(l), cur)
else f(next(l), cur+1, max_sum, idx)
}
f(List(root), 1)
}
}
p3: 类似p2
object P3 {
case class P(x:Int, y:Int){
def +(that:P):P = P(x + that.x, y + that.y)
}
val neigh = List(P(0,1), P(0,-1), P(-1,0), P(1,0))
def maxDistance(grid: Array[Array[Int]]): Int = {
val n = grid.length
val m = grid(0).length
val l = for{
i <- 0 until n
j <- 0 until m
if grid(i)(j) == 1
} yield P(i,j)
if(l.isEmpty || l.length == m * n) return -1
def inBound(p:P):Boolean = {
grid.indices.contains(p.x) &&
grid(p.x).indices.contains(p.y)
}
def nei(p:P):List[P]= {
neigh.map(_+p) filter inBound
}
def get(p:P):Int = grid(p.x)(p.y)
def solve(l:Seq[P],v:Set[P],acc:Int):Int = {
if(v.size == n * m) acc
else {
val nl = for{
x <- l
y <- nei(x)
if !v.contains(y)
}yield y
solve(nl.distinct, v ++ nl,acc + 1)
}
}
solve(l, l.toSet, 0)
}
}
p4: s ( i , j ) < = s ( i , n − 1 ) , s ( i , j ) : s u b S t r i n g o f s . f r o m i t o j s(i,j) <= s(i,n-1), s(i,j):\ subString\ of \ s.\ from\ i\ to\ j s(i,j)<=s(i,n−1),s(i,j): subString of s. from i to j
scala code超时,我转化成python3交了一次就AC了。 因为无法理解对100000个a的复杂度是 O ( n ) O(n) O(n), 居然超时了。 这是不可能的事情,一定是scala在处理字符串是有额外的开销。
object P4 {
def lastSubstring(s: String): String = {
def g(i:Int, k:Int, d:Int):Int = {
if(i + d >= s.length) k
else if(s(i+d) == s(k+d)) g(i,k, d+1)
else if(s(i+d) > s(k+d)) i
else k
}
var ans = s.length -1
for { i <- (s.length -2 to 0 by -1) }{
if(s(i) > s(ans)) ans = i
else if (s(i) < s(ans)) {}
else {
if(ans - i == 1) ans = i
else ans = g(ans, i, 1)
}
}
s.slice(ans, s.length)
}
}
class Solution:
def lastSubstring(self, s: str) -> str:
n = len(s)
ans = n-1
def g(i, k, d):
if i + d >= n:
return k
if s[i+d] == s[k+d]:
return g(i,k, d+1)
if s[i+d] > s[k+d]:
return i
if s[i+d] < s[k+d]:
return k
for i in range (n-2,-1,-1):
if s[i] == s[ans]:
if ans - i == 1:
ans = i
else:
ans = g(ans, i, 1)
if s[i] > s[ans]:
ans = i
return s[ans:]