Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a9e5df5

Browse files
refactor 372
1 parent c766fe4 commit a9e5df5

File tree

2 files changed

+81
-52
lines changed

2 files changed

+81
-52
lines changed
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 372. Super Pow
5+
*
46
* Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
57
68
Example1:
@@ -17,33 +19,32 @@
1719
Result: 1024
1820
*/
1921
public class _372 {
20-
/**Reference: https://discuss.leetcode.com/topic/50586/math-solusion-based-on-euler-s-theorem-power-called-only-once-c-java-1-line-python*/
21-
22-
public int superPow(int a, int[] b) {
23-
if (a % 1337 == 0) {
24-
return 0;
25-
}
26-
int p = 0;
27-
for (int i : b) {
28-
p = (p * 10 + i) % 1140;
29-
}
30-
if (p == 0) {
31-
p += 1140;
22+
public static class Solution1 {
23+
public int superPow(int a, int[] b) {
24+
if (a % 1337 == 0) {
25+
return 0;
26+
}
27+
int p = 0;
28+
for (int i : b) {
29+
p = (p * 10 + i) % 1140;
30+
}
31+
if (p == 0) {
32+
p += 1140;
33+
}
34+
return power(a, p, 1337);
3235
}
33-
return power(a, p, 1337);
34-
}
3536

36-
private int power(int a, int n, int mod) {
37-
a %= mod;
38-
int result = 1;
39-
while (n != 0) {
40-
if ((n & 1) != 0) {
41-
result = result * a % mod;
37+
private int power(int a, int n, int mod) {
38+
a %= mod;
39+
int result = 1;
40+
while (n != 0) {
41+
if ((n & 1) != 0) {
42+
result = result * a % mod;
43+
}
44+
a = a * a % mod;
45+
n >>= 1;
4246
}
43-
a = a * a % mod;
44-
n >>= 1;
47+
return result;
4548
}
46-
return result;
4749
}
48-
4950
}

src/test/java/com/fishercoder/_372Test.java

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,65 @@
77

88
import static junit.framework.Assert.assertEquals;
99

10-
/**
11-
* Created by fishercoder on 1/9/17.
12-
*/
1310
public class _372Test {
14-
private static _372 test;
15-
private static int expected;
16-
private static int actual;
17-
private static int a;
18-
private static int[] b;
11+
private static _372.Solution1 solution1;
12+
private static int expected;
13+
private static int actual;
14+
private static int a;
15+
private static int[] b;
1916

20-
@BeforeClass
21-
public static void setup() {
22-
test = new _372();
23-
}
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _372.Solution1();
20+
}
2421

25-
@Before
26-
public void setupForEachTest() {
27-
expected = 0;
28-
actual = 0;
29-
a = 0;
30-
b = new int[10000];
31-
}
22+
@Before
23+
public void setupForEachTest() {
24+
expected = 0;
25+
actual = 0;
26+
a = 0;
27+
b = new int[10000];
28+
}
3229

33-
@Test
34-
public void test1() {
30+
@Test
31+
public void test1() {
3532

36-
a = 78267;
37-
b = new int[]{1,7,7,4,3,1,7,0,1,4,4,9,2,8,5,0,0,9,3,1,2,5,9,6,0,9,9,0,9,6,0,5,3,7,9,8,8,9,8,2,5,4,1,9,3,8,0,5,9,5,6,1,1,8,9,3,7,8,5,8,5,5,3,0,4,3,1,5,4,1,7,9,6,8,8,9,8,0,6,7,8,3,1,1,1,0,6,8,1,1,6,6,9,1,8,5,6,9,0,0,1,7,1,7,7,2,8,5,4,4,5,2,9,6,5,0,8,1,0,9,5,8,7,6,0,6,1,8,7,2,9,8,1,0,7,9,4,7,6,9,2,3,1,3,9,9,6,8,0,8,9,7,7,7,3,9,5,5,7,4,9,8,3,0,1,2,1,5,0,8,4,4,3,8,9,3,7,5,3,9,4,4,9,3,3,2,4,8,9,3,3,8,2,8,1,3,2,2,8,4,2,5,0,6,3,0,9,0,5,4,1,1,8,0,4,2,5,8,2,4,2,7,5,4,7,6,9,0,8,9,6,1,4,7,7,9,7,8,1,4,4,3,6,4,5,2,6,0,1,1,5,3,8,0,9,8,8,0,0,6,1,6,9,6,5,8,7,4,8,9,9,2,4,7,7,9,9,5,2,2,6,9,7,7,9,8,5,9,8,5,5,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