f*e
3 楼
对头
f*e
4 楼
要求写代码的,这只是一个input
f*e
5 楼
我的思路是进行全排列,然后排序,最笨的方法
S*I
6 楼
next_permutation in C++ std will do it, a sample implementation is shown below:
http://wordaligned.org/articles/next-permutation
【在 f********e 的大作中提到】
: 要求写代码的,这只是一个input
http://wordaligned.org/articles/next-permutation
【在 f********e 的大作中提到】
: 要求写代码的,这只是一个input
f*e
7 楼
谢谢!!
p*n
9 楼
I don't think we need do permutation.
how about this solution?
int FindNext( int x)
{
int base = 1;
int current = x % 10 ;
int temp = x /10;
while (temp)
{
if (temp %10 {
int y = (current - temp%10)*9*base ;
return x+y;
}
else {
base *= 10;
current = temp%10 ;
temp /=10;
}
}
return 0;
}
how about this solution?
int FindNext( int x)
{
int base = 1;
int current = x % 10 ;
int temp = x /10;
while (temp)
{
if (temp %10
int y = (current - temp%10)*9*base ;
return x+y;
}
else {
base *= 10;
current = temp%10 ;
temp /=10;
}
}
return 0;
}
q*t
10 楼
import java.util.ArrayList;
import java.util.List;
public class NextLargerNumberWithSameDigits {
public static void main(String[] args) throws Exception {
int a = 1245963;
System.out.println(findnext(a));
a = 698754;
System.out.println(findnext(a));
}
public static int findnext(int a) throws Exception {
List d = new ArrayList();
while (a != 0) {
d.add(a % 10);
a /= 10;
}
Integer[] digits = d.toArray(new Integer[d.size()]);
// find the first digit that is out of order
// digits[i] < digits[i-1] >= digits[i-2] >= ... >= digits[1] >= digits[0]
int i;
for (i = 1; i < digits.length; i++) {
if (digits[i] < digits[i - 1])
break;
}
if (i >= digits.length) {
throw new Exception("Not found!");
}
// find in digits[i-1], digits[i-2], ..., digits[1], digits[0]
// the smallest digit that is larger than digits[i]
// Because digits[i] is out of order, i.e. digits[i-1]>digits[i],
// it's guaranteed to find one
for (int j = 0; j <= i - 1; j++) {
if (digits[j] > digits[i]) {
int t = digits[j];
digits[j] = digits[i];
digits[i] = t;
break;
}
}
// reverse digits[i-1], digits[i-2], ..., digits[1], digits[0]
for (int j = 0; j < i / 2; j++) {
int t = digits[j];
digits[j] = digits[i - 1 - j];
digits[i - 1 - j] = t;
}
int b = digits[digits.length - 1];
for (int j = digits.length - 2; j >= 0; j--) {
b = b * 10 + digits[j];
}
return b;
}
}
import java.util.List;
public class NextLargerNumberWithSameDigits {
public static void main(String[] args) throws Exception {
int a = 1245963;
System.out.println(findnext(a));
a = 698754;
System.out.println(findnext(a));
}
public static int findnext(int a) throws Exception {
List
while (a != 0) {
d.add(a % 10);
a /= 10;
}
Integer[] digits = d.toArray(new Integer[d.size()]);
// find the first digit that is out of order
// digits[i] < digits[i-1] >= digits[i-2] >= ... >= digits[1] >= digits[0]
int i;
for (i = 1; i < digits.length; i++) {
if (digits[i] < digits[i - 1])
break;
}
if (i >= digits.length) {
throw new Exception("Not found!");
}
// find in digits[i-1], digits[i-2], ..., digits[1], digits[0]
// the smallest digit that is larger than digits[i]
// Because digits[i] is out of order, i.e. digits[i-1]>digits[i],
// it's guaranteed to find one
for (int j = 0; j <= i - 1; j++) {
if (digits[j] > digits[i]) {
int t = digits[j];
digits[j] = digits[i];
digits[i] = t;
break;
}
}
// reverse digits[i-1], digits[i-2], ..., digits[1], digits[0]
for (int j = 0; j < i / 2; j++) {
int t = digits[j];
digits[j] = digits[i - 1 - j];
digits[i - 1 - j] = t;
}
int b = digits[digits.length - 1];
for (int j = digits.length - 2; j >= 0; j--) {
b = b * 10 + digits[j];
}
return b;
}
}
e*s
12 楼
public static int FindNext(int x)
{
ArrayList tempa = new ArrayList();
int current = x % 10;
int temp = x / 10;
int result;
while (temp > 0)
{
tempa.Add(current);
//find the falling edge from right to left, e.g. "1321" 1->2
->3->(1)falling edge
if (current > temp % 10)
{
//switch the fall edge with first element which greater
than itself
for (int i = 0; i < tempa.Count; i++)
if ((temp % 10) < (int)tempa[i])
{
result = (temp / 10) * 10 + (int)tempa[i];
tempa[i] = temp % 10;
int j = 0;
//put the elements back to the result
while (j < tempa.Count)
{
result = result * 10 + (int)tempa[j];
++j;
}
return result;
}
}
//if the number is raising, add the digit to ArrayList and
keep finding
else
{
current = temp % 10;
temp /= 10;
}
}
//Can't find the next number return 0
return 0;
}
{
ArrayList tempa = new ArrayList();
int current = x % 10;
int temp = x / 10;
int result;
while (temp > 0)
{
tempa.Add(current);
//find the falling edge from right to left, e.g. "1321" 1->2
->3->(1)falling edge
if (current > temp % 10)
{
//switch the fall edge with first element which greater
than itself
for (int i = 0; i < tempa.Count; i++)
if ((temp % 10) < (int)tempa[i])
{
result = (temp / 10) * 10 + (int)tempa[i];
tempa[i] = temp % 10;
int j = 0;
//put the elements back to the result
while (j < tempa.Count)
{
result = result * 10 + (int)tempa[j];
++j;
}
return result;
}
}
//if the number is raising, add the digit to ArrayList and
keep finding
else
{
current = temp % 10;
temp /= 10;
}
}
//Can't find the next number return 0
return 0;
}
B*5
13 楼
这是careercup的第几题?我怎么没见过。。。
p*n
15 楼
嗯。兄弟你的很正确! scan过的digit不能丢
f*e
16 楼
qqshoot 的代码是对的,而且思路很清晰
【在 q*****t 的大作中提到】
: import java.util.ArrayList;
: import java.util.List;
: public class NextLargerNumberWithSameDigits {
: public static void main(String[] args) throws Exception {
: int a = 1245963;
: System.out.println(findnext(a));
: a = 698754;
: System.out.println(findnext(a));
: }
: public static int findnext(int a) throws Exception {
【在 q*****t 的大作中提到】
: import java.util.ArrayList;
: import java.util.List;
: public class NextLargerNumberWithSameDigits {
: public static void main(String[] args) throws Exception {
: int a = 1245963;
: System.out.println(findnext(a));
: a = 698754;
: System.out.println(findnext(a));
: }
: public static int findnext(int a) throws Exception {
f*e
17 楼
careercup上的
123 的下个数是啥?用相同的数字
123 的下个数是啥?用相同的数字
f*e
19 楼
对头
f*e
20 楼
要求写代码的,这只是一个input
f*e
21 楼
我的思路是进行全排列,然后排序,最笨的方法
S*I
22 楼
next_permutation in C++ std will do it, a sample implementation is shown below:
http://wordaligned.org/articles/next-permutation
【在 f********e 的大作中提到】
: 要求写代码的,这只是一个input
http://wordaligned.org/articles/next-permutation
【在 f********e 的大作中提到】
: 要求写代码的,这只是一个input
f*e
23 楼
谢谢!!
p*n
25 楼
I don't think we need do permutation.
how about this solution?
int FindNext( int x)
{
int base = 1;
int current = x % 10 ;
int temp = x /10;
while (temp)
{
if (temp %10 {
int y = (current - temp%10)*9*base ;
return x+y;
}
else {
base *= 10;
current = temp%10 ;
temp /=10;
}
}
return 0;
}
how about this solution?
int FindNext( int x)
{
int base = 1;
int current = x % 10 ;
int temp = x /10;
while (temp)
{
if (temp %10
int y = (current - temp%10)*9*base ;
return x+y;
}
else {
base *= 10;
current = temp%10 ;
temp /=10;
}
}
return 0;
}
q*t
26 楼
import java.util.ArrayList;
import java.util.List;
public class NextLargerNumberWithSameDigits {
public static void main(String[] args) throws Exception {
int a = 1245963;
System.out.println(findnext(a));
a = 698754;
System.out.println(findnext(a));
}
public static int findnext(int a) throws Exception {
List d = new ArrayList();
while (a != 0) {
d.add(a % 10);
a /= 10;
}
Integer[] digits = d.toArray(new Integer[d.size()]);
// find the first digit that is out of order
// digits[i] < digits[i-1] >= digits[i-2] >= ... >= digits[1] >= digits[0]
int i;
for (i = 1; i < digits.length; i++) {
if (digits[i] < digits[i - 1])
break;
}
if (i >= digits.length) {
throw new Exception("Not found!");
}
// find in digits[i-1], digits[i-2], ..., digits[1], digits[0]
// the smallest digit that is larger than digits[i]
// Because digits[i] is out of order, i.e. digits[i-1]>digits[i],
// it's guaranteed to find one
for (int j = 0; j <= i - 1; j++) {
if (digits[j] > digits[i]) {
int t = digits[j];
digits[j] = digits[i];
digits[i] = t;
break;
}
}
// reverse digits[i-1], digits[i-2], ..., digits[1], digits[0]
for (int j = 0; j < i / 2; j++) {
int t = digits[j];
digits[j] = digits[i - 1 - j];
digits[i - 1 - j] = t;
}
int b = digits[digits.length - 1];
for (int j = digits.length - 2; j >= 0; j--) {
b = b * 10 + digits[j];
}
return b;
}
}
import java.util.List;
public class NextLargerNumberWithSameDigits {
public static void main(String[] args) throws Exception {
int a = 1245963;
System.out.println(findnext(a));
a = 698754;
System.out.println(findnext(a));
}
public static int findnext(int a) throws Exception {
List
while (a != 0) {
d.add(a % 10);
a /= 10;
}
Integer[] digits = d.toArray(new Integer[d.size()]);
// find the first digit that is out of order
// digits[i] < digits[i-1] >= digits[i-2] >= ... >= digits[1] >= digits[0]
int i;
for (i = 1; i < digits.length; i++) {
if (digits[i] < digits[i - 1])
break;
}
if (i >= digits.length) {
throw new Exception("Not found!");
}
// find in digits[i-1], digits[i-2], ..., digits[1], digits[0]
// the smallest digit that is larger than digits[i]
// Because digits[i] is out of order, i.e. digits[i-1]>digits[i],
// it's guaranteed to find one
for (int j = 0; j <= i - 1; j++) {
if (digits[j] > digits[i]) {
int t = digits[j];
digits[j] = digits[i];
digits[i] = t;
break;
}
}
// reverse digits[i-1], digits[i-2], ..., digits[1], digits[0]
for (int j = 0; j < i / 2; j++) {
int t = digits[j];
digits[j] = digits[i - 1 - j];
digits[i - 1 - j] = t;
}
int b = digits[digits.length - 1];
for (int j = digits.length - 2; j >= 0; j--) {
b = b * 10 + digits[j];
}
return b;
}
}
e*s
28 楼
public static int FindNext(int x)
{
ArrayList tempa = new ArrayList();
int current = x % 10;
int temp = x / 10;
int result;
while (temp > 0)
{
tempa.Add(current);
//find the falling edge from right to left, e.g. "1321" 1->2
->3->(1)falling edge
if (current > temp % 10)
{
//switch the fall edge with first element which greater
than itself
for (int i = 0; i < tempa.Count; i++)
if ((temp % 10) < (int)tempa[i])
{
result = (temp / 10) * 10 + (int)tempa[i];
tempa[i] = temp % 10;
int j = 0;
//put the elements back to the result
while (j < tempa.Count)
{
result = result * 10 + (int)tempa[j];
++j;
}
return result;
}
}
//if the number is raising, add the digit to ArrayList and
keep finding
else
{
current = temp % 10;
temp /= 10;
}
}
//Can't find the next number return 0
return 0;
}
{
ArrayList tempa = new ArrayList();
int current = x % 10;
int temp = x / 10;
int result;
while (temp > 0)
{
tempa.Add(current);
//find the falling edge from right to left, e.g. "1321" 1->2
->3->(1)falling edge
if (current > temp % 10)
{
//switch the fall edge with first element which greater
than itself
for (int i = 0; i < tempa.Count; i++)
if ((temp % 10) < (int)tempa[i])
{
result = (temp / 10) * 10 + (int)tempa[i];
tempa[i] = temp % 10;
int j = 0;
//put the elements back to the result
while (j < tempa.Count)
{
result = result * 10 + (int)tempa[j];
++j;
}
return result;
}
}
//if the number is raising, add the digit to ArrayList and
keep finding
else
{
current = temp % 10;
temp /= 10;
}
}
//Can't find the next number return 0
return 0;
}
B*5
29 楼
这是careercup的第几题?我怎么没见过。。。
p*n
31 楼
嗯。兄弟你的很正确! scan过的digit不能丢
f*e
32 楼
qqshoot 的代码是对的,而且思路很清晰
【在 q*****t 的大作中提到】
: import java.util.ArrayList;
: import java.util.List;
: public class NextLargerNumberWithSameDigits {
: public static void main(String[] args) throws Exception {
: int a = 1245963;
: System.out.println(findnext(a));
: a = 698754;
: System.out.println(findnext(a));
: }
: public static int findnext(int a) throws Exception {
【在 q*****t 的大作中提到】
: import java.util.ArrayList;
: import java.util.List;
: public class NextLargerNumberWithSameDigits {
: public static void main(String[] args) throws Exception {
: int a = 1245963;
: System.out.println(findnext(a));
: a = 698754;
: System.out.println(findnext(a));
: }
: public static int findnext(int a) throws Exception {
z*u
33 楼
C version,求指正
int NextNumber(int i)
{
int max_length = 1;
int* digits = (int*) malloc(sizeof(int) * max_length);
/* break number down to digits */
int idx = 0;
while(i)
{
if (idx > max_length - 1)
{
max_length *= 2;
digits = (int*) realloc(digits, sizeof(int) * max_length);
}
digits[idx++] = i % 10;
i /= 10;
}
int length = idx;
/* find the break digit */
for (idx = 1; idx < length; idx++)
{
if (digits[idx] < digits[idx - 1])
{
break;
}
}
int break_idx = idx;
/* i is already maximum */
if (break_idx == length)
{
return -1;
}
/* find the min digit before break digit, which is > break digit */
int min = -1;
int min_idx = 0;
for (idx = 0; idx < break_idx; idx++)
{
if (digits[idx] > digits[break_idx])
{
if ((min == -1) || digits[idx] < min)
{
min = digits[idx];
min_idx = idx;
}
}
}
/* switch min digit and break digit */
int tmp = digits[break_idx];
digits[break_idx] = digits[min_idx];
digits[min_idx] = tmp;
/* sort after break digit */
QuickSort(digits, break_idx);
/* reconstruct result */
int result = 0;
for (idx = length - 1; idx >= 0; idx--)
{
result = result * 10 + digits[idx];
}
free(digits);
return result;
}
int NextNumber(int i)
{
int max_length = 1;
int* digits = (int*) malloc(sizeof(int) * max_length);
/* break number down to digits */
int idx = 0;
while(i)
{
if (idx > max_length - 1)
{
max_length *= 2;
digits = (int*) realloc(digits, sizeof(int) * max_length);
}
digits[idx++] = i % 10;
i /= 10;
}
int length = idx;
/* find the break digit */
for (idx = 1; idx < length; idx++)
{
if (digits[idx] < digits[idx - 1])
{
break;
}
}
int break_idx = idx;
/* i is already maximum */
if (break_idx == length)
{
return -1;
}
/* find the min digit before break digit, which is > break digit */
int min = -1;
int min_idx = 0;
for (idx = 0; idx < break_idx; idx++)
{
if (digits[idx] > digits[break_idx])
{
if ((min == -1) || digits[idx] < min)
{
min = digits[idx];
min_idx = idx;
}
}
}
/* switch min digit and break digit */
int tmp = digits[break_idx];
digits[break_idx] = digits[min_idx];
digits[min_idx] = tmp;
/* sort after break digit */
QuickSort(digits, break_idx);
/* reconstruct result */
int result = 0;
for (idx = length - 1; idx >= 0; idx--)
{
result = result * 10 + digits[idx];
}
free(digits);
return result;
}
z*u
35 楼
相关阅读
请有电面经验的前辈指点。。。On site 可以要求住便宜的旅馆么?下周onsite, 我被阵容给吓了请问有人找过鄢妮律师办h1b的没有?怎么样? (转载)强烈建议大家准备得充分些再出手关于一个visiting professor的offer的纠结,请大家给意见不预约就打来的recuiter电话真讨厌问一下reference的问题沮丧至极的今天下午,忽然收到 Google 的电面邀请,又重新燃起希望。 求鼓励,求祝福!请教h1-h4-h1的问题会计硕士容易申请吗,要考G,T吗?免费拿最高400刀Discover&Amex bonus+Discover 12000mile+10刀礼品卡若干(自取)CV中的publication list写在哪里?请问有自己(不请律师)办h1b的么?朋友的IT公司招developer (chicago)OPT 申请,一个半月了还没有收据OPT case (Vermont) 外加一个小offer大家在公司有没有见过以前当professor,现在在做程序员的例子?求 BlessH1-b十月开始,可以明年1月再报道开始工作吗?