diff --git a/src/main/java/com/fishercoder/solutions/_150.java b/src/main/java/com/fishercoder/solutions/_150.java index c8e7abfbd9..c03cc3bd49 100644 --- a/src/main/java/com/fishercoder/solutions/_150.java +++ b/src/main/java/com/fishercoder/solutions/_150.java @@ -62,4 +62,30 @@ public int evalRPN(String[] tokens) { return Integer.parseInt(stack.pop()); } + //using one stack only. + public int evalRPN1(String[] tokens) { + Stack stack = new Stack(); + for (int i = 0; i < tokens.length; i++) { + if (tokens[i].equals("+")) { + stack.push(stack.pop() + stack.pop()); + } + else if (tokens[i].equals("-")) { + stack.push(-stack.pop() + stack.pop()); + } + else if (tokens[i].equals("*") ) { + stack.push(stack.pop() * stack.pop()); + + } + else if (tokens[i].equals("/")) { + int a = stack.pop(); + int b = stack.pop(); + stack.push(b / a); + + } else { + stack.push(Integer.parseInt (tokens[i])); + } + } + return stack.pop(); + } + } diff --git a/src/main/java/com/fishercoder/solutions/_36.java b/src/main/java/com/fishercoder/solutions/_36.java index e9eac998a8..a8d52374da 100644 --- a/src/main/java/com/fishercoder/solutions/_36.java +++ b/src/main/java/com/fishercoder/solutions/_36.java @@ -1,5 +1,5 @@ package com.fishercoder.solutions; - +import java.util.*; /**Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. @@ -84,6 +84,21 @@ boolean isValidSquare(char[][] board, int row, int col) { return true; } + // the 3sets version + public boolean isValidSudoku1(char[][] board) { + for(int i = 0; i < 9; i++) { + Set rows = new HashSet<>(); + Set cols = new HashSet<>(); + Set cube = new HashSet<>(); + for (int j = 0;j < 9; j++) { + if(board[i][j] != '.' && ! rows.add(board[i][j])) return false; + if(board[j][i] != '.' && ! cols.add(board[j][i])) return false; + if(board[3*(i/3) + j/3][3*(i%3) + j % 3] != '.' && !cube.add(board[3*(i/3) + j/3][3*(i%3) + j % 3] )) return false; + } + } + return true; +} + public static void main(String... strings) { _36 test = new _36(); // char[][] board = new char[][]{ @@ -126,5 +141,6 @@ public static void main(String... strings) { // ["....5..1.",".4.3.....",".....3..1","8......2.","..2.7....",".15......",".....2...",".2.9.....","..4......"] System.out.println(test.isValidSudoku(board)); + System.out.println(test.isValidSudoku1(board)); } } diff --git a/src/main/java/com/fishercoder/solutions/_659.java b/src/main/java/com/fishercoder/solutions/_659.java new file mode 100644 index 0000000000..04eee49b1e --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_659.java @@ -0,0 +1,4 @@ +package com.fishercoder.solutions; + +public class _659 { +}