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的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. Table of Contents - Nginx

    Downloading and  Installing Nginx Nginx for Windows Basic Nginx Configuration Configuration File Syn ...

  2. Win7如何快速修复系统

    Windows 7可能是微软迄 今为止最好的桌面操作系统,但是偶尔出现一些问题还是在所难免的.这里教你如何快速修复出现问题的Windows 7系统,每个操作系统都有重新安装的可能,Windows 7也 ...

  3. 1 ubuntu下装setuptools

    setuptools可以让程序员更方便的创建和发布 Python 包,特别是那些对其它包具有依赖性的状况,分享以下我在ubuntu下装setuptools的过程 系统:ubuntu 语言:python ...

  4. php读取目录下的文件

    工作需要写了一个读取指定目录下的文件,并显示列表,点击之后读取文件中的内容 高手拍砖,目录可以自由指定,我这里直接写的是获取当前文件目录下面的所有文件 <?php /** * 读取指定目录下面的 ...

  5. CSS制作彩虹效果

    今天看到一篇文章,说到margin的塌陷的问题,并提供了好几个例子. 自己之前还没怎么遇到过这个问题,正好来研究一下. <div class="box1"></d ...

  6. 10 个超酷的 HTML5/CSS3 应用及源码

    1.CSS3密码强度验证表单,码速表样式 我们在网站上注册会员时,输入一个强大较大的密码会大大增加帐号安全性,那么什么样的密码才比较安全呢?这款CSS3密码强度验证表单插件可以提示你当前输入密码的安全 ...

  7. Java线程面试题 Top 50(转载)

    原文链接:http://www.importnew.com/12773.html 本文由 ImportNew - 李 广 翻译自 javarevisited.欢迎加入Java小组.转载请参见文章末尾的 ...

  8. 《DDNS服务器的搭建和案例解决方法》

    DDNS原理:DNS + DHCP =DDNS DHCP负责ip解析,和分配给客户机ip,ip为随机数. DNS负责域名解析,A记录里记录了每个ip对应的域名. 客户端ip肯定是变化的,不可能一直使用 ...

  9. Android发送请求到不同的Servlet,但都是一个Servlet处理

    错误原因,在Servlet文件中 @WebServlet("/ServletForGETMethod") 与实际的ServletForQUERYMethod 文件名不符. @Web ...

  10. 转:JAVA中this用法小结

    转:http://blog.sina.com.cn/s/blog_6a6badc90100t8hm.html#SinaEditor_Temp_FontName Java关键字this只能用于方法方法体 ...