funcisAnagram(s string, t string)bool { record := [26]int{} for _,s := range s{ record[s-rune('a')]++ } for _,s := range t{ record[s-rune('a')]-- } return record == [26]int{} }
classSolution { publicbooleanisHappy(int n) { HashSet<Integer>set = newHashSet<>(); while(n !=1 && !set.contains(n)){ set.add(n); n = cal(n); //调换一下顺序,不调换的话少了一个数 } return n ==1 ; } publicintcal(int n){ intres=0; while(n > 0){ inti= n %10; res += i*i; n = n/10; } return res; } }
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
funcisHappy(n int)bool { m := make(map[int]bool,0) for n != 1 && !m[n]{ n,m[n]= cal(n),true//好简洁的写法!! } return n==1 } funccal(n int)int{ res := 0 for n >0{ i:= n%10 res += i*i n = n/10 } return res }
funccanConstruct(ransomNote string, magazine string)bool { res :=make([]int,26) for _,i := range magazine{ res[ i - 'a' ]++ } for _,i := range ransomNote{ res[ i - 'a']-- } for _,i := range res{ if i < 0 { returnfalse } } returntrue }