leetcode 440. K-th Smallest in Lexicographical Order 第k个字典序的数字
来源:互联网 发布:外贸crm软件 编辑:程序博客网 时间:2024/06/07 23:16
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n.
Note: 1 ≤ k ≤ n ≤ 109.
Example:
Input:
n: 13 k: 2
Output:
10
Explanation:
The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
其实和这一道题leetcode 386. Lexicographical Numbers 字典序的排列 是一样的,不过本题是求第k个字典序的数字是哪一个,就这么做吧
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <cmath>using namespace std;class Solution {public: int findKthNumber(int n, int k) { vector<int> res(n, 0); int cur = 1; for (int i = 0; i < n; i++) { res[i] = cur; if (cur * 10 <= n) cur *= 10; else { if (cur >= n) cur /= 10; cur += 1; while (cur % 10 == 0) cur /= 10; } } return res[k-1]; }};
阅读全文
0 0
- leetcode 440. K-th Smallest in Lexicographical Order 第k个字典序的数字
- [leetcode]K-th Smallest in Lexicographical Order
- 440. K-th Smallest in Lexicographical Order
- 440. K-th Smallest in Lexicographical Order
- [LeetCode]440. K-th Smallest in Lexicographical Order
- [Leetcode] 440. K-th Smallest in Lexicographical Order 解题报告
- [leetcode 440]K-th Smallest in Lexicographical Order
- K-th Smallest in Lexicographical Order
- LeetCode-230. Kth Smallest Element in a BST (JAVA)二叉树第k小的数字
- Find k-th smallest element in BST (Order Statistics in BST)
- [LeetCode] k-th permutation 第k个排列
- LeetCode Find K-th Smallest Pair Distance
- [LeetCode] Lexicographical Numbers 字典序排列数字
- LeetCode 230. Kth Smallest Element in a BST(二叉搜索树中的第K个元素)
- 第k个字典序全排列
- 模拟之全排列的第k个字典序
- Find K-th smallest in N sorted arrays
- 字典序—lexicographical order
- spark写数据到装有searchgard的ES集群
- 判断AndroidCPU32位还是64位
- HDU
- 多渠道打包正式签名APK
- 一篇真正教会你开发移动端页面的文章(二)
- leetcode 440. K-th Smallest in Lexicographical Order 第k个字典序的数字
- Numpy 总结
- 训练py-faster-rcnn(caffe)
- BRIEF描述子介绍及其opencv3实现
- 关于FLEXenabled许可
- Wzy—Shader新手入门精讲(一)
- WebView--安卓与HTML的交互
- 数据库控制台宽度和分页长度设置
- 距离及相似度度量方法