码迷,mamicode.com
首页 > 其他好文 > 详细

今日头条,找出下一个比它大的整数

时间:2018-09-14 21:38:44      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:r++   value   asc   continue   oid   tostring   app   system   下标   

package Integer;

import java.util.Arrays;

public class GetNextAsc {

public static int getNextAsc(int obj) {
String temp = obj + "";
char[] chars = temp.toCharArray();
int len = chars.length;
for (int i = 0; (len - i - 2 >= 0) && i < len; i++) {
int maxIndex = -1;
char maxValue = ‘9‘ + 1;
//获取最小的大数值下标
for (int r = len - i - 1; r <= len - 1; r++) {
if (chars[r] > chars[len - i - 2]) {
if (chars[r] < maxValue) {
maxIndex = r;
maxValue = chars[r];
}
}
}

if (maxIndex == -1) {
continue;
} else {
//交换
char c = chars[len - i - 2];
chars[len - i - 2] = chars[maxIndex];
chars[maxIndex] = c;
//排序,从小到大
char[] subChar = Arrays.copyOfRange(chars, len - i - 1, len);
Arrays.sort(subChar);
for (int j = 0; j < i + 1; j++) {
int k = len - i - 1 + j;
chars[k] = subChar[j];
}
StringBuilder sb = new StringBuilder();
for (int p = 0; p < chars.length; p++) {
sb.append(chars[p]);
}

return Integer.valueOf(sb.toString());

}

}

return -1;

}

public static void main(String[] args) {
System.out.println(getNextAsc(1111111));
}

}

今日头条,找出下一个比它大的整数

标签:r++   value   asc   continue   oid   tostring   app   system   下标   

原文地址:https://www.cnblogs.com/mlz-2019/p/9648782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!