将整数转换为两个无零整数的和

「无零整数」是十进制表示中 不含任何 0 的正整数。

给你一个整数 n,请你返回一个 由两个整数组成的列表 [A, B],满足:

A 和 B 都是无零整数
A + B = n
题目数据保证至少有一个有效的解决方案。

如果存在多个有效解决方案,你可以返回其中任意一个。

示例 1:

输入:n = 2
输出:[1,1]
解释:A = 1, B = 1. A + B = n 并且 A 和 B 的十进制表示形式都不包含任何 0 。
示例 2:

输入:n = 11
输出:[2,9]
示例 3:

输入:n = 10000
输出:[1,9999]

思路

1,从1开始到n/2结束遍历数组

2,判断i是否包含0,入不包含,则判断n-i 是否包含0

代码

public int[] getNoZeroIntegers(int n) {
        if(n < 1){
            return null;
        }
        int[] result = new int[2];
        for (int i = 1; i <= n>> 1; i++) {
            boolean containZero = isContainZero(i);
            if(!containZero){
                int next = n - i;
                containZero = isContainZero(next);
                if(!containZero){
                    result[0] = i;
                    result[1] = next;
                    break;
                }

            }
        }
        return result;
    }
    private boolean isContainZero(int n){
        if(n < 10){
            return false;
        }
        while (n > 0){
            int num = n % 10;
            if(num == 0){
                return true;
            }
            n /= 10;
        }
        return false;
    }

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

原文地址:https://www.cnblogs.com/dongma/p/14219547.html