Compare commits

..

2 Commits

Author SHA1 Message Date
27074cb4c6 Update test.java 2024-07-08 21:37:57 +08:00
5a0d46a2d9 Update Solution.java 2024-07-08 21:37:55 +08:00
2 changed files with 61 additions and 4 deletions

View File

@@ -205,7 +205,12 @@ public class Solution {
return true;
}
/**
* 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
* @param list1
* @param list2
* @return
*/
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0);
ListNode current = dummy;
@@ -227,11 +232,26 @@ public class Solution {
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++) {
@@ -242,6 +262,13 @@ public class Solution {
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--) {
@@ -256,4 +283,34 @@ public class Solution {
}
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;
}
}

View File

@@ -6,9 +6,9 @@ import java.util.stream.Collectors;
public class test {
public static void main(String[] args) {
int[] nums = new int[]{1,1,2};
int result = Solution.removeDuplicates(nums);
System.out.println(result);
int[] nums = new int[]{9};
int[] result = Solution.plusOne(nums);
System.out.println(result.toString());
}
}