Skip to content

Commit 9ac7183

Browse files
committed
实现strstr()
1 parent e12f6d0 commit 9ac7183

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

[28]实现 strStr().java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
1+
import java.util.Objects;
22

33
//leetcode submit region begin(Prohibit modification and deletion)
44
class Solution {
55
public int strStr(String haystack, String needle) {
6+
//为空判断
67
if (needle.isEmpty()) {
78
return 0;
89
}
@@ -24,8 +25,9 @@ public int strStr(String haystack, String needle) {
2425
char[] b = needle.toCharArray();
2526

2627
for (int i = 0; i < a.length; i++) {
27-
if (a[i] == b[0]) {
28-
if (needle.equals(haystack.substring(i, i + needle.length()-1))) {
28+
if (Objects.equals(a[i],b[0])&&i+needle.length()<=haystack.length()) {
29+
String temp = haystack.substring(i, i + needle.length());
30+
if (needle.equals(temp)) {
2931
return i;
3032
}
3133
}

[409]最长回文串.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
//leetcode submit region begin(Prohibit modification and deletion)
4+
class Solution {
5+
public int longestPalindrome(String s) {
6+
7+
}
8+
}
9+
//leetcode submit region end(Prohibit modification and deletion)

0 commit comments

Comments
 (0)