Compare commits

...

2 Commits

Author SHA1 Message Date
wsy182 27074cb4c6 Update test.java 2024-07-08 21:37:57 +08:00
wsy182 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());
}
}