Update Solution.java

master
wsy182 2024-07-08 21:14:35 +08:00
parent c72996f342
commit b75d236fbb
1 changed files with 30 additions and 0 deletions

View File

@ -226,4 +226,34 @@ public class Solution {
}
return dummy.next;
}
public int strStr(String haystack, String needle) {
if (needle.isEmpty()) return -1;
return haystack.indexOf(needle);
}
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;
}
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;
}
}