Careercup - Google面试题 - 4857362737266688
2014-05-04 00:10
原题:
Write a function return an integer that satisfies the following conditions:
) positive integer
) no repeated digits, eg., (valid), (invalid)
) incremental digit sequence, eg., (valid) (invalid)
) the returned integer MUST be the smallest one that greater than the input. eg., input=, return= function signature could be like this:
String nextInteger(String input)
题目:请设计一个函数nextInteger(),以字符串形式的整数为输入,返回一个字符串形式的整数。要去这个整数:1. 是正整数 2. 任何数位的数字不能相同 3. 各个数位由高到低必须严格递增 4. 输出的数字必须是满足上述三条件并且比输入数大的最小的数。其实用英语描述,一个“next”就表达这个意思了。
解法:分析这三个条件,可以很快发现满足条件的数是很少的。各个位的数不能重复,并且还得保持升序,这样的话可以数位动态规划的思想找出所有的数,并进行排序。对于得到的所有符合条件的数组成的数组,进行upper_bound二分查找就能得到题目要求的数了。
代码:
// http://www.careercup.com/question?id=4857362737266688
#include <algorithm>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std; class Solution {
public:
Solution() {
generateNumbers();
}; string nextInteger(string input) {
int n1, n2;
vector<int>::iterator it;
stringstream ss; ss << input;
ss >> n1;
it = upper_bound(arr.begin(), arr.end(), n1);
if (it == arr.end()) {
n2 = n1;
} else {
n2 = *it;
} string output = to_string(n2); return output;
};
private:
vector<int> arr; void generateNumbers() {
queue<int> q;
int n, n1;
int i, d; arr.push_back();
q.push();
while (!q.empty()) {
n = q.front();
q.pop();
d = n % ;
for (i = d + ; i <= ; ++i) {
n1 = n * + i;
arr.push_back(n1);
q.push(n1);
}
}
};
};
Careercup - Google面试题 - 4857362737266688的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- 【trim()】去掉字符串开头和结尾的空格,防止不必要的空格导致的错误。
去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String a=" abc"; Strin ...
- C#垃圾回收机制
C#属于托管的面相对象的语言,内存回收机制就是一个代表, C#有一套类似"全自动"的垃圾回收机制,也就是虚拟机会自动来判断执行内存的回收, 我们一般常用的Dispose(),Usi ...
- Part 9 Sorting data in AngularJS
To sort the data in Angular 1. Use orderBy filter {{ orderBy_expression | orderBy : expression : ...
- Warning: Permanently added '...' (RSA) to the list of known hosts --Windows下git bash 警告处理
原链接地址 StackOverflow 处理方法: 创建文件~/.ssh/config, 此处对应windows当前用户目录下的.ssh文件夹 增加如下语句 UserKnownHostsFile ~/ ...
- OSPF LSA的详解
LSA类型的配置与查看 1基本配置 R1(config)#NO IP DO LO R1(config)#NO ENAble PAssword R1(config)#LINe COnsole 0 R1( ...
- 谷歌浏览器Chrome不再支持showModalDialog的解决办法
问题重现 弹出窗口编码: JavaScript 0 1 2 3 4 5 6 7 var obj = new Object(); var retval = window.showModalDia ...
- button等按钮onclientclick事件失效
如果确定JS没写错 第一种方法: 在JS方法最后return false; 调用方法前加上return 第二种方法: 在JS方法最后event.returnValue=false; 附加:event. ...
- OpenGL第8,9讲小结
这两节,透明度和物体的3D运动,主要集中在第9讲,因为第9讲也用到了通过Alpha值来调整透明度的地方. 因为要模拟星星,所以要创建的四边形需要很多,例子中创建了50个正方形.因为每个星星的属性都差不 ...
- 利用word2vec对关键词进行聚类
1.收集预料 自己写个爬虫去收集网页上的数据. 使用别人提供好的数据http://www.sogou.com/labs/dl/ca.html 2.对预料进行去噪和分词 我们需要content其中的值, ...
- git 的使用(4)-git暂缓区工作区原理和修改删除等命令
文章转载自:http://blog.csdn.net/think2me/article/details/39056379 博主说未经本人允许,不得转载,那就不贴了,拷贝关键部分作备忘 1. 暂存区是G ...