1. 有一個數(shù)組,按偶奇降序排序后武花,按照偶奇的形式交替輸出圆凰,例如{2, 4, 3, 6, 1, 5, 7, 8, 9},則輸出結(jié)果為{8, 9, 7, 4, 5, 2, 3, 1}
解題思路:
先按照最簡單的辦法:先把數(shù)組分成奇偶兩個數(shù)組体箕,再對這兩個數(shù)組進行降序排序专钉,最后再交替輸出。
代碼:
public class Test1 {
public static void main(String[] args) {
int[] arr = { 2, 4, 6, 1, 5, 7, 8, 9 };
System.out.println(evenOddSort(arr));
}
public static String evenOddSort(int[] nums) {
int length = nums.length;
List<Integer> evenList = new ArrayList<Integer>();
List<Integer> oddList = new ArrayList<Integer>();
for (int i = 0; i < length; i++) {
int num = nums[i];
if (num % 2 == 0) {
evenList.add(num);
} else {
oddList.add(num);
}
}
// 降序排序
sortDesc(evenList);
sortDesc(oddList);
// 公共的次數(shù)
int time = evenList.size() > oddList.size() ? evenList.size() : oddList.size();
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < time; i++) {
list.add(evenList.get(i));
list.add(oddList.get(i));
}
// 剩余的次數(shù)
if (evenList.size() > oddList.size()) {
for (int i = time; i < evenList.size(); i++) {
list.add(evenList.get(i));
}
}else {
for (int i = time; i < oddList.size(); i++) {
list.add(oddList.get(i));
}
}
return list.toString();
}
public static void sortDesc(List<Integer> list) {
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size() - i - 1; j++) {
int current = list.get(j);
int next = list.get(j + 1);
if (current < next) {
list.set(j + 1, current);
list.set(j, next);
}
}
}
System.out.println(list);
}
}
雖然解決了這個問題累铅,但是代碼很復(fù)雜跃须,在室友的啟發(fā)下,有了一種新的思路:先把列表進行降序排序娃兽,然后再用兩個索引(或者叫指針)菇民,分別代表下一個偶數(shù)位置和奇數(shù)位置,使用List<Integer>
來存儲要輸出的數(shù)組,這樣在每次遍歷只需要分別放入一個偶數(shù)和奇數(shù)即可玉雾。
代碼:
public class Test1pro {
public static void main(String[] args) {
int[] arr = { 2, 4, 6, 1, 5, 7, 8, 9 };
System.out.println(evenOddSort(arr));
}
public static String evenOddSort(int[] nums) {
int length = nums.length;
// 降序排序
for (int i = 0; i < length; i++) {
for (int j = 0; j < length - i - 1; j++) {
if (nums[j] < nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(nums));
int even = 0, odd = 0;
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < length; i++) {
// 用兩個索引表示偶奇數(shù)的位置
// 下一個偶數(shù)
while (even < length && nums[even] % 2 != 0) {
even ++;
}
if (even < length) {
list.add(nums[even]);
even ++;
}
// 下一個奇數(shù)
while (odd < length && nums[odd] % 2 == 0) {
odd ++;
}
if (odd < length) {
list.add(nums[odd]);
odd ++;
}
}
return list.toString();
}
}
2. 有一個數(shù)組翔试,求輸出能夠組成的最大數(shù),例如{20, 13, 9, 6}复旬,則輸出結(jié)果為962013
解題思路:
針對給出的示例數(shù)組垦缅,按照正常的思維,那么只需要取出每個首位比較大小即可驹碍。但是如果存在首位相同的情況壁涎,那么就需要繼續(xù)判斷;例如{20, 211}志秃,解決辦法就是設(shè)較小數(shù)的長度為x
怔球,那么只需要取兩個數(shù)的前x
位比較大小即可;但是如果兩個數(shù)的前x
位大小也一樣呢浮还?比如{20, 201}竟坛,那么我們還要把較大數(shù)截掉前x
位后剩下的數(shù)的首位和前x
位的數(shù)的首位進行比較【啵可能聽起來很拗口担汤,我舉個例子:
例如數(shù)組{20, 201},較小數(shù)為20
洼冻,長度為2
崭歧,這兩個數(shù)的前2
位都是20
,則較大數(shù)201
截掉前前2
位后還剩下一個1
撞牢,那么這個1
比2
小率碾,所以組成的數(shù)是20201
,但是如果是{20, 205}屋彪,那么剩下的5
就要比2
大所宰,則組成的數(shù)應(yīng)該是20520
。
代碼:
public class Test2 {
public static void main(String[] args) {
// [13, 6, 9]
// [21, 2, 1]
// [20, 1, 205]
// [9, 7, 979, 997, 799] 9 997 979 799 7
int[] nums = {9, 7, 979, 997, 799};
System.out.println(maxNum(nums));
}
public static String maxNum(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
compare(i, j, nums);
}
}
StringBuffer buffer = new StringBuffer();
for (int num : nums) {
buffer.append(num);
}
return buffer.toString();
}
public static void compare(int i, int j, int[] nums) {
int current = nums[i];
int next = nums[j];
System.out.println(current + " " + next + "數(shù)組:" + Arrays.toString(nums));
int clength = String.valueOf(current).length();
int nlength = String.valueOf(next).length();
if (clength != nlength) {
// 最短長度
int min = clength < nlength ? clength : nlength;
// 把兩個數(shù)轉(zhuǎn)成字符串
String cstr = String.valueOf(current);
String nstr = String.valueOf(next);
// 按照最短長度進行裁剪
// 并轉(zhuǎn)化為整型進行大小比較
int minCurrent = Integer.parseInt(cstr.substring(0, min));
int minNext = Integer.parseInt(nstr.substring(0, min));
if (minCurrent == minNext) {
// 代表前面公共部分是相同的
// 但是并不代表數(shù)長的肯定比數(shù)短的小
// 還需要進一步判斷
// 比如【21, 2】和【20畜挥, 205】
// 取出相同部分的首位
int first = Integer.parseInt(cstr.substring(0, 1));
if (clength > nlength) {
// 剩余的部分的首位
int last = Integer.parseInt(cstr.substring(min, min + 1));
// 如果剩余的部分的首位比相同部分的首位小
// 則需要對調(diào)兩個數(shù)
if (last < first) {
nums[i] = next;
nums[j] = current;
}
} else {
// 多余的部分
int last = Integer.parseInt(nstr.substring(min, min + 1));
// 如果剩余的部分的首位比相同部分的首位大
// 則需要對調(diào)兩個數(shù)
if (last > first) {
nums[i] = next;
nums[j] = current;
}
}
} else {
// 雖然長度不一樣
// 但是只需要比較相同的長度的數(shù)的大小即可
if (minCurrent < minNext) {
nums[i] = next;
nums[j] = current;
}
}
} else {
// 代表兩個數(shù)長度一樣
// 那么只需要比較兩個數(shù)的大小即可
if (current < next) {
nums[i] = next;
nums[j] = current;
}
}
}
}
后續(xù)可能還會更新~