力扣挑战赛第9天-No.1317将整数转换为两个无零整数的和

题目描述

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

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

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

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

示例:

输入:n = 69
输出:[1,68]

注意:

2 <= n <= 10^4

解法

枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @param {number} n
* @return {number[]}
*/
let getNoZeroIntegers = function(n) {
let a = 1;
let b = n - 1;
while (a <= b) {
if (!('' + a).includes('0') && !('' + b).includes('0')) { // 经测试,'' + a 比 String(a) 速度要快
return [a, b];
} else {
a++;
b--;
}
}
};
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2020-2021 Sanmu
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信