Skip to content

Commit 4aa2f87

Browse files
committed
leetcode 4일차 완료
1 parent 19fb071 commit 4aa2f87

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode.easy;
2+
3+
/**
4+
* https://leetcode.com/problems/implement-strstr/
5+
*/
6+
public class implement_strstr {
7+
public static void main(String[] args) {
8+
String haystack = "mississippi";
9+
String needle = "issi";
10+
System.out.println(strStr(haystack, needle));
11+
}
12+
13+
static public int strStr(String haystack, String needle) {
14+
for (int i = 0; ; i++) {
15+
for (int j = 0; ; j++) {
16+
if (j == needle.length()) return i;
17+
if (i + j == haystack.length()) return -1;
18+
if (needle.charAt(j) != haystack.charAt(i + j)) break;
19+
}
20+
}
21+
}
22+
}

src/leetcode/easy/remove_duplicates_from_sorted_array.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package leetcode.easy;
22

3+
/**
4+
* https://leetcode.com/problems/remove-duplicates-from-sorted-array
5+
*/
36
public class remove_duplicates_from_sorted_array {
47
public static void main(String[] args) {
58
int[] nums = {1, 1, 2};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode.easy;
2+
3+
/**
4+
* https://leetcode.com/problems/remove-element
5+
*/
6+
public class remove_element {
7+
public static void main(String[] args) {
8+
int[] nums = {3, 2, 2, 3};
9+
int val = 3;
10+
System.out.println(removeElement(nums, val));
11+
}
12+
13+
static public int removeElement(int[] nums, int val) {
14+
int idx = 0;
15+
for (int i = 0; i < nums.length; i++) {
16+
if (nums[i] != val)
17+
nums[idx++] = nums[i];
18+
}
19+
return idx;
20+
}
21+
}

0 commit comments

Comments
 (0)