当前位置:网站首页>Leetcode-2048. Next larger numerical balance
Leetcode-2048. Next larger numerical balance
2022-06-12 05:56:00 【Taoist scholar】
link
2048. The next larger numerical equilibrium number
subject
If the whole number x Satisfy : For each digit d , This digit just stay x It appears that d Time . So integers x It's just one. Numerical equilibrium number .
Give you an integer n , Please return Strictly greater than n Of Minimum numerical equilibrium number .
Example
Example 1:
Input :n = 1
Output :22
explain :
22 Is a numerical equilibrium number , because :
- Numbers 2 appear 2 Time
This is also strictly greater than 1 Minimum numerical equilibrium number of .Example 2:
Input :n = 1000
Output :1333
explain :
1333 Is a numerical equilibrium number , because :
- Numbers 1 appear 1 Time .
- Numbers 3 appear 3 Time .
This is also strictly greater than 1000 Minimum numerical equilibrium number of .
Be careful ,1022 Cannot be used as an answer to this input , Because the numbers 0 More than 0 .Example 3:
Input :n = 3000
Output :3133
explain :
3133 Is a numerical equilibrium number , because :
- Numbers 1 appear 1 Time .
- Numbers 3 appear 3 Time .
This is also strictly greater than 3000 Minimum numerical equilibrium number of
explain
0 <= n <= 10e6
Train of thought ( violence )
Let's first understand the meaning of the topic , That means numbers n Can only appear n Time ,1 Can only appear 1 Time ,2 Can only appear 2 Time , Then you can arrange and combine these numbers , such as 1,22,212,221,3331,122333 wait , It's a question n The maximum is 10e6, So we can list all the minimum equilibrium numbers , Then judge directly
C++ Code
class Solution {
public:
int nextBeautifulNumber(int n) {
vector<int> num{1,22,122,212,221,333,1333,3133,3313,3331,4444,14444,22333,23233,23323,23332,32233,32323,32332,33223,33232,33322,41444,44144,44414,44441,
55555,122333,123233,123323,123332,132233,132323,132332,133223,133232,133322,155555,212333,213233,213323,213332,221333,223133,223313,223331,224444,231233,231323,231332,
232133,232313,232331,233123,233132,233213,233231,233312,233321,242444,244244,244424,244442,312233,312323,312332,313223,313232,313322,321233,321323,
321332,322133,322313,322331,323123,323132,323213,323231,323312,323321,331223,331232,331322,332123,332132,332213,332231,332312,332321,333122,333212,333221,422444,
424244,424424,424442,442244,442424,442442,444224,444242,444422,515555,551555,555155,555515,555551,666666,1224444};
int p = num.size();
for (int i = 0; i < p; i++) {
if (n < num[i]) {
return num[i];
}
}
return 0;
}
};Train of thought two ( violence )
The same violence , But we don't list all the minimum equilibrium numbers , But from n+1 The number of starts to judge one by one , Until the minimum equilibrium number is found .
C++ Code
class Solution {
public:
bool check(int num){
int cnt[10] = {0};
while (num > 0)
{
int temp = num % 10;
cnt[temp]++;
num = num / 10;
}
for (int i = 0; i < 10; i++)
{
if (cnt[i] && cnt[i] != i)
return false;
}
return true;
}
int nextBeautifulNumber(int n) {
int num=n+1;
while(check(num) == false) num++;
return num;
}
};边栏推荐
- China Aquatic Fitness equipment market trend report, technical innovation and market forecast
- XML parameter schema, the same MTK SW version is compatible with two different sets of audio parameters
- Nrf52832 services et fonctionnalités personnalisés
- [grpc development] go language builds simple server and client
- Unable to access this account. You may need to update your password or grant the account permission to synchronize to this device. Tencent corporate email
- log4j 1. X upgrade to 2 Solution of X dependency incompatibility
- China's elastic belt market trend report, technical dynamic innovation and market forecast
- 基于tensorflow的校园绿植识别
- Select gb28181, RTSP or RTMP for data push?
- Annotation configuration of filter
猜你喜欢

EBook list page

Project technical structure

Golang idea configures the agent to improve the speed of packages downloaded by go get

Halcon uses points to fit a plane
![[fastapi] use pycharm to configure and use environment variables for fastapi projects](/img/a5/47cabfed3f12bf70b4b047ef29fc9d.jpg)
[fastapi] use pycharm to configure and use environment variables for fastapi projects

Quickly master makefile file writing

Individual application for ov type SSL certificate

Thesis reading_ Figure neural network gin

Review notes of naturallanguageprocessing based on deep learning

Leetcode simple problem: converting an integer to the sum of two zero free integers
随机推荐
Database Experiment 3: data query
数据库实验三:数据查询
Conversion of Halcon 3D depth map to 3D image
XML参数架构,同一MTK SW版本兼容两套不同的音频参数
[road of system analyst] collection of wrong topics in software engineering chapters
C WMI query remote Win32_ Operatingsystem class
虚函数与纯虚函数的关系
Market trend report, technical innovation and market forecast of Chinese stump crusher
Tabulation skills and matrix processing skills
TCP and UDP introduction
[untitled]
Recursive implementation of exponential, permutation and combination enumerations
Jpg format and XML format files are separated into different folders
nRF52832自定義服務與特性
[go] Viper reads the configuration file in the go project
Stack and queue classic interview questions
Database experiment I: data definition experiment guide
Filter的注解配置
项目管理与统筹
China's alternative sports equipment market trend report, technology dynamic innovation and market forecast