aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--convert_int_sum_two_int.c53
-rw-r--r--convert_int_sum_two_int.py30
2 files changed, 83 insertions, 0 deletions
diff --git a/convert_int_sum_two_int.c b/convert_int_sum_two_int.c
new file mode 100644
index 0000000..ec3b711
--- /dev/null
+++ b/convert_int_sum_two_int.c
@@ -0,0 +1,53 @@
+// 1317. Convert Integer to the Sum of Two No-Zero Integers
+#include "leetcode.h"
+
+/*
+ * no-zero integer is a positive integer that does not contain any 0 in its
+ * decimal representation. given an integer 'n', return a list of two integers
+ * '[a, b]' where 'a' and 'b' are no-zero integers and 'a + b = n'. the test
+ * cases are generated such that there is at lease one valid solution. if there
+ * are many valid solutions, you can return any of them
+ */
+
+int *getNoZeroIntegers(int n, int *returnSize) {
+ int *ans = (int *)malloc(2 * sizeof(int));
+ for (int i = 1; i < n; ++i) {
+ int j = n - i, valid = 1;
+ int a = i, b = j;
+ while (a > 0) {
+ if (!(a % 10)) {
+ valid = false;
+ break;
+ }
+ a /= 10;
+ }
+ while (b > 0 && valid) {
+ if (!(b % 10)) {
+ valid = false;
+ break;
+ }
+ b /= 10;
+ }
+ if (valid) {
+ ans[0] = i;
+ ans[1] = j;
+ *returnSize = 2;
+ return ans;
+ }
+ }
+ *returnSize = 0;
+ return NULL;
+}
+
+int main() {
+ int rs1, *gnzi1 = getNoZeroIntegers(2, &rs1);
+ int rs2, *gnzi2 = getNoZeroIntegers(11, &rs2);
+ for (int i = 0; i < rs1; i++)
+ printf("%d ", gnzi1[i]); // expect: 1 1
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%d ", gnzi2[i]); // expect: 2 9
+ printf("\n");
+ free(gnzi1);
+ free(gnzi2);
+}
diff --git a/convert_int_sum_two_int.py b/convert_int_sum_two_int.py
new file mode 100644
index 0000000..6029ed4
--- /dev/null
+++ b/convert_int_sum_two_int.py
@@ -0,0 +1,30 @@
+# 1317. Convert Integer to the Sum of Two No-Zero Integers
+
+"""
+no-zero integer is a positive integer that does not contain any 0 in its
+decimal representation. given an integer 'n', return a list of two integers
+'[a, b]' where 'a' and 'b' are no-zero integers and 'a + b = n'. the test
+cases are generated such that there is at lease one valid solution. if there
+are many valid solutions, you can return any of them
+"""
+
+
+class Solution(object):
+ def check(self, n):
+ return "0" not in str(n)
+
+ def getNoZeroIntegers(self, n):
+ """
+ :type n: int
+ :rtype: List[int]
+ """
+ for i in range(1, n):
+ j = n - i
+ if self.check(i) and self.check(j):
+ return [i, j]
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.getNoZeroIntegers(2))
+ print(obj.getNoZeroIntegers(11))