ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 2094. Finding 3-Digit Even Numbers

2022-08-08 03:00:27  阅读:166  来源: 互联网

标签:Even digits Digit integers int ++ count 2094 array


原题链接在这里:https://leetcode.com/problems/finding-3-digit-even-numbers/

题目:

You are given an integer array digits, where each element is a digit. The array may contain duplicates.

You need to find all the unique integers that follow the given requirements:

  • The integer consists of the concatenation of three elements from digits in any arbitrary order.
  • The integer does not have leading zeros.
  • The integer is even.

For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.

Return a sorted array of the unique integers.

Example 1:

Input: digits = [2,1,3,0]
Output: [102,120,130,132,210,230,302,310,312,320]
Explanation: All the possible integers that follow the requirements are in the output array. 
Notice that there are no odd integers or integers with leading zeros.

Example 2:

Input: digits = [2,2,8,8,2]
Output: [222,228,282,288,822,828,882]
Explanation: The same digit can be used as many times as it appears in digits. 
In this example, the digit 8 is used twice each time in 288, 828, and 882. 

Example 3:

Input: digits = [3,7,5]
Output: []
Explanation: No even integers can be formed using the given digits.

Constraints:

  • 3 <= digits.length <= 100
  • 0 <= digits[i] <= 9

题解:

From 100 to 999, for each even number i, check its digits all appear in the map.

The map is precalculated by digits array.

Time Complexity: O(n). n = 900/2.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int[] findEvenNumbers(int[] digits) {
 3         int [] count = new int[10];
 4         for(int d : digits){
 5             count[d]++;
 6         }
 7         
 8         List<Integer> list = new ArrayList<>();
 9         for(int i = 100; i < 1000; i+=2){
10             int a = i % 10; 
11             int b = (i / 10) % 10;
12             int c = i / 100;
13             
14             count[a]--;
15             count[b]--;
16             count[c]--;
17             if(count[a] >=0 && count[b] >= 0 && count[c] >= 0){
18                 list.add(i);
19             }
20             
21             count[a]++;
22             count[b]++;
23             count[c]++;
24         }
25         
26         int [] res = new int[list.size()];
27         for(int i = 0; i < res.length; i++){
28             res[i] = list.get(i);
29         }
30         
31         return res;
32     }
33 }

 

标签:Even,digits,Digit,integers,int,++,count,2094,array
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16560422.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有