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

This commit is contained in:
huangge1199@hotmail.com 2021-04-27 22:22:45 +08:00
parent d136b001b6
commit 58927c1a49
3 changed files with 141 additions and 1 deletions

View File

@ -0,0 +1,86 @@
//无零整数是十进制表示中 不含任何 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]
//
//
// 示例 4
//
// 输入n = 69
//输出[1,68]
//
//
// 示例 5
//
// 输入n = 1010
//输出[11,999]
//
//
//
//
// 提示
//
//
// 2 <= n <= 10^4
//
// Related Topics 数学
// 👍 19 👎 0
package leetcode.editor.cn;
//1317:将整数转换为两个无零整数的和
public class ConvertIntegerToTheSumOfTwoNoZeroIntegers {
public static void main(String[] args) {
//测试代码
Solution solution = new ConvertIntegerToTheSumOfTwoNoZeroIntegers().new Solution();
System.out.println(solution.getNoZeroIntegers(105));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] getNoZeroIntegers(int n) {
int num1 = n < 10 ? 1 : 0;
int mul = 1;
int temp = n;
while (n >= 10) {
int delta = n % 10 != 1 ? 1 : 2;
num1 += delta * mul;
n -= delta;
n /= 10;
mul *= 10;
}
return new int[]{num1, temp - num1};
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,54 @@
<p>「无零整数」是十进制表示中 <strong>不含任何 0</strong>&nbsp;的正整数。</p>
<p>给你一个整数&nbsp;<code>n</code>,请你返回一个 <strong>由两个整数组成的列表</strong> <code>[A, B]</code>,满足:</p>
<ul>
<li><code>A</code><code>B</code>&nbsp;都是无零整数</li>
<li><code>A + B = n</code></li>
</ul>
<p>题目数据保证至少有一个有效的解决方案。</p>
<p>如果存在多个有效解决方案,你可以返回其中任意一个。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>n = 2
<strong>输出:</strong>[1,1]
<strong>解释:</strong>A = 1, B = 1. A + B = n 并且 A 和 B 的十进制表示形式都不包含任何 0 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>n = 11
<strong>输出:</strong>[2,9]
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>n = 10000
<strong>输出:</strong>[1,9999]
</pre>
<p><strong>示例 4</strong></p>
<pre><strong>输入:</strong>n = 69
<strong>输出:</strong>[1,68]
</pre>
<p><strong>示例 5</strong></p>
<pre><strong>输入:</strong>n = 1010
<strong>输出:</strong>[11,999]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10^4</code></li>
</ul>
<div><div>Related Topics</div><div><li>数学</li></div></div>\n<div><li>👍 19</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long