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. java上传组件FileUpload

    如果表单中有文件要上传,也就是有<input type="file" name="name"/> 就需要在form标签中添加enctype=&quo ...

  2. 基于asp.net MVC 的服务器和客户端的交互(三)之客户端请求响应

    一.分析 WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD 操作: GET 用于获取 URI 资源的进行展示,GET ...

  3. table表格实现点击修改 PHP同步数据库 排序

    最近几天在做一个网站,牵扯到一个导航管理的功能!领导说不用作,可是由于自己自作主张,搞了1天的功能.领导说这个导航管理就是不用做!容易牵扯出好多问题来!估摸是客户小的原因! 没办法就把我1天的劳动荒废 ...

  4. [转]oracle 实现插入自增列

    本文转自:http://blog.csdn.net/love_zt_love/article/details/7911104 刚使用oracle,它和sql server 好多地方还是有所不同的,简单 ...

  5. @Register指令

    @Register指令用来创建标记前缀和自定义控件之间的关联,这为开发人员提供了一种在ASP.NET应用程序文件(包括网页.用户控件和母板页)中引用自定义控件的简单方法. <%@Register ...

  6. 【Unity3D】Unity3D之 注册表动态存取游戏存档——PlayerPrefs类

    [Unity3D]Unity3D之 注册表动态存取游戏存档--PlayerPrefs类 1.Unity3D提供了一个用于本地持久化保存与读取的类--PlayerPrefs.工作原理非常简单,以键值对的 ...

  7. [javascript|基本概念|一元操作符]学习笔记

    只操作一个值的操作符 递增/递减操作符 前置型/后置型 前置型:操作符位于操作数前面 e.g.: var a = 30; ++a; 等同于 var a = 30; a = a + 1; --> ...

  8. c语言 char*类型作为中间变量将许多字符串保存到一个数组的问题

    char*是一个字符串指针,如下面的程序value_作为一个中间变量用来在for循环中scanf输入的值的接收者,然后将value_保存到array中,但是一下程序会出现一个问题就是当你跳出这个函数时 ...

  9. C指针赋值

    Node* p = A; Node* f = B; Node* t; t = p; t = f 本人试图让p指向B,但这样操作是不行的.如下图:只是改变了t的指向,p并没有变

  10. 获取Class对象的方法及Class类型的一些讨论

    (1)Class.forName(className) (2)classname.Class 如果是数组,则是数组类型[].class (3)对象.getClass() 例: String path ...