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;
}
相关推荐
D e re k ZH1 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶2 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊2 小时前
LeetCode2161~2180题目解析
java·leetcode
雪落漂泊2 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
文丑颜不良啊4 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发4 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
严肃小白兔5 小时前
MATLAB 数值计算中的精度与误差问题:浮点运算、误差传播与数值稳定性
人工智能·算法·matlab
Hhy_11076 小时前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
青少儿编程课堂6 小时前
蓝桥杯青少组图形化省考报名倒计时·全国青少年信息素养大赛·电子学会图形化考级:校园黑白棋人机对战解析
c++·python·算法·bfs·信息学竞赛