Array Puzzle
int j, temp = 0, n = array.length / 2;
for (int i = 1; i < array.length - 1; i++) {
temp = 0;
j = i ;
j = n + (j/((j^(j-1))+1));
while (j < i) {
j = n + (j/((j^(j-1))+1));
}
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
package com.onezero;
/**
* change array {a1,a2,...an,b1,b2,...bn}
* to {a1,b1,a2,b2,...,an,bn}
* using O(n) time and O(1) space
*
* @author andyjojo
*
*/
public class ArrayPuzzle1 {
int [] array;
public ArrayPuzzle1(int[] arr){
array = arr;
}
/**
* from 1 to array.length - 1
* calculate result array i-th location
*
*/
public void change1() {
int j, temp = 0, n = array.length / 2;
for (int i = 1; i < array.length - 1; i++) {
temp = 0;
j = i + 1;
while (j % 2 == 1)
j = (j + 1) / 2;
j = n + j / 2 - 1;
while (j < i) {
j++;
while (j % 2 == 1)
j = (j + 1) / 2;
j = n + j / 2 - 1;
}
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
/**
* The magic calculation of {@link#change1()}
*
*/
public void change2(){
int j, temp = 0, n = array.length / 2;
for (int i = 1; i < array.length - 1; i++) {
temp = 0;
j = i ;
j = n + (j/((j^(j-1))+1));
while (j < i) {
j = n + (j/((j^(j-1))+1));
}
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
/**
* find in http://discuss.techinterview.org/default.asp?interview.11.482833.2
* it not work, need to check what it work for.
*/
/*public void changeother(){
int n = array.length/2;
for(int i=0;i<n;++i)
{
array[i]^=array[n+i]^=array[i]^=array[n+i];
}
}*/
/**
* print the array
*/
public void print() {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
/**
* compares with a array
* @param b array b
* @return true when array b is the same with array, false otherwise
*/
public boolean compare(int[] b) {
if(array.length!=b.length) return false;
for (int i = 0; i < array.length; i++) {
if (array[i] != b[i])
return false;
}
return true;
}
/**
* construct a 2n element array {array[i]=i}
* aka. ai=i, bi=n+i
* @param n the half number of array length
* @return a 2n element array {ai=i}
*/
public static int[] constructOrignal(int n) {
int[] array = new int[n * 2];
for (int i = 0; i < 2*n; i++) {
array[i] = i;
}
return array;
}
/**
* construct the result of this puzzle of array {array[i]=i}
* @param n the half number of array length
* @return the result of this puzzle of array {array[i]=i}
*/
public static int[] constructResult(int n) {
int[] array = new int[n * 2];
for (int i = 0; i < n; i++) {
array[2 * i] = i;
array[2 * i + 1] = n + i;
}
return array;
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayPuzzle1 ap;
for (int i = 1; i < 9999; i++) {
ap = new ArrayPuzzle1(constructOrignal(i));
ap.change2();//the same with ap.change1()
if (!ap.compare(constructResult(i)))
System.out.println("Fail on " + i);
}
}
}
설명(내용을 거꾸로 선택하면 코드를 보고 볼 수 있습니다. 당신의 의견을 받기를 바랍니다. 감사합니다):
1과 2n의 위치는 j를 2에서 2n-1로 바꾸지 않아도 된다. 만약에 j가 짝수라면 n+j/2와 직접 교환하면 된다. 만약에 j가 홀수라면 a[(j+1)/2]를 놓으면 된다. 그러면 문제는 a[(j+1)/2]의 현재 위치를 찾아야 한다. (j+1)/2
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JS 동적 추가 삭제 테이블텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.