Skip to content

Commit c2e117c

Browse files
author
FreeTymeKiyan
committed
add Count Primes and Reverse words in a string 2
update solutions
1 parent 78042aa commit c2e117c

13 files changed

+780
-663
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.freetymekiyan.algorithms.level.easy;
2+
3+
/**
4+
* 204. Count Primes
5+
* <p>
6+
* Count the number of prime numbers less than a non-negative number, n.
7+
* <p>
8+
* Example:
9+
* <p>
10+
* Input: 10
11+
* Output: 4
12+
* Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
13+
* <p>
14+
* Companies: Microsoft, Amazon, Adobe, Google, Apple, Yahoo, Goldman Sachs, Nvidia, Tesla, Tencent, Bloomberg, Capital
15+
* One
16+
* <p>
17+
* Related Topics: Hash Table, Math
18+
* <p>
19+
* Similar Questions: (E) Ugly Number, (M) Ugly Number II, (M) Perfect Squares
20+
*/
21+
public class CountPrimes {
22+
23+
public int countPrimes(int n) {
24+
if (n <= 2) return 0;
25+
26+
int res = n >> 1;
27+
int m = (int) Math.sqrt(n - 1);
28+
boolean[] notPrime = new boolean[n];
29+
30+
for (int i = 3; i <= m; i += 2) {
31+
if (!notPrime[i]) {
32+
for (int j = i * i, step = i << 1; j < n; j += step) {
33+
if (!notPrime[j]) {
34+
notPrime[j] = true;
35+
--res;
36+
}
37+
}
38+
}
39+
}
40+
41+
return res;
42+
}
43+
}

src/main/java/com/freetymekiyan/algorithms/level/easy/HappyNumber.java

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,71 @@
44
import java.util.Set;
55

