JP 3xk + 日本妹
vs HK 3xk + 無日本妹
盲都識揀
[quote][quote][quote][quote]```
public class Solution {
public int[] powerSet;
public int[] solution(int[] A) {
int length = A.length;
if (length == 0)
return A;
powerSet = new int[length];
int sum = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] == 1) {
int power = pow(-2, i);
powerSet[i] = power;
sum += power;
}
}
int ceil;
if (sum % 2 == 1 && sum > 0) {
ceil = sum / 2 + 1;
} else {
ceil = sum / 2;
}
int index = getLeadingIndex(ceil);
int[] array = new int[index+1];
array[index] = 1;
ceil -= powerSet[index];
while (ceil != 0) {
index = getLeadingIndex(ceil);
array[index] = 1;
ceil -= powerSet[index];
}
return array;
}
public int pow(int base, int power) {
int number = 1;
while (power-- > 0) {
number *= base;
}
return number;
}
public int getLeadingIndex (int number) {
int index = 0;
int sum = 0;
if (number > 0) {
index = -2;
while (sum < number) {
index += 2;
sum += powerSet[index];
}
} else if (number < 0) {
index = -1;
while (sum > number) {
index += 2;
sum += powerSet[index];
}
}
return index;
}
}
咁做唔知得唔得
將A向左sift一格, 出 B =-floor(A)
e.g. let A = 101101
B = A = 101111 => 01101
之後將floor轉ceil同乘-1, 出C = ceil(A)
C = array of zero
B[0] -=A[0]
if B[0] = -1:
C[0] = 1
for i in range (len(B)):
if B[i]=1:
C[i]++, C[i+1]++
e.g. C = 000000 => 100000 => 111000 => 112100 => 112111
p.s. C[i] = 0, 1 or 2, |C[i]-C[i+1]|<=1
進位
for i in range (len(C)) :
if C[i] =2:
C[i] = 0, C[i+1] --
e.g. C = 112111 => 110011
public int[] solution(int[] A) {
if (A.length <= 1) {
return A;
}
int[] B = new int[A.length];
for (int i = 0, j = 1; i < A.length; i++, j++) {
if (j == A.length) {
B[i] = A[i];
} else if (A[i] != A[j]) {
// 0 1 1 0
// 1 ? or 0 ?
B[i] = 1;
} else if (A[i] == 0) {
// 0 0
// 0 ?
B[i] = 0;
} else {
B[i] = 0;
if (j + 1 == A.length || A[j + 1] == 0) {
// 1 1
// 1 0
B[j] = 0;
} else {
// 1 1
// 1 1
B[j] = 1;
}
i++;
j++;
}
}
int effectiveIndex = 0;
for (int i = B.length - 1; i >= 0; i--) {
if (B[i] == 1) {
effectiveIndex = i;
break;
}
}
return Arrays.copyOf(B, effectiveIndex + 1);
}