Skip to content

Commit 6c36acd

Browse files
add 1317
1 parent 7978d30 commit 6c36acd

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy||
1112
|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium||
1213
|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy||
1314
|1302|[Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1302.java) | |Medium||
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1317. Convert Integer to the Sum of Two No-Zero Integers
5+
*
6+
* Given an integer n. No-Zero integer is a positive integer which doesn't contain any 0 in its decimal representation.
7+
*
8+
* Return a list of two integers [A, B] where:
9+
* A and B are No-Zero integers.
10+
* A + B = n
11+
* It's guarateed that there is at least one valid solution. If there are many valid solutions you can return any of them.
12+
*
13+
* Example 1:
14+
* Input: n = 2
15+
* Output: [1,1]
16+
* Explanation: A = 1, B = 1. A + B = n and both A and B don't contain any 0 in their decimal representation.
17+
*
18+
* Example 2:
19+
* Input: n = 11
20+
* Output: [2,9]
21+
*
22+
* Example 3:
23+
* Input: n = 10000
24+
* Output: [1,9999]
25+
*
26+
* Example 4:
27+
* Input: n = 69
28+
* Output: [1,68]
29+
*
30+
* Example 5:
31+
* Input: n = 1010
32+
* Output: [11,999]
33+
*
34+
* Constraints:
35+
* 2 <= n <= 10^4
36+
* */
37+
public class _1317 {
38+
public static class Solution1 {
39+
public int[] getNoZeroIntegers(int n) {
40+
int left = 1;
41+
int right = n - 1;
42+
while (left <= right) {
43+
if (noZero(left) && noZero(right)) {
44+
return new int[]{left, right};
45+
} else {
46+
left++;
47+
right--;
48+
}
49+
}
50+
return null;
51+
}
52+
53+
private boolean noZero(int num) {
54+
while (num != 0) {
55+
if (num % 10 == 0) {
56+
return false;
57+
} else {
58+
num /= 10;
59+
}
60+
}
61+
return true;
62+
}
63+
}
64+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1317;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1317Test {
10+
private static _1317.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1317.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{11, 999}, solution1.getNoZeroIntegers(1010));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)