Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 97addbe

Browse files
committed
strstr done
1 parent 0d96dcf commit 97addbe

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

‎src/main/java/com/leetcode/arrays/MajorityElement.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ public class MajorityElement {
1515

1616
/**
1717
* Time complexity: O(n)
18-
* Runtime: <a href="https://leetcode.com/submissions/detail/225284632/">1 ms</a>.
18+
* Runtime: <a href="https://leetcode.com/submissions/detail/225285480/">1 ms</a>.
1919
*
2020
* @param nums
2121
* @return
2222
*/
2323
public static int majorityElement(int[] nums) {
24-
int count = 0;
24+
int count = 1;
2525
int majElem = nums[0];
2626

27-
for (int i = 0; i < nums.length; i++) {
27+
for (int i = 1; i < nums.length; i++) {
2828
if (count <= 0) {
2929
majElem = nums[i];
3030
count = 0;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.leetcode.strings;
2+
3+
/**
4+
* Level: Easy
5+
* Problem Link: https://leetcode.com/problems/implement-strstr/
6+
* Problem Description:
7+
* Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle
8+
* is not part of haystack.
9+
* <p>
10+
* Example 1:
11+
* <p>
12+
* Input: haystack = "hello", needle = "ll"
13+
* Output: 2
14+
* Example 2:
15+
* <p>
16+
* Input: haystack = "aaaaa", needle = "bba"
17+
* Output: -1
18+
*
19+
* @author rampatra
20+
* @since 2019年04月28日
21+
*/
22+
public class StrStr {
23+
24+
/**
25+
* Time complexity: O(m*n)
26+
* where,
27+
* m = length of haystack
28+
* n = length of needle
29+
* <p>
30+
* Runtime: <a href="https://leetcode.com/submissions/detail/225543896/">3 ms</a>.
31+
*
32+
* @param haystack
33+
* @param needle
34+
* @return
35+
*/
36+
public static int strStr(String haystack, String needle) {
37+
for (int i = 0; ; i++) {
38+
for (int j = 0; ; j++) {
39+
if (j == needle.length()) return i;
40+
if (i + j == haystack.length()) return -1;
41+
if (needle.charAt(j) != haystack.charAt(i + j)) break;
42+
}
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
System.out.println(strStr("hello", "ll"));
48+
System.out.println(strStr("leet", "e"));
49+
System.out.println(strStr("mississippi", "issip"));
50+
System.out.println(strStr("mississippi", "pi"));
51+
System.out.println(strStr("aaaa", "bba"));
52+
53+
// edge cases
54+
System.out.println(strStr("aaa", "aaaa"));
55+
System.out.println(strStr("aaaa", ""));
56+
System.out.println(strStr("", "abc"));
57+
System.out.println(strStr("", ""));
58+
}
59+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /