日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Leetcode 738. Monotone Increasing Digits

發布時間:2023/12/29 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode 738. Monotone Increasing Digits 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原題鏈接: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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。