编程题之数串

题目描述 设有n个正整数,将他们连接成一排,组成一个最大的多位整数。 如:n=3时,3个整数13,312,343,连成的最大整数为34331213。 如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。 输入描述: 有多组测试样例,每组测试样例包含两行,第一行为一个整数N(N<=100),第二行包含N个数(每个数不超过1000,空格分开)。 输出描述: 每组数据输出一个表示最大的整数。

1
2
3
4
5
6
7
8
9
10
11
示例1
输入

2
12 123
4
7 13 4 246
输出

12312
7424613
Read more

摆动排序-归并排序-归并排序

排序

- 摆动排序

给你一个没有排序的数组,请将原数组就地重新排列满足如下性质:

nums[0] <= nums[1] >= nums[2] <= nums[3]....

答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Solution {
/**
* @param nums a list of integer
* @return void
*/
public void wiggleSort(int[] nums) {
for(int i=1; i<nums.length; i++){
if ((i%2==1 && nums[i-1]>nums[i])||
(i%2==0 && nums[i-1]<nums[i]))
swap(nums, i-1, i);
}

}

/**
* Swap two values
* */
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

}
Read more
Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×