FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

linear search optimization by realDuYuanChao · Pull Request #188 · examplehub/Java · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (5) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or 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
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);
}
}
This file contains hidden or 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
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
This file contains hidden or 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
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
This file contains hidden or 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
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
}
}
This file contains hidden or 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
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, ""));
}
}

Back | FazBrowse Home | New Git URL