Created
September 26, 2015 22:39
-
-
Save nickwph/b2b6b15b316a9c11631d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Solution1 { | |
public static void main(String[] args) { | |
try { | |
// read in matrix | |
Scanner scanner = new Scanner(System.in); | |
int length = Integer.parseInt(scanner.nextLine()); | |
int[][] matrix = new int[length][length]; | |
for (int i = 0; i < length; i++) { | |
matrix[i] = new int[length]; | |
String line = scanner.nextLine(); | |
String[] stringArray = line.split(" "); | |
if (stringArray.length != length) { | |
// detect bad number of columns | |
throw new IllegalAccessError(); | |
} | |
for (int j = 0; j < length; j++) { | |
matrix[i][j] = Integer.parseInt(stringArray[j]); | |
} | |
} | |
// print matrix | |
for (int i = 0; i < length; i++) { | |
for (int j = 0; j < length; j++) { | |
System.out.print(matrix[length - j - 1][i] + " "); | |
} | |
System.out.println(); | |
} | |
} catch (Exception e) { | |
// found error inputs | |
System.out.println("ERROR"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Solution2 { | |
public static void main(String[] args) { | |
try { | |
// read in matrix | |
// same as question 1 | |
Scanner scanner = new Scanner(System.in); | |
int length = Integer.parseInt(scanner.nextLine()); | |
int[][] matrix = new int[length][length]; | |
for (int i = 0; i < length; i++) { | |
matrix[i] = new int[length]; | |
String line = scanner.nextLine(); | |
String[] stringArray = line.split(" "); | |
if (stringArray.length != length) { | |
// detect bad number of columns | |
throw new IllegalAccessError(); | |
} | |
for (int j = 0; j < length; j++) { | |
matrix[i][j] = Integer.parseInt(stringArray[j]); | |
} | |
} | |
int k = Integer.parseInt(scanner.nextLine()); | |
// check for duplicates | |
for (int x = 0; x < length; x++) { | |
for (int y = 0; y < length; y++) { | |
for (int compareX = x - k - 1; compareX < x + k + 1; compareX++) { | |
for (int compareY = y - k - 1; compareY < y + k + 1; compareY++) { | |
if (compareY == y && compareX == x || compareX < 0 || compareX >= length || compareY < 0 || compareY >= length) { | |
continue; | |
} | |
// System.out.println("Comparing... " + x + "," + y + " " + compareX + "," + compareY); | |
if (matrix[x][y] == matrix[compareX][compareY]) { | |
// found duplicated | |
System.out.println("YES"); | |
return; | |
} | |
} | |
} | |
} | |
} | |
// no duplicates | |
System.out.println("NO"); | |
} catch (Exception e) { | |
// show error | |
System.out.println("ERROR"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment