Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
numbers-at-most-n-given-digit-set.cpp 940 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-09-09 18:23 +08:00 . Create numbers-at-most-n-given-digit-set.cpp
// Time: O(logn)
// Space: O(logn)
class Solution {
public:
int atMostNGivenDigitSet(vector<string>& D, int N) {
string str_N = to_string(N);
unordered_set<string> set_D(D.cbegin(), D.cend());
int result = 0;
for (int i = 1 ; i < str_N.length() ; ++i) {
result += pow(D.size(), i); // x, xx, xxx
}
int i = 0;
// assume N = 1234, D = 1, 2, 3, 4
for (i = 0 ; i < str_N.length() ; ++i) {
for (const auto& d : D) {
if (d[0] < str_N[i]) {
// 11xx; 121x, 122x; 1231, 1232, 1233;
result += pow(D.size(), str_N.length() - i - 1);
} else {
break;
}
}
if (!set_D.count(string(1, str_N[i]))) {
break;
}
}
return result + int(i == str_N.length()); // 1234
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助