首页 > 其他分享 >leetcode-1317-easy

leetcode-1317-easy

时间:2023-03-29 20:58:15浏览次数:58  
标签:1317 integers return No int result easy isNoZeroInteger leetcode

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 = 9 = n.
Note that there are other valid answers as [8, 3] that can be accepted.
Constraints:

2 <= n <= 104

思路一:随机选数字

    public int[] getNoZeroIntegers(int n) {
        int[] result = new int[2];

        while (true) {
            int x = (int) (Math.random() * n);

            if (isNoZeroInteger(x) && isNoZeroInteger(n - x)) {
                result[0] = x;
                result[1] = n - x;
                break;
            }
        }

        return result;
    }

    public boolean isNoZeroInteger(int n) {
        if (n == 0) {
            return false;
        }
        while (n > 0) {
            int x = n % 10;
            n /= 10;
            if (x == 0) {
                return false;
            }
        }
        return true;
    }

标签:1317,integers,return,No,int,result,easy,isNoZeroInteger,leetcode
From: https://www.cnblogs.com/iyiluo/p/17270268.html

相关文章

  • leetcode-1009-easy
    ComplementofBase10IntegerThecomplementofanintegeristheintegeryougetwhenyouflipallthe0'sto1'sandallthe1'sto0'sinitsbinaryreprese......
  • 【LeetCode】35.搜索插入位置
    题目描述  解法思路:二分查找classSolution{public:intsearchInsert(vector<int>&nums,inttarget){intleft=0,right=nums.size()-1......
  • 【LeetCode】278.第一个错误的版本
    题目描述  解法思路:二分查找注意:当第一个 isBadVersion(mid)的结果为true时,得到第一个错误的版本//TheAPIisBadVersionisdefinedforyou.//boolisBadVe......
  • leetcode 176
    leetcode176第二高的薪水,查第二高的人的信息1、使用ifnull(exp1,exp2)函数,limitoffset子句 selectifnull((selectdistinctsalaryfromEmployeeorderb......
  • LeetCode 101.对称二叉树
    1.题目:给你一个二叉树的根节点 root ,检查它是否轴对称。 示例1:输入:root=[1,2,2,3,4,4,3]输出:true2.代码:方法一:递归实现/***Definitionforabinarytreenode.......
  • #yyds干货盘点# LeetCode程序员面试金典:盛最多水的容器
    题目:给定一个长度为n的整数数组 height 。有 n 条垂线,第i条线的两个端点是 (i,0) 和 (i,height[i]) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以......
  • #yyds干货盘点# LeetCode面试题:不同路径
    1.简述:一个机器人位于一个mxn 网格的左上角(起始点在下图中标记为“Start”)。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Fin......
  • Leetcode81. 搜索旋转排序数组 II
    classSolution{public:boolcheck(vector<int>&nums,inttarget,intl,intr)//[l,r]区间查找target{while(l<r){intmid=(......
  • 3月更新!EasyOps®全平台超10项功能升级,您的需求在其中~
    又到了每月产品盘点时刻,12大新功能上线和升级优化,涉及Hyperlnsight超融合持续观测平台、DevOps持续交付平台、AutoOps自动化运维平台、ITSM服务平台,在不断的技术创新过程中,......
  • 【单调队列】LeetCode 239. 滑动窗口最大值
    题目链接239.滑动窗口最大值思路单调队列的使用方法,可以参考【单调队列】LeetCode面试题59-II.队列的最大值在本题中将滑动窗口的移动看作往队列中放数和取数的过......