738. Monotone Increasing Digits
来源:互联网 发布:白兰js 编辑:程序博客网 时间:2024/05/21 06:03
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 = 10Output: 9Example 2:Input: N = 1234Output: 1234Example 3:Input: N = 332Output: 299
这题暴力解法就是一个个数check,超时。
贪心算法,构造需要的解去试。
key point就是,如果已知现有的部分是‘123’,下一个数字最大可以是多少了?比如5,那么就要保证1235…5 <= N。在这个要求下去找每一位可能的数字就行。
代码:
class Solution {public: int monotoneIncreasingDigits(int N) { string res = ""; string num = to_string(N); for (int i = 0; i < num.size(); i++) { int flag = false; for (char c = '1'; c <= '9'; c++) { string temp= res; while (temp.size() < num.size()) temp += c; if (temp > num) { res += c - 1; flag = true; break; } } if (flag == false) res += '9'; } return stoi(res); }};
阅读全文
0 0
- 738. Monotone Increasing Digits
- 738. Monotone Increasing Digits
- Leetcode 738. Monotone Increasing Digits
- LWC 61:738. Monotone Increasing Digits
- LeetCode刷题 | 738. Monotone Increasing Digits
- LeetCode—738. Monotone Increasing Digits
- leetcode weekly contest 61 ( 738. Monotone Increasing Digits )
- 【算法分析与设计】【第十四周】738. Monotone Increasing Digits
- Leetcode 738. Monotone Increasing Digits(算法分析week16)
- Monotone Increasing Digits问题及解法
- dp+tire+Monotone queue
- monotone queue(单调队列)
- hdu 2195 Monotone SE Min 【dp】
- Increasing Sequence
- Increasing Subsequences
- Increasing Subsequences
- FZU 1894 志愿者选拔【单调队列】【monotone decreasing queue】
- 11687 - Digits
- Leetcode -- Minimum Path Sum
- Ubuntu16.04安装 有道词典.deb
- 爬取百度对应词汇页面量
- CockroachDB 中的 Visitor模式加composite模式
- 架构师总结
- 738. Monotone Increasing Digits
- XMind8u6
- C++反汇编第二讲,不同作用域下的构造和析构的识别
- Android JVM, JRE, JDK, SDK之间的关系
- Suse12网络设置
- 搭建dns服务器-域名解析
- scrapy 实战练习
- 知乎日报 API 分析(如何规范api设计)
- thinkphp5行为扩展实现html静态缓存设置