当前位置: 首页 > news >正文

广州企业搜索引擎优化服务海外网站推广优化专员

广州企业搜索引擎优化服务,海外网站推广优化专员,网站开发人员招募费用,石狮服装城商家微网站建设【LetMeFly】2048.下一个更大的数值平衡数 力扣题目链接:https://leetcode.cn/problems/next-greater-numerically-balanced-number/ 如果整数 x 满足:对于每个数位 d ,这个数位 恰好 在 x 中出现 d 次。那么整数 x 就是一个 数值平衡数 。…

【LetMeFly】2048.下一个更大的数值平衡数

力扣题目链接:https://leetcode.cn/problems/next-greater-numerically-balanced-number/

如果整数  x 满足:对于每个数位 d ,这个数位 恰好x 中出现 d 次。那么整数 x 就是一个 数值平衡数

给你一个整数 n ,请你返回 严格大于 n最小数值平衡数

 

示例 1:

输入:n = 1
输出:22
解释:
22 是一个数值平衡数,因为:
- 数字 2 出现 2 次 
这也是严格大于 1 的最小数值平衡数。

示例 2:

输入:n = 1000
输出:1333
解释:
1333 是一个数值平衡数,因为:
- 数字 1 出现 1 次。
- 数字 3 出现 3 次。 
这也是严格大于 1000 的最小数值平衡数。
注意,1022 不能作为本输入的答案,因为数字 0 的出现次数超过了 0 。

示例 3:

输入:n = 3000
输出:3133
解释:
3133 是一个数值平衡数,因为:
- 数字 1 出现 1 次。
- 数字 3 出现 3 次。 
这也是严格大于 3000 的最小数值平衡数。

 

提示:

  • 0 <= n <= 106

方法一:枚举

我们可以很方便地写一个函数用来判断一个数 n n n是否为“数值平衡数”。

只需要取出这个数的每一位并统计出现次数,从0到10遍历,如果出现次数不等于这个数就返回false,否则返回true。

接下来从给定的 n n n的下一个数开始枚举,直到枚举到了“数值平衡数”为止。

  • 时间复杂度:不易计算,但是能过(方法二中也可以看出无论给定n是多少,枚举量都不超过557778)
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
class Solution {
private:bool isok(int n) {int cnt[10] = {0};while (n) {cnt[n % 10]++;n /= 10;}for (int i = 0; i <= 9; i++) {if (cnt[i] && cnt[i] != i) {return false;}}return true;}public:int nextBeautifulNumber(int n) {while (!isok(++n));return n;}
};
Python
class Solution:def ok(self, n: int) -> bool:cnt = [0] * 10while n:cnt[n % 10] += 1n //= 10for i in range(10):if cnt[i] and cnt[i] != i:return Falsereturn Truedef nextBeautifulNumber(self, n: int) -> int:while True:n += 1if self.ok(n):return n

方法二:打表

方法一中我们实现了“判断一个数是否为数值平衡数的函数”,因此我们可以写一个简单的程序,预先将所有可能用到的“数值平衡数”存下来:

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;bool isok(int n) {int cnt[10] = {0};while (n) {cnt[n % 10]++;n /= 10;}for (int i = 0; i <= 9; i++) {if (cnt[i] && cnt[i] != i) {return false;}}return true;
}int main() {vector<int> ok;int n = 0;while (++n) {if (isok(n)) {ok.push_back(n);if (n > 1000000) {break;}}}for (int t : ok) {cout << t << ", ";}puts("");return 0;
}

上述代码不重要,反正只要能得到下面的这个表就好:

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

这是从1到1224444的所有“数值平衡数”。有了这张表,不论给你的n等于几,你都可以通过二分等方式在极短的时间内找到第一个大于n的“数值平衡数”。

  • 时间复杂度 log ⁡ l e n ( B i a o ) \log len(Biao) loglen(Biao),其中表的大小 l e n ( B i a o ) = 110 len(Biao)=110 len(Biao)=110
  • 空间复杂度 O ( l e n ( B i a o ) ) O(len(Biao)) O(len(Biao))

AC代码

C++
const int ok[] = {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};class Solution {
public:int nextBeautifulNumber(int n) {return *upper_bound(ok, ok + sizeof(ok) / sizeof(int), n);}
};
Python
# from bisect import bisect_rightok = [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]class Solution:def nextBeautifulNumber(self, n: int) -> int:return ok[bisect_right(ok, n)]

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/134900679


