package main
import (
"fmt"
)
const MOD = 1000000007
func countPasswordsSpaceOptimized(n int, a []int) int {
prev, current := 1, 0
for i := 1; i <= n; i++ {
if a[i-1] == 0 {
current = prev
} else {
current = ((26 - a[i-1]) * prev) % MOD
}
prev = current
}
return current
}
func main() {
n := 3 // 密码长度
a := []int{5, 2, 1} // 给定的差值
result := countPasswordsSpaceOptimized(n, a)
fmt.Println(result)
}
#福大大架构师每日一题#2023-12-13:用go语言,密码是一串长度为n的小写字母,一则关于密码的线索纸条, 首先将字母
package main import ( "fmt" ) const MOD = 1000000007 func countPasswordsSpaceOptimized(n int, a []int) int { prev, current := 1, 0 for i := 1; i <= n; i++ { if a[i-1] == 0 { current = prev } else { current = ((26 - a[i-1]) * prev) % MOD } prev = current } return current } func main() { n := 3 // 密码长度 a := []int{5, 2, 1} // 给定的差值 result := countPasswordsSpaceOptimized(n, a) fmt.Println(result) }