LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers

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.
  • a + b = n

The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.

Example 1:

Input: n = 2

Output: 1,1

Explanation: Let a = 1 and b = 1.

Both a and b are no-zero integers, and a + b = 2 = n.

Example 2:

Input: n = 11

Output: 2,9

Explanation: Let a = 2 and b = 9.

Both a and b are no-zero integers, and a + b = 11 = n.

Note that there are other valid answers as 8, 3 that can be accepted.

Constraints:
  • 2 < = n < = 10 4 2 <= n <= 10^4 2<=n<=104

From: LeetCode

Link: 1317. Convert Integer to the Sum of Two No-Zero Integers


Solution:

Ideas:

Try every a from 1 to n - 1, set b = n - a, and return the first pair containing no digit 0.

Code:
c 复制代码
#include <stdlib.h>
#include <stdbool.h>

static bool isNoZero(int num) {
    while (num > 0) {
        if (num % 10 == 0) {
            return false;
        }
        num /= 10;
    }
    return true;
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getNoZeroIntegers(int n, int* returnSize) {
    int* answer = malloc(2 * sizeof(int));

    for (int a = 1; a < n; a++) {
        int b = n - a;

        if (isNoZero(a) && isNoZero(b)) {
            answer[0] = a;
            answer[1] = b;
            *returnSize = 2;
            return answer;
        }
    }

    *returnSize = 0;
    free(answer);
    return NULL;
}
相关推荐
青少儿编程课堂4 小时前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
郝学胜-神的一滴6 小时前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
geovindu6 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗8 小时前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
凌冰_8 小时前
数据结构 - 队列(五)
数据结构·算法·软件工程
让我们一起加油好吗9 小时前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
让我们一起加油好吗10 小时前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
Rain的Java大神之路10 小时前
LeetCode 42「接雨水」:面试官百出不厌的题,一文讲透从暴力到双指针 O(1)
java·后端·leetcode
lisw0520 小时前
稀疏算法概述!
算法·机器人·无人机