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 deleted file mode 100644 index 4f8d442b02..0000000000 --- a/src/main/java/com/fishercoder/solutions/_659.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.fishercoder.solutions; - -import java.util.*; - -/** - * 659. Split Array into Consecutive Subsequences - * - * You are given an integer array sorted in ascending order (may contain duplicates), - * you need to split them into several subsequences, - * where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. - - Example 1: - Input: [1,2,3,3,4,5] - Output: True - Explanation: - You can split them into two consecutive subsequences : - 1, 2, 3 - 3, 4, 5 - - Example 2: - Input: [1,2,3,3,4,4,5,5] - Output: True - Explanation: - You can split them into two consecutive subsequences : - 1, 2, 3, 4, 5 - 3, 4, 5 - - Example 3: - Input: [1,2,3,4,4,5] - Output: False - - Note: - The length of the input is in range of [1, 10000] - */ - -public class _659 { - - /**reference: https://discuss.leetcode.com/topic/99187/java-o-n-time-o-n-space*/ - public boolean isPossible(int[] nums) { - Map freqMap = new HashMap<>(); - for (int i : nums) { - freqMap.put(i, freqMap.getOrDefault(i, 0) + 1); - } - Map appendFreqMap = new HashMap<>(); - for (int i : nums) { - if (freqMap.get(i) == 0) { - continue; - } else if (appendFreqMap.getOrDefault(i, 0) > 0) { - appendFreqMap.put(i, appendFreqMap.get(i) - 1); - appendFreqMap.put(i+1, appendFreqMap.getOrDefault(i+1, 0) + 1); - } else if (freqMap.getOrDefault(i+1, 0) > 0 && freqMap.getOrDefault(i+2, 0) > 0) { - freqMap.put(i+1, freqMap.get(i+1) - 1); - freqMap.put(i+2, freqMap.get(i+2) - 1); - appendFreqMap.put(i+3, appendFreqMap.getOrDefault(i+3, 0) + 1); - } else return false; - freqMap.put(i, freqMap.get(i) - 1); - } - return true; - } - -}