738. Monotone Increasing Digits
内容:
Given a non-negative integer N
, find the largest number that is less than or equal to N
with monotone increasing digits.
(Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x
and y
satisfy x <= y
.)
思路:
只需要找出需要调整的数即可,然后其后面的数全部赋9即可保证最大,分几种情况讨论,从位数低的开始考虑,需要找出的是最后一次从右向左出现递增的位置,然后中间需要加上数字重复的情况,如果数字重复刚好出现在递增的位置,那么也同样移动标记位,但如果没有移动过标记位,则将原数输出
int monotoneIncreasingDigits(int N) {int i = 0; // 记录需要调整的位置int j = 0; // 位数bool flag = 0; // 标记是否是转折点int n = N;int m1 = n % 10; // 末位数n = n / 10;while (n) {j++;int m2 = n % 10;// 倒数第二个数if (m1 < m2) {flag = 1;i = j;} else {if (flag && m1 == m2) i = j;else flag = 0;}m1 = m2;n /= 10;}return i == 0 ? N : ((N / int(pow(10, i)) - 1) * pow(10, i) + int(pow(10, i)) - 1);
}int main() {int N;while (cin >> N) {cout << monotoneIncreasingDigits(N) << endl;}system("pause");return 0;
}
738. Monotone Increasing Digits相关推荐
- Leetcode 738. Monotone Increasing Digits
原题链接:https://leetcode.com/problems/monotone-increasing-digits/description/ 描述: Given a non-negative ...
- 《每日一题》738. Monotone Increasing Digits 单调递增的数字
给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x 和 y 满足 x <= y 时,我们称这个整数是单 ...
- leetcode 738. Monotone Increasing Digits | 738. 单调递增的数字(Java)
题目 https://leetcode.com/problems/monotone-increasing-digits/ 题解 先把 num 的每一位转化为数组. 从左往右找第一个 前>后 的位 ...
- leetcode 926. Flip String to Monotone Increasing | 926. 将字符串翻转到单调递增(前缀和)
题目 https://leetcode.com/problems/flip-string-to-monotone-increasing/ 题解 方法1:前缀和 class Solution {publ ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
- Leetcode题目练习总结(持续更新......)
Leetcode题目练习 数组 1.两数之和 26. 删除排序数组中的重复项 27. 移除元素 35.搜索插入位置 53.最大子序列 66.加一 88.合并两个有序数组 118.杨辉三角 119.杨辉 ...
- 【HDU - 5187】zhx's contest (快速幂+ 快速乘,模板)
题干: 2018百度之星复赛晋级名单出炉(增加20%晋级名额)~ zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- C#LeetCode刷题之#896-单调数列(Monotonic Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3760 访问. 如果数组是单调递增或单调递减的,那么它是单调的. ...
- 【HDU5187】contest
真的没有什么会写的东西了QAQ 原题: As one of the most powerful brushes, zhx is required to give his juniors n probl ...
最新文章
- shell函数的返回值不能大于255的问题
- 【数学和算法】初识卡尔曼滤波器(五)
- Sql Server2005性能
- Linux Supervisor 守护进程基本配置
- 奈雪的茶回应违规操作事件:虚心接受意见并及时改进
- 嵌入式开发环境搭建:开发板tftp下载环境搭建
- Python+Opencv简易车牌识别(二):形态学运算,HSV颜色空间筛选与图像分割
- 蓝桥杯:十六进制转八进制
- 分组数据筛选(group by后取出每组的第n条数据)
- 拓端tecdat|视频:R语言中的Stan概率编程MCMC采样的贝叶斯模型
- 如何打造一款可靠的WAF(Web应用防火墙)
- 【考研】2021年哈尔滨工业大学计算机考研「复试」经验贴
- 土方工程量计算表格excel_市政道路土方excel计算表(含公式)
- Linux学习笔记-shell脚本-log脚本函数
- 机器人瓦力漫威_章节目录 86、机器人瓦力
- 电子报刊制作软件之比较
- 从红队视角看AWD攻击
- 二、全国计算机三级数据库考试——理论知识总结(填空题)
- JZOJ.5331【NOIP2017模拟8.23】壕游戏
- 谷歌浏览器如何查css,谷歌浏览器查看编辑元素CSS样式_谷歌工具
热门文章
- 深度优先和广度优先的理解
- Fred Books图集
- 刷脸签到python代码_背景提升|“刷脸”时代,如何运用Python实现人脸识别?
- c语言二级考试试卷,二级 C语言考试试卷
- Gitee码云remote: error: File: , exceeds 100.00 MB 踩坑指南
- 2.5 浮点数的表示及运算(浮点数表示、规格化)
- windows下OpenSSL安装
- 你不得不关注的5 大Android 开发技术——2020
- swagger遇到的坑
- C++无符号数比较大小