66
/**
7+
* 202. Happy Number
8+
* <p>
79
* Write an algorithm to determine if a number is "happy".
8-
*
10+
* <p>
911
* A happy number is a number defined by the following process: Starting with any positive integer, replace the number
1012
* by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it
1113
* loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy
1214
* numbers.
13-
*
15+
* <p>
1416
* Example:
1517
* 19 is a happy number
1618
* 1^2 + 9^2 = 82
1719
* 8^2 + 2^2 = 68
1820
* 6^2 + 8^2 = 100
1921
* 1^2 + 0^2 + 0^2 = 1
20-
*
22+
* <p>
2123
* Tags: Hash Table Math
2224
* Similar Problems: (E) addRecursive Digits (E) Ugly Number
2325
*/
2426
public class HappyNumber {
2527

26-
public static void main(String[] args) {
27-
HappyNumber hn = new HappyNumber();
28-
for (int i = 10; i < 20; i++) {
29-
System.out.println(i + ": " + hn.isHappy(i));
30-
System.out.println(i + ": " + hn.isHappy2(i));
31-
}
28+
public static void main(String[] args) {
29+
HappyNumber hn = new HappyNumber();
30+
for (int i = 10; i < 20; i++) {
31+
System.out.println(i + ": " + hn.isHappy(i));
32+
System.out.println(i + ": " + hn.isHappy2(i));
3233
}
34+
}
3335

34-
/**
35-
* loop detection like linked list
36-
*/
37-
public boolean isHappy(int n) {
38-
int slow, fast;
39-
slow = fast = n;
40-
do {
41-
slow = digitSquareSum(slow);
42-
fast = digitSquareSum(digitSquareSum(fast));
43-
} while (slow != fast);
44-
return slow == 1;
45-
}
36+
/**
37+
* loop detection like linked list
38+
*/
39+
public boolean isHappy(int n) {
40+
int slow, fast;
41+
slow = fast = n;
42+
do {
43+
slow = digitSquareSum(slow);
44+
fast = digitSquareSum(digitSquareSum(fast));
45+
} while (slow != fast);
46+
return slow == 1;
47+
}
4648

47-
/**
48-
* loop detection using Set, use more space
49-
*/
50-
public boolean isHappy2(int n) {
51-
if (n < 1) return false;
49+
/**
50+
* loop detection using Set, use more space
51+
*/
52+
public boolean isHappy2(int n) {
53+
if (n < 1) return false;
5254

53-
int num = n;
54-
Set<Integer> results = new HashSet<>();
55-
while (!results.contains(num)) {
56-
results.add(num);
57-
num = digitSquareSum(num);
58-
}
59-
60-
return num == 1;
55+
int num = n;
56+
Set<Integer> results = new HashSet<>();
57+
while (!results.contains(num)) {
58+
results.add(num);
59+
num = digitSquareSum(num);
6160
}
6261

63-
public int digitSquareSum(int n) {
64-
int res = 0;
65-
int digit;
66-
for (; n > 0; n /= 10) {
67-
digit = n % 10;
68-
res += digit * digit;
69-
}
70-
return res;
62+
return num == 1;
63+
}
64+
65+
private int digitSquareSum(int n) {
66+
int res = 0;
67+
int digit;
68+
for (; n > 0; n /= 10) {
69+
digit = n % 10;
70+
res += digit * digit;
7171
}
72+
return res;
73+
}
7274
}

src/main/java/com/freetymekiyan/algorithms/level/easy/IsomorphicStrings.java

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.junit.Test;
77

88
/**
9+
* 205. Isomorphic Strings
10+
* <p>
911
* Given two strings s and t, determine if they are isomorphic.
1012
* <p>
1113
* Two strings are isomorphic if the characters in s can be replaced to get t.
@@ -29,59 +31,59 @@
2931
*/
3032
public class IsomorphicStrings {
3133

32-
private IsomorphicStrings is;
34+
private IsomorphicStrings is;
3335

34-
/**
35-
* Hash Table.
36-
* Store the previous seen index of a character. 0 means not seen yet.
37-
* If the last seen indices are different, return false.
38-
* Update index to i + 1.
39-
* After the check, return true, since s and t are of same length.
40-
*/
41-
public boolean isIsomorphic(String s, String t) {
42-
if (s == null || t == null) {
43-
return false;
44-
}
45-
int[] m = new int[512];
46-
for (int i = 0; i < s.length(); i++) {
47-
if (m[s.charAt(i)] != m[t.charAt(i) + 256]) {
48-
return false;
49-
}
50-
m[s.charAt(i)] = m[t.charAt(i) + 256] = i + 1;
51-
}
52-
return true;
36+
/**
37+
* Hash Table.
38+
* Store the previous seen index of a character. 0 means not seen yet.
39+
* If the last seen indices are different, return false.
40+
* Update index to i + 1.
41+
* After the check, return true, since s and t are of same length.
42+
*/
43+
public boolean isIsomorphic(String s, String t) {
44+
if (s == null || t == null) {
45+
return false;
5346
}
54-
55-
@Before
56-
public void setUp() {
57-
is = new IsomorphicStrings();
47+
int[] m = new int[512];
48+
for (int i = 0; i < s.length(); i++) {
49+
if (m[s.charAt(i)] != m[t.charAt(i) + 256]) {
50+
return false;
51+
}
52+
m[s.charAt(i)] = m[t.charAt(i) + 256] = i + 1;
5853
}
54+
return true;
55+
}
5956

60-
@Test
61-
public void testEdgeCases() {
62-
Assert.assertFalse(is.isIsomorphic(null, null));
63-
Assert.assertFalse(is.isIsomorphic(null, ""));
64-
Assert.assertFalse(is.isIsomorphic("", null));
65-
Assert.assertTrue(is.isIsomorphic("", ""));
66-
}
57+
@Before
58+
public void setUp() {
59+
is = new IsomorphicStrings();
60+
}
6761

68-
@Test
69-
public void testExamples() {
70-
// "egg", "add", return true
71-
Assert.assertTrue(is.isIsomorphic("egg", "add"));
72-
// "foo", "bar", return false
73-
Assert.assertFalse(is.isIsomorphic("foo", "bar"));
74-
// "paper", "title", return true
75-
Assert.assertTrue(is.isIsomorphic("paper", "title"));
76-
// "papper", "tittle", return true
77-
Assert.assertTrue(is.isIsomorphic("papper", "tittle"));
78-
// "abba", "abab"
79-
Assert.assertFalse(is.isIsomorphic("abba", "abab"));
80-
}
62+
@Test
63+
public void testEdgeCases() {
64+
Assert.assertFalse(is.isIsomorphic(null, null));
65+
Assert.assertFalse(is.isIsomorphic(null, ""));
66+
Assert.assertFalse(is.isIsomorphic("", null));
67+
Assert.assertTrue(is.isIsomorphic("", ""));
68+
}
8169

82-
@After
83-
public void tearDown() {
84-
is = null;
85-
}
70+
@Test
71+
public void testExamples() {
72+
// "egg", "add", return true
73+
Assert.assertTrue(is.isIsomorphic("egg", "add"));
74+
// "foo", "bar", return false
75+
Assert.assertFalse(is.isIsomorphic("foo", "bar"));
76+
// "paper", "title", return true
77+
Assert.assertTrue(is.isIsomorphic("paper", "title"));
78+
// "papper", "tittle", return true
79+
Assert.assertTrue(is.isIsomorphic("papper", "tittle"));
80+
// "abba", "abab"
81+
Assert.assertFalse(is.isIsomorphic("abba", "abab"));
82+
}
83+
84+
@After
85+
public void tearDown() {
86+
is = null;
87+
}
8688

8789
}

0 commit comments

Comments
 (0)