Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/com/examplehub/leetcode/easy/PathSumII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.examplehub.leetcode.easy;

import com.examplehub.leetcode.TreeNode;
import com.sun.source.tree.Tree;

import java.util.ArrayList;
import java.util.List;

/**
* https://leetcode.com/problems/path-sum-ii/
*/
public class PathSumII {
private static List<List<Integer>> result = new ArrayList<>();
private static List<Integer> path = new ArrayList<>();
public static List<List<Integer>> doSolution1(TreeNode root, int targetNum) {
deepFirstSearch(root, targetNum);
return result;
}

private static void deepFirstSearch(TreeNode root, int targetNum) {
if (root == null) {
return;
}
path.add(root.val);
targetNum -= root.val;
if (root.left == null && root.right == null && targetNum == 0) {
result.add(new ArrayList<>(path));
}
deepFirstSearch(root.left, targetNum);
deepFirstSearch(root.right, targetNum);
path.remove(path.size() - 1);
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/examplehub/leetcode/middle/ValidSudoku.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.examplehub.leetcode.middle;

/**
* https://leetcode.com/problems/valid-sudoku/
*/
public class ValidSudoku {
public static boolean solution1(char[][] board) {

//travel all rows to check
for (int i = 0; i < 9; i++) {
int[] digitTable = new int[10];
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
digitTable[board[i][j] - '0']++;
}
}
if (containsRepeat(digitTable)) {
return false;
}
}

//travel all columns to check
for (int i = 0; i < 9; i++) {
int[] digitTable = new int[10];
for (int j = 0; j < 9; j++) {
if (board[j][i] != '.') {
digitTable[board[j][i] - '0']++;
}
}
if (containsRepeat(digitTable)) {
return false;
}
}

//travel all sub-box
//TODO
return true;
}

public static boolean containsRepeat(int[] table) {
for (int num : table) {
if (num > 1) {
return true;
}
}
return false;
}

public static boolean solution2(char[][] board) {
int[][] rows = new int[9][9];
int[][] cols = new int[9][9];
int[][][] subBoxes = new int[3][3][9];
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (board[i][j] != '.') {
int index = board[i][j] - '1';
rows[i][index]++;
cols[j][index]++;
subBoxes[i / 3][j / 3][index]++;
if (rows[i][index] > 1 || cols[j][index] > 1 || subBoxes[i / 3][j / 3][index] > 1) {
return false;
}
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.examplehub.searches;

public class LinearSearchOptimization implements Search {

/**
* Linear search algorithm.
*
* @param numbers the numbers to be searched.
* @param key the key value t o be searched.
* @return index of {@code key} value if found, otherwise -1.
*/
@Override
public int search(int[] numbers, int key) {
if (numbers.length == 0) {
return -1;
}
if (key == numbers[0]) {
return 0;
}
numbers[0] = key;
int index = numbers.length - 1;
while (key != numbers[index]) {
index--;
}
return index == 0 ? -1 : index;
}

/**
* Generic linear search algorithm.
*
* @param array the array to be searched.
* @param key the key object to be searched.
* @param <T> the class of the objects in the array.
* @return index of {@code key} if found, otherwise -1.
*/
@Override
public <T extends Comparable<T>> int search(T[] array, T key) {
if (array.length == 0) {
return -1;
}
if (key.compareTo(array[0]) == 0) {
return 0;
}
array[0] = key;
int index = array.length - 1;
while (key.compareTo(array[index]) != 0) {
index--;
}
return index == 0 ? -1 : index;
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/examplehub/sorts/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class BubbleSort implements Sort {
* @param numbers the numbers to be sorted.
*/
public void sort(int[] numbers) {
for (int i = 0; i < numbers.length - 1; ++i) {
for (int i = 1; i < numbers.length; ++i) {
boolean swapped = false;
for (int j = 0; j < numbers.length - 1 - i; ++j) {
for (int j = 0; j < numbers.length - i; ++j) {
if (numbers[j] > numbers[j + 1]) {
SortUtils.swap(numbers, j, j + 1);
swapped = true;
Expand All @@ -31,9 +31,9 @@ public void sort(int[] numbers) {
* @param <T> the class of the objects in the array.
*/
public <T extends Comparable<T>> void sort(T[] array) {
for (int i = 0; i < array.length - 1; ++i) {
for (int i = 1; i < array.length; ++i) {
boolean swapped = false;
for (int j = 0; j < array.length - 1 - i; ++j) {
for (int j = 0; j < array.length - i; ++j) {
if (array[j].compareTo(array[j + 1]) > 0) {
SortUtils.swap(array, j, j + 1);
swapped = true;
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/examplehub/leetcode/easy/PathSumIITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.examplehub.leetcode.easy;

import com.examplehub.leetcode.TreeNode;
import com.examplehub.leetcode.utils.BinaryTreeUtils;
import org.junit.jupiter.api.Test;

class PathSumIITest {
@Test
void testSolution1() {
TreeNode root = BinaryTreeUtils.createTree(new int[]{5, 4, 8, 11, 0, 13, 4, 7, 2, 0, 0, 5, 1}, 0);
//TODO
}
}
66 changes: 66 additions & 0 deletions src/test/java/com/examplehub/leetcode/middle/ValidSudokuTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.examplehub.leetcode.middle;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ValidSudokuTest {

@Test
void testSolution1() {
char[][] board = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
};
assertTrue(ValidSudoku.solution1(board));

board = new char[][]{
{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
};
assertFalse(ValidSudoku.solution1(board));
}

@Test
void testSolution2() {
char[][] board = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
};
assertTrue(ValidSudoku.solution2(board));

board = new char[][]{
{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
};
assertFalse(ValidSudoku.solution2(board));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.examplehub.searches;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.assertEquals;

class LinearSearchOptimizationTest {

private Search search;

@BeforeEach
void setup() {
search = new LinearSearchOptimization();
}

@Test
void testLinearSearch() {
int[] ints = IntStream.range(0, 10).toArray();
for (int i = 0; i < ints.length; ++i) {
assertEquals(i, search.search(ints, i));
}

assertEquals(-1, search.search(ints, 10));
assertEquals(-1, search.search(ints, 100));
assertEquals(-1, search.search(ints, -1));

String[] strings = {"abc", "123", "ABC", "ExampleHub", "Java"};
for (int i = 0; i < strings.length; ++i) {
assertEquals(i, search.search(strings, strings[i]));
}
assertEquals(-1, search.search(strings, "Windows"));
assertEquals(-1, search.search(strings, "321"));
assertEquals(-1, search.search(strings, ""));
}
}