Update Solution.java
parent
6c39c4d462
commit
5a0d46a2d9
|
|
@ -205,7 +205,12 @@ public class Solution {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
|
||||||
|
* @param list1
|
||||||
|
* @param list2
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
|
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
|
||||||
ListNode dummy = new ListNode(0);
|
ListNode dummy = new ListNode(0);
|
||||||
ListNode current = dummy;
|
ListNode current = dummy;
|
||||||
|
|
@ -227,11 +232,26 @@ public class Solution {
|
||||||
return dummy.next;
|
return dummy.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。
|
||||||
|
* @param haystack
|
||||||
|
* @param needle
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
|
||||||
public int strStr(String haystack, String needle) {
|
public int strStr(String haystack, String needle) {
|
||||||
if (needle.isEmpty()) return -1;
|
if (needle.isEmpty()) return -1;
|
||||||
return haystack.indexOf(needle);
|
return haystack.indexOf(needle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
|
||||||
|
*
|
||||||
|
* 请必须使用时间复杂度为 O(log n) 的算法。
|
||||||
|
* @param nums
|
||||||
|
* @param target
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public int searchInsert(int[] nums, int target) {
|
public int searchInsert(int[] nums, int target) {
|
||||||
if (nums.length == 0) return 0;
|
if (nums.length == 0) return 0;
|
||||||
for (int i = 0; i < nums.length; i++) {
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
|
@ -242,6 +262,13 @@ public class Solution {
|
||||||
return nums.length;
|
return nums.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。
|
||||||
|
*
|
||||||
|
* 单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
|
||||||
|
* @param s
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public int lengthOfLastWord(String s) {
|
public int lengthOfLastWord(String s) {
|
||||||
if (s.isEmpty()) return 0;
|
if (s.isEmpty()) return 0;
|
||||||
for (int i = s.length() - 1; i >= 0; i--) {
|
for (int i = s.length() - 1; i >= 0; i--) {
|
||||||
|
|
@ -256,4 +283,34 @@ public class Solution {
|
||||||
}
|
}
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue