牛客网原题地址:NC111 最大数
import java.util.*;
public class Solution {
/**
* 最大数
* @param nums int整型一维数组
* @return string字符串
*/
public String solve (int[] nums) {
// write code here
// 求数组中所有数的和,用于判断特殊情况,即数组中全是0的情况
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (sum > 0) {
break;
}
}
// 如果数组中全是0,那么返回字符串0
if (sum == 0) {
return "0";
}
String result = "";
specialSort(nums);
// 拼接字符串来返回结果
for (int i = 0; i < nums.length; i++) {
result += nums[i];
}
return result;
}
public void specialSort(