原题链接:https://leetcode.com/problems/monotone-increasing-digits/description/

描述:

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.)

Example 1:
Input: N = 10
Output: 9
Example 2:
Input: N = 1234
Output: 1234
Example 3:
Input: N = 332
Output: 299
Note: N is an integer in the range [0, 10^9].


Solution:

本题依然是数学问题,只需要找出需要调整的数即可,然后其后面的数全部赋9即可保证最大,分几种情况讨论,从位数低的开始考虑,需要找出的是最后一次从右向左出现递增的位置,然后中间需要加上数字重复的情况,如果数字重复刚好出现在递增的位置,那么也同样移动标记位,但如果没有移动过标记位,则将原数输出,具体代码如下所示:

#include <iostream>
#include <cmath>
using namespace std;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;
}

Leetcode 738. Monotone Increasing Digits相关推荐

  1. leetcode 738. Monotone Increasing Digits | 738. 单调递增的数字(Java)

    题目 https://leetcode.com/problems/monotone-increasing-digits/ 题解 先把 num 的每一位转化为数组. 从左往右找第一个 前>后 的位 ...

  2. 738. Monotone Increasing Digits

    内容: Given a non-negative integer N, find the largest number that is less than or equal to N with mon ...

  3. 《每日一题》738. Monotone Increasing Digits 单调递增的数字

    给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x 和 y 满足 x <= y 时,我们称这个整数是单 ...

  4. 【暴力】LeetCode 300. Longest Increasing Subsequence

    LeetCode 300. Longest Increasing Subsequence Solution1:我的答案 暴力搜索,时间复杂度O(n2)O(n2)O(n^2) class Solutio ...

  5. leetcode 926. Flip String to Monotone Increasing | 926. 将字符串翻转到单调递增(前缀和)

    题目 https://leetcode.com/problems/flip-string-to-monotone-increasing/ 题解 方法1:前缀和 class Solution {publ ...

  6. LeetCode 300. Longest Increasing Subsequence--O(n log n)--Java,C++,Python解法

    题目地址: Given an unsorted array of integers, find the length of longest increasing subsequence. Exampl ...

  7. leetcode 402. Remove K Digits | 402. 移掉 K 位数字(单调栈)

    题目 https://leetcode.com/problems/remove-k-digits/ 题解 本题考察对问题的抽象能力,多写几个例子可以发现,这是一个单调栈问题,维护一个单调不减栈. cl ...

  8. leetcode 300. Longest Increasing Subsequence | 300. 最长递增子序列(动态规划)

    题目 https://leetcode.com/problems/longest-increasing-subsequence/ 题解 难得有官方题解的一道题. 参考:https://leetcode ...

  9. [LeetCode] 402. Remove K Digits Java

    题目: Given a non-negative integer num represented as a string, remove k digits from the number so tha ...

最新文章

  1. 图片过渡效果一例 使用bitmapData操作
  2. Linux系统时间和时序,什么是时序竞态 Linux系统时序竞态问题分析
  3. 从零开始-小程序采坑记录
  4. UIViewController详解
  5. 2021泰州高考什么时候查成绩查询,2021年泰州学院高考录取结果什么时候出来及查询系统入口...
  6. 麦克风阵列声源定位 GCC-PHAT
  7. Web服务器与Servlet容器初步认识
  8. C语言解决约瑟夫环问题 详细注释
  9. 基本概念学习--TaskList(任务列表)
  10. java语言特点概述
  11. AHCI和IDE区别,和在目前系统中设置 AHCI - 摘自网络
  12. php网站访问卡顿,网站反应慢,经常卡顿是什么原因?怎么解决?
  13. ZK实现SASL认证+Kafka连接ZK
  14. Java学习笔记(二)
  15. CentOS 6.5 安装使用iscsi
  16. PLSQL:导出表结构、导出整个表、导出表部分数据、复制表、导入表
  17. 使用CSS实现文字的两端对齐方式
  18. ad10搜索快捷键_AD10设计所需快捷键(席雪晴)
  19. 如果我来设计“有道词典”单词本同步
  20. 通过rancher部署loki-distributed loki日志高可用集群 helm方式部署

热门文章

  1. windows渗透常用命令
  2. 赛门铁克企业级防病毒产品 》(Symantec Endpoint Protection)V11.0.6005.562 简体中文版[压缩包]
  3. Python论文复现:VMD之自适应选择分解模态数K值
  4. OC门电路和OD门电路的区别
  5. Arduino UNO R3 (CH340G)基础篇-引脚
  6. Markdown(Typora)学习记录
  7. OpenDDS ConnextDDS FastDDS是兄弟吗
  8. 公众号快速吸粉提高微信分销商城的转化率案例分析
  9. 详解 Laravel 中的依赖注入和 IoC
  10. 【蓝桥】做题笔记及心得总结(基础篇)