Skip to content

Commit a086964

Browse files
committed
246 (1) 1st try
1 parent 8ac6a67 commit a086964

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
***************************************************************************
3+
* Description:
4+
*
5+
* A strobogrammatic number is a number that looks the same when rotated
6+
* 180 degrees (looked at upside down).
7+
*
8+
* Write a function to determine if a number is strobogrammatic.
9+
* The number is represented as a string.
10+
*
11+
* For example, the numbers "69", "88", and "818" are all strobogrammatic.
12+
*
13+
***************************************************************************
14+
* @tag : Hash Table; Math
15+
* {@link https://leetcode.com/problems/strobogrammatic-number/ }
16+
*/
17+
package _246_StrobogrammaticNumber;
18+
19+
/** see test {@link _246_StrobogrammaticNumber.PracticeTest } */
20+
public class Practice {
21+
22+
public boolean isStrobogrammatic(String num) {
23+
// TODO Auto-generated method stub
24+
return false;
25+
}
26+
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Time : O() ; Space: O()
3+
* @tag : Hash Table; Math
4+
* @by : Steven Cooks
5+
* @date: Oct 3, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* A strobogrammatic number is a number that looks the same when rotated
10+
* 180 degrees (looked at upside down).
11+
*
12+
* Write a function to determine if a number is strobogrammatic.
13+
* The number is represented as a string.
14+
*
15+
* For example, the numbers "69", "88", and "818" are all strobogrammatic.
16+
*
17+
***************************************************************************
18+
* {@link https://leetcode.com/problems/strobogrammatic-number/ }
19+
*/
20+
package _246_StrobogrammaticNumber;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
/** see test {@link _246_StrobogrammaticNumber.SolutionTest } */
26+
public class Solution {
27+
28+
@SuppressWarnings({ "serial" })
29+
private static final Map<Character, Character> map = new HashMap<Character, Character>() {
30+
{
31+
put('6', '9');
32+
put('9', '6');
33+
put('0', '0');
34+
put('1', '1');
35+
put('8', '8');
36+
}
37+
};
38+
39+
public boolean isStrobogrammatic(String num) {
40+
for (int i = 0, j = num.length() - 1; i <= j ; i++, j--) {
41+
char ch = num.charAt(i);
42+
if (!map.containsKey(ch) || map.get(ch) != num.charAt(j)) {
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
49+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package _246_StrobogrammaticNumber;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
public class PracticeTest {
12+
13+
/** Test method for {@link _246_StrobogrammaticNumber.Practice } */
14+
Practice solution;
15+
16+
@Rule
17+
public Timeout globalTimeout = new Timeout(200);
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
solution = new Practice();
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
solution = null;
27+
}
28+
29+
@Test
30+
public void Test1() {
31+
String num = "69";
32+
assertTrue(solution.isStrobogrammatic(num));
33+
}
34+
35+
@Test
36+
public void Test2() {
37+
String num = "88";
38+
assertTrue(solution.isStrobogrammatic(num));
39+
}
40+
41+
@Test
42+
public void Test3() {
43+
String num = "818";
44+
assertTrue(solution.isStrobogrammatic(num));
45+
}
46+
47+
@Test
48+
public void Test4() {
49+
String num = "88";
50+
assertTrue(solution.isStrobogrammatic(num));
51+
}
52+
53+
@Test
54+
public void Test5() {
55+
String num = "66";
56+
assertTrue(!solution.isStrobogrammatic(num));
57+
}
58+
59+
@Test
60+
public void Test6() {
61+
String num = "8998";
62+
assertTrue(!solution.isStrobogrammatic(num));
63+
}
64+
65+
@Test
66+
public void Test7() {
67+
String num = "2";
68+
assertTrue(!solution.isStrobogrammatic(num));
69+
}
70+
71+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package _246_StrobogrammaticNumber;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
public class SolutionTest {
12+
13+
/** Test method for {@link _246_StrobogrammaticNumber.Solution } */
14+
Solution solution;
15+
16+
@Rule
17+
public Timeout globalTimeout = new Timeout(200);
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
solution = new Solution();
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
solution = null;
27+
}
28+
29+
@Test
30+
public void Test1() {
31+
String num = "69";
32+
assertTrue(solution.isStrobogrammatic(num));
33+
}
34+
35+
@Test
36+
public void Test2() {
37+
String num = "88";
38+
assertTrue(solution.isStrobogrammatic(num));
39+
}
40+
41+
@Test
42+
public void Test3() {
43+
String num = "818";
44+
assertTrue(solution.isStrobogrammatic(num));
45+
}
46+
47+
@Test
48+
public void Test4() {
49+
String num = "88";
50+
assertTrue(solution.isStrobogrammatic(num));
51+
}
52+
53+
@Test
54+
public void Test5() {
55+
String num = "66";
56+
assertTrue(!solution.isStrobogrammatic(num));
57+
}
58+
59+
@Test
60+
public void Test6() {
61+
String num = "8998";
62+
assertTrue(!solution.isStrobogrammatic(num));
63+
}
64+
65+
@Test
66+
public void Test7() {
67+
String num = "2";
68+
assertTrue(!solution.isStrobogrammatic(num));
69+
}
70+
71+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy