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

Leetcode link

题目简介

这是道简单

题目给我们一个正整数 n,要求我们返回两个正整数组成的数组,这两个正整数之和等于 n,并且数组内的两个整数中都不能有 0

解题思路

直接一个循环判断就好

Javascript

/**
 * @param {number} n
 * @return {number[]}
 */
var getNoZeroIntegers = function(n) {
    const isZeroInside = num => num.toString().includes('0')

    for(let i=1;i<=n;i++) {
        let j = n-i
        if(!isZeroInside(j) && !isZeroInside(i)) {
            return [i, j]
        }
    }
    return []
};

results matching ""

    No results matching ""