Movatterモバイル変換


[0]ホーム

URL:


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

Commita086964

Browse files
committed
246 (1) 1st try
1 parent8ac6a67 commita086964

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+
publicclassPractice {
21+
22+
publicbooleanisStrobogrammatic(Stringnum) {
23+
// TODO Auto-generated method stub
24+
returnfalse;
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+
importjava.util.HashMap;
23+
importjava.util.Map;
24+
25+
/** see test {@link _246_StrobogrammaticNumber.SolutionTest } */
26+
publicclassSolution {
27+
28+
@SuppressWarnings({"serial" })
29+
privatestaticfinalMap<Character,Character>map =newHashMap<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+
publicbooleanisStrobogrammatic(Stringnum) {
40+
for (inti =0,j =num.length() -1;i <=j ;i++,j--) {
41+
charch =num.charAt(i);
42+
if (!map.containsKey(ch) ||map.get(ch) !=num.charAt(j)) {
43+
returnfalse;
44+
}
45+
}
46+
returntrue;
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+
importstaticorg.junit.Assert.*;
4+
5+
importorg.junit.After;
6+
importorg.junit.Before;
7+
importorg.junit.Rule;
8+
importorg.junit.Test;
9+
importorg.junit.rules.Timeout;
10+
11+
publicclassPracticeTest {
12+
13+
/** Test method for {@link _246_StrobogrammaticNumber.Practice } */
14+
Practicesolution;
15+
16+
@Rule
17+
publicTimeoutglobalTimeout =newTimeout(200);
18+
19+
@Before
20+
publicvoidsetUp()throwsException {
21+
solution =newPractice();
22+
}
23+
24+
@After
25+
publicvoidtearDown()throwsException {
26+
solution =null;
27+
}
28+
29+
@Test
30+
publicvoidTest1() {
31+
Stringnum ="69";
32+
assertTrue(solution.isStrobogrammatic(num));
33+
}
34+
35+
@Test
36+
publicvoidTest2() {
37+
Stringnum ="88";
38+
assertTrue(solution.isStrobogrammatic(num));
39+
}
40+
41+
@Test
42+
publicvoidTest3() {
43+
Stringnum ="818";
44+
assertTrue(solution.isStrobogrammatic(num));
45+
}
46+
47+
@Test
48+
publicvoidTest4() {
49+
Stringnum ="88";
50+
assertTrue(solution.isStrobogrammatic(num));
51+
}
52+
53+
@Test
54+
publicvoidTest5() {
55+
Stringnum ="66";
56+
assertTrue(!solution.isStrobogrammatic(num));
57+
}
58+
59+
@Test
60+
publicvoidTest6() {
61+
Stringnum ="8998";
62+
assertTrue(!solution.isStrobogrammatic(num));
63+
}
64+
65+
@Test
66+
publicvoidTest7() {
67+
Stringnum ="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+
importstaticorg.junit.Assert.*;
4+
5+
importorg.junit.After;
6+
importorg.junit.Before;
7+
importorg.junit.Rule;
8+
importorg.junit.Test;
9+
importorg.junit.rules.Timeout;
10+
11+
publicclassSolutionTest {
12+
13+
/** Test method for {@link _246_StrobogrammaticNumber.Solution } */
14+
Solutionsolution;
15+
16+
@Rule
17+
publicTimeoutglobalTimeout =newTimeout(200);
18+
19+
@Before
20+
publicvoidsetUp()throwsException {
21+
solution =newSolution();
22+
}
23+
24+
@After
25+
publicvoidtearDown()throwsException {
26+
solution =null;
27+
}
28+
29+
@Test
30+
publicvoidTest1() {
31+
Stringnum ="69";
32+
assertTrue(solution.isStrobogrammatic(num));
33+
}
34+
35+
@Test
36+
publicvoidTest2() {
37+
Stringnum ="88";
38+
assertTrue(solution.isStrobogrammatic(num));
39+
}
40+
41+
@Test
42+
publicvoidTest3() {
43+
Stringnum ="818";
44+
assertTrue(solution.isStrobogrammatic(num));
45+
}
46+
47+
@Test
48+
publicvoidTest4() {
49+
Stringnum ="88";
50+
assertTrue(solution.isStrobogrammatic(num));
51+
}
52+
53+
@Test
54+
publicvoidTest5() {
55+
Stringnum ="66";
56+
assertTrue(!solution.isStrobogrammatic(num));
57+
}
58+
59+
@Test
60+
publicvoidTest6() {
61+
Stringnum ="8998";
62+
assertTrue(!solution.isStrobogrammatic(num));
63+
}
64+
65+
@Test
66+
publicvoidTest7() {
67+
Stringnum ="2";
68+
assertTrue(!solution.isStrobogrammatic(num));
69+
}
70+
71+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp