小红书前端3.26笔试第三题
算法难度好像比别的笔试小一些,正常无优化都能a82%,利用=号优化一下100%,赛码好像跟牛客挺像的,但是用户体验好像两个极端
package main import "fmt" func scan(n int, dist []int) { for i := 0; i < n; i++ { fmt.Scan(&dist[i]) } } func main() { N := 0 fmt.Scan(&N) arr := make([]int, N) scan(N, arr) M := 0 fmt.Scan(&M) Ls := make([]int, M) Rs := make([]int, M) Os := "" Xs := make([]int, M) scan(M, Ls) scan(M, Rs) fmt.Scan(&Os) scan(M, Xs) var compute func(int, int) int compute = func(i int, p int) int { if i==-1{ return arr[p] } l, r, o, x := Ls[i], Rs[i], Os[i], Xs[i] if p<r&&p>=l-1{ if o == '|' { return compute(i-1,p) | x } else if o == '&' { return compute(i-1,p) & x }else{ return x } }else{ return compute(i-1,p) } } for p:=0;p<N;p++{ arr[p]=compute(M-1,p) } for i := 0; i < N; i++ { fmt.Print(arr[i], " ") } }