avatar
S*I
2
132?

【在 f********e 的大作中提到】
: careercup上的
: 123 的下个数是啥?用相同的数字

avatar
f*e
3
对头
avatar
f*e
4
要求写代码的,这只是一个input
avatar
f*e
5
我的思路是进行全排列,然后排序,最笨的方法
avatar
f*e
7
谢谢!!
avatar
r*y
8
like the idea to find the permutation ?

【在 f********e 的大作中提到】
: 要求写代码的,这只是一个input
avatar
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;
}
avatar
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;
}
}
avatar
e*s
11
大哥您CODE不对啊, 1321 return 3121, should be 2113啊

【在 p*******n 的大作中提到】
: 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
avatar
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;
}
avatar
B*5
13
这是careercup的第几题?我怎么没见过。。。
avatar
p*n
14
大哥我很羞惭啊,竟然有BUG。谢谢兄弟提醒啊,我研究一下你的啊。

>2

【在 e***s 的大作中提到】
: 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

avatar
p*n
15
嗯。兄弟你的很正确! scan过的digit不能丢
avatar
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 {

avatar
f*e
17
careercup上的
123 的下个数是啥?用相同的数字
avatar
S*I
18
132?

【在 f********e 的大作中提到】
: careercup上的
: 123 的下个数是啥?用相同的数字

avatar
f*e
19
对头
avatar
f*e
20
要求写代码的,这只是一个input
avatar
f*e
21
我的思路是进行全排列,然后排序,最笨的方法
avatar
f*e
23
谢谢!!
avatar
r*y
24
like the idea to find the permutation ?

【在 f********e 的大作中提到】
: 要求写代码的,这只是一个input
avatar
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;
}
avatar
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;
}
}
avatar
e*s
27
大哥您CODE不对啊, 1321 return 3121, should be 2113啊

【在 p*******n 的大作中提到】
: 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
avatar
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;
}
avatar
B*5
29
这是careercup的第几题?我怎么没见过。。。
avatar
p*n
30
大哥我很羞惭啊,竟然有BUG。谢谢兄弟提醒啊,我研究一下你的啊。

>2

【在 e***s 的大作中提到】
: 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

avatar
p*n
31
嗯。兄弟你的很正确! scan过的digit不能丢
avatar
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 {

avatar
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;
}
avatar
q*x
34
白板都写不下吧。

【在 f********e 的大作中提到】
: qqshoot 的代码是对的,而且思路很清晰
avatar
z*u
35
又看了一下 qqshot 的解法,发现我的后边的排序是不需要的,因为那个 digit array
肯定是升续的,reverse 变成降续就好了

【在 z****u 的大作中提到】
: 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)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。