Skip to content

Latest commit

 

History

History

0869.Reordered-Power-of-2

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

题目

Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.

Return true if and only if we can do this in a way such that the resulting number is a power of 2.

Example 1:

Input:1 Output:true 

Example 2:

Input:10 Output:false 

Example 3:

Input:16 Output:true 

Example 4:

Input:24 Output:false 

Example 5:

Input:46 Output:true 

Note:

  1. 1 <= N <= 10^9

题目大意

给定正整数 N ,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零。如果我们可以通过上述方式得到 2 的幂,返回 true;否则,返回 false。

解题思路

  • 将整数每个位上的所有排列看成字符串,那么题目转换为判断这些字符串是否和 2 的幂的字符串是否一致。判断的方法有很多种,笔者这里判断借助了一个 map。两个不同排列的字符串要相等,所有字符出现的频次必定一样。利用一个 map 统计它们各自字符的频次,最终都一致,则判定这两个字符串是满足题意的。
  • 此题数据量比较小,在 [1,10^9] 这个区间内,2 的幂只有 30 几个,所以最终要判断的字符串就是这 30 几个。笔者这里没有打表了,采用更加一般的做法。数据量更大,此解法代码也能通过。

代码

package leetcode import"fmt"funcreorderedPowerOf2(nint) bool { sample, i:=fmt.Sprintf("%v", n), 1forlen(fmt.Sprintf("%v", i)) <=len(sample) { t:=fmt.Sprintf("%v", i) iflen(t) ==len(sample) &&isSame(t, sample) { returntrue } i=i<<1 } returnfalse } funcisSame(t, sstring) bool { m:=make(map[rune]int) for_, v:=ranget { m[v]++ } for_, v:=ranges { m[v]--ifm[v] <0 { returnfalse } ifm[v] ==0 { delete(m, v) } } returnlen(m) ==0 }
close