Compare commits
42 Commits
95ea49c5a0
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| bd860423c4 | |||
| dfda3d7f08 | |||
| 042fbd164a | |||
| c6b68cd40e | |||
| fd72f740eb | |||
| 9a98ed3770 | |||
| 9eba905fc0 | |||
| 0295bde18c | |||
| 4b6f4dc631 | |||
| 6ca0b464a8 | |||
| a3f5a177a5 | |||
| feb1f5fcad | |||
| b5059f6996 | |||
| 022850f76c | |||
| 35e5acb50f | |||
| 4f478c08ac | |||
| 7827051d82 | |||
| 664b4478d7 | |||
| 922c779457 | |||
| b415741eb0 | |||
| 67d4fa3983 | |||
| abd073c2b5 | |||
| c664823f9c | |||
| 2c2ee20dd2 | |||
| 45b40688c7 | |||
| 438abeca95 | |||
| 6fb29ad89e | |||
| 1d77960011 | |||
| 732d006f3b | |||
| e27cb1fb87 | |||
| d589c45e45 | |||
| 97eb4196e9 | |||
| 23dc31f9ae | |||
| a4fd3e2a15 | |||
| 5d29f810a2 | |||
| 2628f99a31 | |||
| 27074cb4c6 | |||
| 5a0d46a2d9 | |||
| 6c39c4d462 | |||
| b75d236fbb | |||
| c72996f342 | |||
| 01f220bacf |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,7 @@
|
|||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
### IntelliJ IDEA ###
|
### IntelliJ IDEA ###
|
||||||
out/
|
out/
|
||||||
|
/target/
|
||||||
!**/src/main/**/out/
|
!**/src/main/**/out/
|
||||||
!**/src/test/**/out/
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
|||||||
37
pom.xml
Normal file
37
pom.xml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.nbee</groupId>
|
||||||
|
<artifactId>solution</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.8.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>17</source>
|
||||||
|
<target>17</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
141
src/main/java/com/nbee/solution/practice/BinarySearch.java
Normal file
141
src/main/java/com/nbee/solution/practice/BinarySearch.java
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
class BinarySearch {
|
||||||
|
public static int binarySearchBasic(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length - 1;//设置指针和初始值
|
||||||
|
while (i <= j) {//此处的条件是i<=j,而不是i<j,
|
||||||
|
// int m = ((i + j) / 2);
|
||||||
|
int m = (i + j) >>> 1; //按位右移补零操作符。相当于除2,解决溢出问题
|
||||||
|
if (target < nums[m]) {//如果中间值大于目标值,则将右指针向左移动
|
||||||
|
j = m - 1;
|
||||||
|
} else if (nums[m] < target) {//如果中间值小于目标值,则将左指针向右移动
|
||||||
|
i = m + 1;
|
||||||
|
} else {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
/**
|
||||||
|
* 为什么必须是i<=j?
|
||||||
|
* 在二分搜索中,使用条件 i <= j 作为循环的继续条件是为了确保在数组只剩下一个元素时,仍然能够检查这个元素。这是因为在二分搜索中
|
||||||
|
* 你每次都在缩小搜索范围,直到没有元素剩余。如果用 i < j 作为条件,当 i 和 j 相等时,这个位置的元素将不会被检查,可能导致漏掉目标元素。
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int binarySearchAlternative(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length;
|
||||||
|
while (i < j) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m;
|
||||||
|
} else if (nums[m] < target) {
|
||||||
|
i = m + 1;
|
||||||
|
} else {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int binarySearch01(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length;
|
||||||
|
while (1 < j - i) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m;
|
||||||
|
} else {
|
||||||
|
i = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nums[i] == target) {
|
||||||
|
return i;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int binarySearchLeftMost(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length - 1;
|
||||||
|
int candidate = -1;
|
||||||
|
while (i <= j) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m - 1;
|
||||||
|
} else if (nums[m] < target) {
|
||||||
|
i = m + 1;
|
||||||
|
} else {
|
||||||
|
candidate = m;
|
||||||
|
j = m - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int binarySearchRightMost(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length - 1;
|
||||||
|
int candidate = -1;
|
||||||
|
while (i <= j) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m - 1;
|
||||||
|
} else if (nums[m] < target) {
|
||||||
|
i = m + 1;
|
||||||
|
} else {
|
||||||
|
candidate = m;
|
||||||
|
i = m + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
//改进二分查找
|
||||||
|
public static int binarySearchLeftMost0(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length - 1;
|
||||||
|
while (i <= j) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m - 1;
|
||||||
|
} else {
|
||||||
|
i = m + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int binarySearchRightMost0(int[] nums, int target) {
|
||||||
|
int i = 0, j = nums.length - 1;
|
||||||
|
while (i <= j) {
|
||||||
|
int m = (i + j) >>> 1;
|
||||||
|
if (target < nums[m]) {
|
||||||
|
j = m - 1;
|
||||||
|
} else {
|
||||||
|
i = m + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。
|
||||||
|
* 如果数组中不存在目标值 target,返回 [-1, -1]。
|
||||||
|
* 你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
|
||||||
|
*
|
||||||
|
* @param nums
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int[] searchRange(int[] nums, int target) {
|
||||||
|
if (nums.length == 0){
|
||||||
|
return new int[]{-1,-1};
|
||||||
|
}
|
||||||
|
if (nums.length == 1){
|
||||||
|
return new int[]{0,0};
|
||||||
|
}
|
||||||
|
int left = binarySearchLeftMost(nums, target);
|
||||||
|
int right = binarySearchRightMost(nums,target);
|
||||||
|
return new int[]{left,right};
|
||||||
|
}
|
||||||
|
}
|
||||||
91
src/main/java/com/nbee/solution/practice/DynamicArray.java
Normal file
91
src/main/java/com/nbee/solution/practice/DynamicArray.java
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
public class DynamicArray implements Iterable<Integer> {
|
||||||
|
private int size = 0;
|
||||||
|
private int capacity = 8;
|
||||||
|
private int[] array = {};
|
||||||
|
|
||||||
|
public void addList(int element) {
|
||||||
|
add(size, element);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(int index, int element) {
|
||||||
|
checkAndGrow();
|
||||||
|
if (index < 0 || index > size) {
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
if (index >= 0 && index < size) {
|
||||||
|
System.arraycopy(array, index, array, index + 1, size - index);
|
||||||
|
}
|
||||||
|
array[index] = element;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkAndGrow() {
|
||||||
|
if (size == 0) {
|
||||||
|
array = new int[capacity];
|
||||||
|
} else if (size == capacity) {
|
||||||
|
capacity = capacity + (capacity >>> 1);
|
||||||
|
int[] newArray = new int[capacity];
|
||||||
|
System.arraycopy(array, 0, newArray, 0, size);
|
||||||
|
array = newArray;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int get(int index) {
|
||||||
|
if (index < 0 || index >= size) {
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
return array[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void forEach0(Consumer<Integer> consumer) {
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
consumer.accept(array[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Integer> iterator() {
|
||||||
|
return new Iterator<Integer>() {
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return i < size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer next() {
|
||||||
|
return array[i++];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntStream stream() {
|
||||||
|
return IntStream.of(Arrays.copyOfRange(array, 0, size));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int remove(int index) {
|
||||||
|
int removed = array[index];
|
||||||
|
if (index < size - 1) {
|
||||||
|
System.arraycopy(array, index + 1, array, index, size - index - 1);
|
||||||
|
size--;
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
public String toString() {
|
||||||
|
return Arrays.toString(Arrays.copyOfRange(array, 0, size));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 链表类
|
||||||
|
*/
|
||||||
|
public class SinglyLinkedList implements Iterable<Integer> {
|
||||||
|
private Node head;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Integer> iterator() {
|
||||||
|
return new Iterator<Integer>() {
|
||||||
|
Node p = head;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return p != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer next() {
|
||||||
|
int value = p.value;
|
||||||
|
p = p.next;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private Node findLast() {
|
||||||
|
if (head == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Node p;
|
||||||
|
for (p = head; p != null; p = p.next) {
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addLast(int value) {
|
||||||
|
Node last = findLast();
|
||||||
|
if (last == null){
|
||||||
|
addFirst(value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
last.next = new Node(value, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Node {
|
||||||
|
int value;
|
||||||
|
Node next;
|
||||||
|
|
||||||
|
Node(int value, Node next) {
|
||||||
|
this.value = value;
|
||||||
|
this.next = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addFirst(int value) {
|
||||||
|
//链表为空
|
||||||
|
// head = new Node(value,null);
|
||||||
|
//链表非空,包含了链表为空的情况
|
||||||
|
head = new Node(value, head);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loop(Consumer<Integer> consumer) {
|
||||||
|
Node p = head;
|
||||||
|
while (p != null) {
|
||||||
|
consumer.accept(p.value);
|
||||||
|
p = p.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loop0(Consumer<Integer> consumer) {
|
||||||
|
for (Node p = head; p != null; p = p.next) {
|
||||||
|
consumer.accept(p.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
20
src/main/java/com/nbee/solution/solution/ListNode.java
Normal file
20
src/main/java/com/nbee/solution/solution/ListNode.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.nbee.solution.solution;
|
||||||
|
|
||||||
|
public class ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode next;
|
||||||
|
|
||||||
|
ListNode() {
|
||||||
|
}
|
||||||
|
|
||||||
|
ListNode(int val) {
|
||||||
|
this.val = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
ListNode(int val, ListNode next) {
|
||||||
|
this.val = val;
|
||||||
|
this.next = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package solution;
|
package com.nbee.solution.solution;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -204,4 +204,202 @@ public class Solution {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
|
||||||
|
* @param list1
|
||||||
|
* @param list2
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
|
||||||
|
ListNode dummy = new ListNode(0);
|
||||||
|
ListNode current = dummy;
|
||||||
|
while (list1 != null && list2 != null) {
|
||||||
|
if (list1.val < list2.val) {
|
||||||
|
current.next = list1;
|
||||||
|
list1 = list1.next;
|
||||||
|
} else {
|
||||||
|
current.next = list2;
|
||||||
|
list2 = list2.next;
|
||||||
|
}
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
if (list1 != null){
|
||||||
|
current.next = list1;
|
||||||
|
} else if (list2 != null){
|
||||||
|
current.next = list2;
|
||||||
|
}
|
||||||
|
return dummy.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。
|
||||||
|
* @param haystack
|
||||||
|
* @param needle
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
|
||||||
|
public int strStr(String haystack, String needle) {
|
||||||
|
if (needle.isEmpty()) return -1;
|
||||||
|
return haystack.indexOf(needle);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
|
||||||
|
*
|
||||||
|
* 请必须使用时间复杂度为 O(log n) 的算法。
|
||||||
|
* @param nums
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int searchInsert(int[] nums, int target) {
|
||||||
|
if (nums.length == 0) return 0;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
if (nums[i] >= target) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nums.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。
|
||||||
|
*
|
||||||
|
* 单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
|
||||||
|
* @param s
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int lengthOfLastWord(String s) {
|
||||||
|
if (s.isEmpty()) return 0;
|
||||||
|
for (int i = s.length() - 1; i >= 0; i--) {
|
||||||
|
if (s.charAt(i) != ' ') {
|
||||||
|
int count = 0;
|
||||||
|
while (i >= 0 && s.charAt(i) != ' ') {
|
||||||
|
count++;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。
|
||||||
|
*
|
||||||
|
* 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
|
||||||
|
*
|
||||||
|
* 你可以假设除了整数 0 之外,这个整数不会以零开头。
|
||||||
|
* @param digits
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int[] plusOne(int[] digits) {
|
||||||
|
int n = digits.length;
|
||||||
|
|
||||||
|
// 从数组的最后一位开始处理进位
|
||||||
|
for (int i = n - 1; i >= 0; i--) {
|
||||||
|
if (digits[i] < 9) {
|
||||||
|
digits[i]++;
|
||||||
|
return digits;
|
||||||
|
}
|
||||||
|
// 如果当前位是9,则将其置为0
|
||||||
|
digits[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果所有的数字都是9,那么会到这里
|
||||||
|
// 创建一个新的数组,其长度比原数组多1
|
||||||
|
int[] newNumber = new int[n + 1];
|
||||||
|
newNumber[0] = 1;
|
||||||
|
|
||||||
|
return newNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给你两个二进制字符串 a 和 b ,以二进制字符串的形式返回它们的和。
|
||||||
|
*
|
||||||
|
* 输入为 非空 字符串且只包含数字 1 和 0。
|
||||||
|
* @param a
|
||||||
|
* @param b
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String addBinary(String a, String b) {
|
||||||
|
StringBuilder result = new StringBuilder(); // 用于存储结果
|
||||||
|
int carry = 0; // 进位
|
||||||
|
int i = a.length() - 1, j = b.length() - 1; // 指向字符串a和b的最后一位
|
||||||
|
|
||||||
|
// 当i或j在范围内,或有进位时,继续循环
|
||||||
|
while (i >= 0 || j >= 0 || carry == 1) {
|
||||||
|
int sum = carry; // 当前位的和,初始为进位
|
||||||
|
|
||||||
|
// 如果i在范围内,加上a的当前位
|
||||||
|
if (i >= 0) {
|
||||||
|
sum += a.charAt(i) - '0'; // 将字符转换为数字
|
||||||
|
i--; // 移动到下一位
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果j在范围内,加上b的当前位
|
||||||
|
if (j >= 0) {
|
||||||
|
sum += b.charAt(j) - '0'; // 将字符转换为数字
|
||||||
|
j--; // 移动到下一位
|
||||||
|
}
|
||||||
|
|
||||||
|
result.append(sum % 2); // 当前位的结果
|
||||||
|
carry = sum / 2; // 更新进位
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.reverse().toString(); // 反转结果并返回
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*给你一个非负整数 x ,计算并返回 x 的 算术平方根 。
|
||||||
|
* 由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。
|
||||||
|
* 注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 。
|
||||||
|
*
|
||||||
|
* @param x
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int mySqrt(int x) {
|
||||||
|
if (x < 2){
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
long guess = x / 2;
|
||||||
|
while (guess * guess > x){
|
||||||
|
guess = (guess + x/guess)/2;
|
||||||
|
}
|
||||||
|
return (int) guess;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
|
||||||
|
* 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
|
||||||
|
* @param n
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int climbStairs(int n) {
|
||||||
|
if(n < 3)return n;
|
||||||
|
int dp[]=new int[n+1];
|
||||||
|
dp[1]=1;
|
||||||
|
dp[2]=2;
|
||||||
|
for(int i=3;i<n+1;i++) {
|
||||||
|
dp[i]=dp[i-1]+dp[i-2];
|
||||||
|
}
|
||||||
|
return dp[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
|
||||||
|
* @param head
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static ListNode deleteDuplicates(ListNode head) {
|
||||||
|
ListNode current = head;
|
||||||
|
while (current!= null && current.next != null){
|
||||||
|
if (current.val == current.next.val){
|
||||||
|
current.next = current.next.next;
|
||||||
|
}else {
|
||||||
|
current = current.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
14
src/main/java/com/nbee/solution/solution/test.java
Normal file
14
src/main/java/com/nbee/solution/solution/test.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.nbee.solution.solution;
|
||||||
|
|
||||||
|
|
||||||
|
public class test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ListNode head = new ListNode(1);
|
||||||
|
head.next = new ListNode(1);
|
||||||
|
head.next.next = new ListNode(1);
|
||||||
|
head.next.next.next = new ListNode(2);
|
||||||
|
|
||||||
|
ListNode result = Solution.deleteDuplicates(head);
|
||||||
|
System.out.println(result.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package solution;
|
|
||||||
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class test {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int[] nums = new int[]{1,2,3,4,4,5,6};
|
|
||||||
boolean canJump = Solution.canJump(nums);
|
|
||||||
System.out.println(canJump);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
139
src/test/java/com/nbee/solution/practice/BinarySearchTest.java
Normal file
139
src/test/java/com/nbee/solution/practice/BinarySearchTest.java
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class BinarySearchTest {
|
||||||
|
private int[] nums = {1, 3, 5, 7, 9};
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_Found() {
|
||||||
|
int[] nums = {1, 3, 5, 7, 9, 11, 13};
|
||||||
|
int target = 5;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
Assert.assertEquals("Element should be found at index 2", 2, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_NotFound() {
|
||||||
|
int[] nums = {1, 3, 5, 7, 9};
|
||||||
|
int target = 4;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
Assert.assertEquals("Element should not be found", -1, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_EmptyArray() {
|
||||||
|
int[] nums = {};
|
||||||
|
int target = 1;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
Assert.assertEquals("Element should not be found in an empty array", -1, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_SingleElementArray() {
|
||||||
|
int[] nums = {1};
|
||||||
|
int target = 1;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
Assert.assertEquals("Element should be found at index 0", 0, result);
|
||||||
|
|
||||||
|
target = 2;
|
||||||
|
result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
Assert.assertEquals("Element should not be found", -1, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchBasic_DecreasingArray() {
|
||||||
|
int[] nums = {9, 7, 5, 3, 1};
|
||||||
|
int target = 5;
|
||||||
|
int result = BinarySearch.binarySearchBasic(nums, target);
|
||||||
|
Assert.assertEquals("Binary search should work on decreasing arrays", 2, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchAlternativeFound() {
|
||||||
|
// Test that the method correctly returns the index of an existing element
|
||||||
|
int target = 7;
|
||||||
|
int result = BinarySearch.binarySearchAlternative(nums, target);
|
||||||
|
assertEquals(3, result, "The target should be found at index 3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchAlternativeNotFound() {
|
||||||
|
// Test that the method correctly returns -1 when the element is not present
|
||||||
|
int target = 4;
|
||||||
|
int result = BinarySearch.binarySearchAlternative(nums, target);
|
||||||
|
assertEquals(-1, result, "The target should not be found");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchAlternativeWithEmptyArray() {
|
||||||
|
// Test with an empty array
|
||||||
|
int[] emptyNums = new int[]{};
|
||||||
|
int target = 1;
|
||||||
|
int result = BinarySearch.binarySearchAlternative(emptyNums, target);
|
||||||
|
assertEquals(-1, result, "The target should not be found in an empty array");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchAlternativeWithSingleElement() {
|
||||||
|
// Test with a single element array
|
||||||
|
int[] singleNums = new int[]{5};
|
||||||
|
int target = 5;
|
||||||
|
int result = BinarySearch.binarySearchAlternative(singleNums, target);
|
||||||
|
assertEquals(0, result, "The target should be found at index 0 in a single-element array");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test Binary Search Java")
|
||||||
|
public void testBinarySearchJava() {
|
||||||
|
int[] nums = {1, 3, 5, 7, 9};
|
||||||
|
int target = 4;
|
||||||
|
int result = Arrays.binarySearch(nums, target);
|
||||||
|
if (result < 0) {
|
||||||
|
int insertIndex = Math.abs(result + 1);
|
||||||
|
System.out.println("nums: " + Arrays.toString(nums));
|
||||||
|
System.out.println("insertIndex: " + insertIndex);
|
||||||
|
int[] newNums = new int[nums.length + 1];
|
||||||
|
//第一次拷贝
|
||||||
|
System.arraycopy(nums, 0, newNums, 0, nums.length);
|
||||||
|
System.out.println("newNums: " + Arrays.toString(newNums));
|
||||||
|
newNums[insertIndex] = target;
|
||||||
|
// 第二次拷贝
|
||||||
|
System.arraycopy(nums, insertIndex, newNums, insertIndex + 1, nums.length - insertIndex);
|
||||||
|
System.out.println("newNums: " + Arrays.toString(newNums));
|
||||||
|
}
|
||||||
|
System.out.println("result: " + result);
|
||||||
|
assertEquals(-3, result, "The target should not be found");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchLeftMost() {
|
||||||
|
int[] nums = new int[]{1, 3, 3, 5, 7};
|
||||||
|
assertEquals(0, BinarySearch.binarySearchLeftMost(nums, 1));
|
||||||
|
assertEquals(1, BinarySearch.binarySearchLeftMost(nums, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBinarySearchRightMost() {
|
||||||
|
int[] nums = new int[]{1, 3, 3, 5, 5, 5, 5, 5, 7};
|
||||||
|
assertEquals(0, BinarySearch.binarySearchRightMost(nums, 1));
|
||||||
|
assertEquals(2, BinarySearch.binarySearchRightMost(nums, 3));
|
||||||
|
assertEquals(7, BinarySearch.binarySearchRightMost(nums, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSearchRange() {
|
||||||
|
int[] nums = {5, 7, 7, 8, 8, 10};
|
||||||
|
int[] nums1 = {1};
|
||||||
|
assertArrayEquals(new int[]{1,2}, BinarySearch.searchRange(nums, 7), "The result should be [1, 2]" );
|
||||||
|
assertArrayEquals(new int[]{3,4}, BinarySearch.searchRange(nums, 8), "The result should be [3, 4]" );
|
||||||
|
assertArrayEquals(new int[]{-1, -1}, BinarySearch.searchRange(nums, 6), "The result should be [-1, -1]");
|
||||||
|
assertArrayEquals(new int[]{0, 0}, BinarySearch.searchRange(nums1, 1), "The result should be [0, 0]");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import java.util.List;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
|
||||||
|
|
||||||
|
class DynamicArrayTest {
|
||||||
|
|
||||||
|
private DynamicArray dynamicArray;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
dynamicArray = new DynamicArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddListInitial() {
|
||||||
|
dynamicArray.addList(1);
|
||||||
|
dynamicArray.addList(3);
|
||||||
|
dynamicArray.addList(5);
|
||||||
|
dynamicArray.add(1, 7);
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
System.out.println(dynamicArray.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test01() {
|
||||||
|
dynamicArray.addList(1);
|
||||||
|
dynamicArray.addList(3);
|
||||||
|
dynamicArray.addList(5);
|
||||||
|
dynamicArray.add(1, 7);
|
||||||
|
dynamicArray.forEach(element -> {
|
||||||
|
System.out.println(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test02() {
|
||||||
|
dynamicArray.addList(1);
|
||||||
|
dynamicArray.addList(3);
|
||||||
|
dynamicArray.addList(5);
|
||||||
|
dynamicArray.add(1, 7);
|
||||||
|
for (Integer element : dynamicArray) {
|
||||||
|
System.out.println(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test03() {
|
||||||
|
dynamicArray.addList(1);
|
||||||
|
dynamicArray.addList(3);
|
||||||
|
dynamicArray.addList(5);
|
||||||
|
dynamicArray.add(1, 7);
|
||||||
|
dynamicArray.stream().forEach(element -> {
|
||||||
|
System.out.println(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test04() {
|
||||||
|
dynamicArray.addList(1);
|
||||||
|
dynamicArray.addList(3);
|
||||||
|
dynamicArray.addList(5);
|
||||||
|
dynamicArray.add(1, 7);
|
||||||
|
int removed =dynamicArray.remove(1);
|
||||||
|
System.out.println("removed: " + removed);
|
||||||
|
for (Integer element : dynamicArray) {
|
||||||
|
System.out.println(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void test05() {
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
dynamicArray.addList(i);
|
||||||
|
}
|
||||||
|
System.out.println(dynamicArray.toString());
|
||||||
|
assertIterableEquals(List.of(0, 1, 2, 3, 4, 5, 6, 7, 8), dynamicArray, "List should be equal");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class SinglyLinkedListTest {
|
||||||
|
private SinglyLinkedList list = new SinglyLinkedList();
|
||||||
|
@Test
|
||||||
|
public void singlyLinkedListAddFirstTest(){
|
||||||
|
list.addFirst(1);
|
||||||
|
list.addFirst(3);
|
||||||
|
list.addFirst(5);
|
||||||
|
list.addFirst(6);
|
||||||
|
list.loop0(i -> System.out.println(i + " "));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void singlyLinkedListIteratorTest(){
|
||||||
|
list.addFirst(1);
|
||||||
|
list.addFirst(3);
|
||||||
|
list.addFirst(5);
|
||||||
|
list.addFirst(6);
|
||||||
|
for (Integer value: list) {
|
||||||
|
System.out.println(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user