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 bfd26d2

Browse files
authored
Added tasks 204-206.
1 parent 1a70769 commit bfd26d2

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0201_0300.s0204_count_primes;
2+
3+
public class Solution {
4+
public int countPrimes(int n) {
5+
boolean[] isprime = new boolean[n];
6+
int count = 0;
7+
for (int i = 2; i * i <= n; i++) {
8+
if (!isprime[i]) {
9+
for (int j = i * 2; j < n; j += i) {
10+
isprime[j] = true;
11+
}
12+
}
13+
}
14+
for (int i = 2; i < isprime.length; i++) {
15+
if (!isprime[i]) {
16+
count++;
17+
}
18+
}
19+
return count;
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0201_0300.s0205_isomorphic_strings;
2+
3+
public class Solution {
4+
public boolean isIsomorphic(String s, String t) {
5+
int[] map = new int[128];
6+
char[] str = s.toCharArray();
7+
char[] tar = t.toCharArray();
8+
int n = str.length;
9+
for (int i = 0; i < n; i++) {
10+
if (map[tar[i]] == 0) {
11+
if (search(map, str[i], tar[i]) != -1) {
12+
return false;
13+
}
14+
map[tar[i]] = str[i];
15+
} else {
16+
if (map[tar[i]] != str[i]) {
17+
return false;
18+
}
19+
}
20+
}
21+
return true;
22+
}
23+
24+
private int search(int[] map, int tar, int skip) {
25+
for (int i = 0; i < 128; i++) {
26+
if (i == skip) {
27+
continue;
28+
}
29+
if (map[i] != 0 && map[i] == tar) {
30+
return i;
31+
}
32+
}
33+
return -1;
34+
}
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0201_0300.s0206_reverse_linked_list;
2+
3+
import com_github_leetcode.ListNode;
4+
5+
public class Solution {
6+
public ListNode reverseList(ListNode head) {
7+
ListNode prev = null;
8+
ListNode curr = head;
9+
while (curr != null) {
10+
ListNode next = curr.next;
11+
curr.next = prev;
12+
prev = curr;
13+
curr = next;
14+
}
15+
return prev;
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g0201_0300.s0204_count_primes;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void countPrimes() {
11+
assertThat(new Solution().countPrimes(10), equalTo(4));
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0201_0300.s0205_isomorphic_strings;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void isIsomorphic() {
11+
assertThat(new Solution().isIsomorphic("egg", "add"), equalTo(true));
12+
}
13+
14+
@Test
15+
public void isIsomorphic2() {
16+
assertThat(new Solution().isIsomorphic("foo", "bar"), equalTo(false));
17+
}
18+
19+
@Test
20+
public void isIsomorphic3() {
21+
assertThat(new Solution().isIsomorphic("paper", "title"), equalTo(true));
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0201_0300.s0206_reverse_linked_list;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ListNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void reverseList() {
12+
ListNode headActual = new ListNode(1);
13+
headActual.next = new ListNode(2);
14+
headActual.next.next = new ListNode(3);
15+
headActual.next.next.next = new ListNode(4);
16+
headActual.next.next.next.next = new ListNode(5);
17+
assertThat(new Solution().reverseList(headActual).toString(), equalTo("5, 4, 3, 2, 1"));
18+
}
19+
}

0 commit comments

Comments
(0)

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