Update Solution.java

master
wsy182 2024-07-08 21:37:55 +08:00
parent 6c39c4d462
commit 5a0d46a2d9
1 changed files with 58 additions and 1 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;
}
}