Practice1
2차원 배열 열로 정렬하기 :
public static void main(String[] args) {
int [][] inputArray = {{4,5,6,1},{7,8,9,2},{1,42,2,3},{5,2,5,1}};
System.out.println(Arrays.deepToString(orderByColumns(inputArray)));
}
private static int[][] orderByColumns(int[][] inputArray) {
int[][] resultArray = new int[inputArray.length][inputArray[0].length];
ArrayList<int[]> list = new ArrayList<int[]>();
for(int inx=0; inx<inputArray.length; inx++) {
Arrays.sort(inputArray[inx]);
list.add(inputArray[inx]);
}
for(int inx=0; inx<inputArray.length; inx++) {
for(int jnx=0; jnx<inputArray[inx].length; jnx++) {
resultArray[inx][jnx] = list.get(inx)[jnx];
}
}
//result will be : [[1, 4, 5, 6], [2, 7, 8, 9], [1, 2, 3, 42], [1, 2, 5, 5]]
return resultArray;
}
Practice2
2차원 배열 행으로 정렬하기 :
private static int[][] orderByRows(int[][] inputArray) {
int [][] resultArray = new int[inputArray.length][inputArray[0].length];
int [][] reorderedArray = new int[inputArray.length][inputArray[0].length];
ArrayList<int[]> list = new ArrayList<int[]>();
for(int inx=0; inx<inputArray.length; inx++) {
for(int jnx=0; jnx<inputArray[inx].length; jnx++) {
reorderedArray[inx][jnx] = inputArray[jnx][inx];
}
Arrays.sort(reorderedArray[inx]);
list.add(reorderedArray[inx]);
}
for(int inx=0; inx<inputArray.length; inx++) {
for(int jnx=0; jnx<inputArray[inx].length; jnx++) {
resultArray[inx][jnx] = list.get(jnx)[inx];
}
}
return resultArray;
}
Practice3
initialize 2d array like above :
a | b | c | d | e | ||||
f | h | s | d | w |
String inputData = "abcde, fhsdw"
private void solution(String inputData) {
String [] strArray = inputData.split(",");
char [] charArr1 = strArray[0].toCharArray();
char [] charArr2 = strArray[1].toCharArray();
int totLength = charArr1.length + charArr1.length -1;
char[][] initArray = initArray(charArr1, charArr2, totLength);
}
private char[][] initArray(char[] charArr1, char[] charArr2, int totLength) {
char [][] initArray = new char[2][totLength];
int pos = 0;
for(int inx=0; inx<initArray.length; inx++) {
for(int jnx=0; jnx< totLength; jnx++) {
if(inx == 0 && charArr1.length > jnx) {
initArray[inx][jnx] = charArr1[jnx];
pos++;
} else if(inx == 1 && charArr2.length > jnx) {
initArray[1][pos -1] = charArr2[jnx];
pos++;
}
}
}
System.out.println(Arrays.deepToString(initArray));
return initArray;
}
Practice4
Move(shift) Array 1step Forward
private void solution(String inputData) {
String [] strArray = inputData.split(",");
char [] charArr1 = strArray[0].toCharArray();
char [] charArr2 = strArray[1].toCharArray();
int totLength = charArr1.length + charArr1.length -1;
char[][] initArray = initArray(charArr1, charArr2, totLength);
//move 1st array 1step forward
int moveStep = totLength - charArr1.length;
char [] modifiedArray = new char[totLength];
for(int inx=0; inx<moveStep ; inx++) {
modifiedArray = moveOneStepFwd(initArray[0]);
System.out.println(Arrays.toString(modifiedArray));
}
}
private char[] moveOneStepFwd(char[] charArr1) {
// 오른쪽으로 한 칸 shift
char temp = charArr1[charArr1.length - 1];
for(int inx = charArr1.length-1 ; inx > 0 ; inx--) {
charArr1[inx] = charArr1[inx-1];
}
charArr1[0] = temp;
return charArr1;
}
Practice5
Move(shift) Array 1step Backward
private void solution(String inputData) {
String [] strArray = inputData.split(",");
char [] charArr1 = strArray[0].toCharArray();
char [] charArr2 = strArray[1].toCharArray();
int totLength = charArr1.length + charArr1.length -1;
char[][] initArray = initArray(charArr1, charArr2, totLength);
//move 2nd array 1step backward
int moveStep2 = totLength - charArr2.length;
char [] modifiedArray2 = new char[totLength];
for(int inx=0; inx<moveStep2 ; inx++) {
modifiedArray2 = moveOneStepBwd(initArray[1]);
System.out.println(Arrays.toString(modifiedArray2));
}
}
private char[] moveOneStepBwd(char[] charArr2) {
// 왼쪽으로 한 칸 shift
char temp = charArr2[0];
for(int inx=0; inx<charArr2.length - 1; inx++) {
charArr2[inx] = charArr2[inx+1];
}
charArr2[charArr2.length-1] = temp;
return charArr2;
}
Practice6
With similarity counter
private void solution(String inputData) {
String [] strArray = inputData.split(",");
char [] charArr1 = strArray[0].toCharArray();
char [] charArr2 = strArray[1].toCharArray();
int totLength = charArr1.length + charArr1.length -1;
char[][] initArray = initArray(charArr1, charArr2, totLength);
//move 1st array 1step forward
int moveStep = totLength - charArr1.length;
char [] modifiedArray = new char[totLength];
for(int inx=0; inx<moveStep ; inx++) {
modifiedArray = moveOneStepFwd(initArray[0]);
System.out.println(Arrays.toString(modifiedArray));
}
//move 2nd array 1step backward
int moveStep2 = totLength - charArr2.length;
char [] modifiedArray2 = new char[totLength];
for(int inx=0; inx<moveStep2 ; inx++) {
modifiedArray2 = moveOneStepBwd(initArray[1]);
System.out.println(Arrays.toString(modifiedArray2));
}
//count similarity for 1st and 2nd array
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(countSimilarity(modifiedArray, modifiedArray2));
System.out.println(list);
}
private int countSimilarity(char[] inputArray1, char[] inputArray2) {
int similarity = 0;
for(int inx=0; inx<inputArray1.length; inx++) {
if(inputArray1[inx] == inputArray2[inx] && inputArray1[inx] != 0 ) {
similarity++;
}
}
return similarity;
}
'Java Programming' 카테고리의 다른 글
Java algorithm practice level2 coding1 (0) | 2019.05.09 |
---|---|
Java algorithm practice level1 coding3 (0) | 2019.04.28 |
Java algorithm practice level1 coding2 (0) | 2019.04.28 |
Java algorithm practice level1 coding1 (0) | 2019.04.06 |