给定正整数 N ,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零。
如果我们可以通过上述方式得到 2 的幂,返回 true;否则,返回 false。
示例 1:
输入:1
输出:true
示例 2:
输入:10
输出:false
示例 3:
输入:16
输出:true
示例 4:
输入:24
输出:false
示例 5:
输入:46
输出:true
class Solution {
public boolean reorderedPowerOf2(int N) {
// Build eg. N = 128 -> A = [1, 2, 8]
String S = Integer.toString(N);
int[] A = new int[S.length()];
for (int i = 0; i < S.length(); ++i) {
A[i] = S.charAt(i) - '0';
}
return permutations(A, 0);
}
// Return true if A represents a valid power of 2
public boolean isPowerOfTwo(int[] A) {
if (A[0] == 0) {
return false; // no leading zero
}
// Build eg. A = [1, 2, 8] -> N = 128
int N = 0;
for (int x : A) {
N = 10 * N + x;
}
// Check that there are no other factors besides 2
return (N & (N-1))==0;
}
/**
* Returns true if some permutation of (A[start], A[start+1], ...) can result in A representing
* a power of 2.
*/
public boolean permutations(int[] A, int start) {
if (start == A.length) {
return isPowerOfTwo(A);
}
// Choose some index i from [start, A.length - 1]
// to be placed into position A[start].
for (int i = start; i < A.length; ++i) {
// Place A[start] with value A[i].
swap(A, start, i);
// For each such placement of A[start], if a permutation
// of (A[start+1], A[start+2], ...) can result in A
// representing a power of 2, return true.
if (permutations(A, start + 1)) {
return true;
}
// Restore the array to the state it was in before
// A[start] was placed with value A[i].
swap(A, start, i);
}
return false;
}
public void swap(int[] A, int i, int j) {
int t = A[i];
A[i] = A[j];
A[j] = t;
}
}
网友评论