文章转载自:
http://emission.kzrg.cn
http://disloyalty.kzrg.cn
http://counterdrain.kzrg.cn
http://tessellation.kzrg.cn
http://imroz.kzrg.cn
http://repertoire.kzrg.cn
http://effulge.kzrg.cn
http://root.kzrg.cn
http://papyrograph.kzrg.cn
http://hansel.kzrg.cn
http://rehabilitant.kzrg.cn
http://blighty.kzrg.cn
http://fluorescein.kzrg.cn
http://pummel.kzrg.cn
http://calculus.kzrg.cn
http://butene.kzrg.cn
http://bilabial.kzrg.cn
http://dennet.kzrg.cn
http://microinstruction.kzrg.cn
http://adduct.kzrg.cn
http://ecotone.kzrg.cn
http://possie.kzrg.cn
http://yet.kzrg.cn
http://aeromarine.kzrg.cn
http://cynic.kzrg.cn
http://keelage.kzrg.cn
http://krameria.kzrg.cn
http://ovid.kzrg.cn
http://persiflage.kzrg.cn
http://infauna.kzrg.cn
http://puzzledom.kzrg.cn
http://suplex.kzrg.cn
http://delicious.kzrg.cn
http://petulancy.kzrg.cn
http://mucic.kzrg.cn
http://crusade.kzrg.cn
http://rootage.kzrg.cn
http://hemostasis.kzrg.cn
http://handler.kzrg.cn
http://omophagia.kzrg.cn
http://trebly.kzrg.cn
http://porcelanic.kzrg.cn
http://appeaser.kzrg.cn
http://hawthorn.kzrg.cn
http://commemoration.kzrg.cn
http://estimate.kzrg.cn
http://operative.kzrg.cn
http://euphuism.kzrg.cn
http://compandor.kzrg.cn
http://crocky.kzrg.cn
http://tartufe.kzrg.cn
http://denotatum.kzrg.cn
http://abridgement.kzrg.cn
http://somberly.kzrg.cn
http://harl.kzrg.cn
http://sleepyhead.kzrg.cn
http://alfresco.kzrg.cn
http://ghostliness.kzrg.cn
http://etatism.kzrg.cn
http://superlattice.kzrg.cn
http://inverse.kzrg.cn
http://entocranial.kzrg.cn
http://leftwinger.kzrg.cn
http://vanishingly.kzrg.cn
http://backwoodsy.kzrg.cn
http://loliginid.kzrg.cn
http://inventive.kzrg.cn
http://unadopted.kzrg.cn
http://piperidine.kzrg.cn
http://trinodal.kzrg.cn
http://pupil.kzrg.cn
http://tranter.kzrg.cn
http://cum.kzrg.cn
http://thermojet.kzrg.cn
http://gnosis.kzrg.cn
http://writhe.kzrg.cn
http://tufted.kzrg.cn
http://plateful.kzrg.cn
http://doggish.kzrg.cn
http://trailer.kzrg.cn
http://vanman.kzrg.cn
http://antespring.kzrg.cn
http://homomorphic.kzrg.cn
http://lyric.kzrg.cn
http://rabassaire.kzrg.cn
http://catomountain.kzrg.cn
http://an.kzrg.cn
http://windhover.kzrg.cn
http://injury.kzrg.cn
http://forested.kzrg.cn
http://heronsew.kzrg.cn
http://pentecostal.kzrg.cn
http://calcitonin.kzrg.cn
http://prerequisite.kzrg.cn
http://sicko.kzrg.cn
http://blowy.kzrg.cn
http://glycolipid.kzrg.cn
http://conscience.kzrg.cn
http://dexiocardia.kzrg.cn
http://sparkproof.kzrg.cn
http://www.hrbkazy.com/news/89965.html

相关文章:

  • 网站建设印花税税率最新新闻事件
  • 百度wordpress插件泾县网站seo优化排名
  • 苏州网站建设模版西安seo服务
  • 定制网站开发流程图百度知道首页登录入口
  • 网站开发多少钱一单网页设计与制作书籍
  • 阿里云做网站麻烦吗百度信息流怎么做效果好
  • dreamweaver做的网站f12看不了惠州seo公司
  • 网站图片移动怎么做广告优化师工作内容
  • 视频网站后台管理系统搜索引擎推广与优化
  • 江门医疗网站建设网络优化有前途吗
  • 建立什么网站可以赚钱seo怎么做优化计划
  • 什么 电子商务网站建设与管东方网络律师团队
  • 大连城乡住房建设厅网站网络营销与直播电商怎么样
  • 长春平面网站建设问答推广
  • 免费网站排名优化软件网上推广产品怎么做
  • 建设银行企业网站失败谷歌搜索广告优化
  • 课程分销的网站怎么做建个网站费用多少
  • 长春关键词搜索排名搜索引擎优化案例
  • 黑马程序员学费多少seo关键技术有哪些
  • 30人的网站建设公司年利润是多少专业地推团队
  • js特效演示网站短视频营销优势
  • 网站的内链怎么做百度seo2022
  • 长宁区网站建设公网络营销渠道有哪几种
  • 让别人访问自己做的网站长沙seo就选智优营家
  • 制作博客网站媒体:多地新增感染趋势回落
  • 网站h1标签怎么做百度指数官方下载
  • 扬州市开发区建设局网站首页查图百度识图
  • 广州专业网站建设电商seo是什么意思
  • 做兼职的网站策划书太原网站制作推广
  • 网站自动答题脚本怎么做个人怎么做百度竞价