[30,1]
"301"
[2,20,23,4,8]
"8423220"
[2]
"2"
[10]
"10"
输出结果可能非常大,所以你需要返回一个字符串而不是整数。
# # 最大数 # @param nums int整型一维数组 # @return string字符串 # class Solution: def solve(self , nums ): # write code here temp=-1 floor=len(nums)-1 st=list(map(str,nums)) while floor>0: for i in range(floor): if st[i][0]<st[i+1][0]: temp=st[i+1] st[i+1]=st[i] st[i]=temp elif st[i][0]==st[i+1][0] and len(st[i])>len(st[i+1]): temp=st[i+1] st[i+1]=st[i] st[i]=temp elif st[i][0]==st[i+1][0] and len(st[i])==len(st[i+1]) and int(st[i])<int(st[i+1]): temp=st[i+1] st[i+1]=st[i] st[i]=temp floor-=1 set_st=set(st)#全部是0,则返回0 ‘[0,0,0]’--->0 if len(set_st)==1 and '0' in set_st: return '0' return ''.join(st)
class Solution: def solve(self , nums ): # write code here self.res = '' n = len(nums) def dfs(index, path): if index == n: tmp = "".join(path) if tmp > self.res: self.res = tmp return for i in range(index, n): nums[index], nums[i] = nums[i], nums[index] path.append(str(nums[index])) dfs(index+1, path) path.pop() nums[index], nums[i] = nums[i], nums[index] dfs(0, []) if int(self.res) == 0: return '0' return self.res
class Solution
{
public:
/**
* 最大数
* @param nums int整型vector
* @return string字符串
*/
string solve(vector<int> &nums)
{
// write code here
vector<string> a;
for (auto &s : nums)
a.push_back(to_string(s));
sort(a.begin(), a.end(), cmp);
string s;
for (auto &ch : a)
s = s + ch;
if (s[0] == '0')
s = "0";
return s;
}
static bool cmp(string a, string b)
{
return a + b > b + a;
}
}; 类似于冒泡,从数组中拿出a和b,转化成String,组合成ab,ba然后再转化为整数类型, 比较大小。一只冒泡到最后,把能组成最大值的放在前面,最后全部合起来转化为一个string返回即可。
// 函数入口
public String maxStr(int[] arr){
// 边界值
if (arr == null || arr.length == 0) return "";
// 开始冒泡
for (int i = 0; i < arr.length - 1; ++i){
for (int j = 0; j < arr.length - i - 1; ++j){
// 把int转为String
String a = String.valueOf(arr[j]); // 也可以直接 arr[j] + "" ,我这里不知道两个的速度比较
String b = String.valueOf(arr[j + 1]); // 就随便用了
String sum1 = a + b,sum2 = b + a;
if (!compare(sum1,sum2)) {
// 交换
swap(arr,j,j + 1);
}
}
}
// 换完之后,结果是能组成最大的数都在前面,所以转化为一个String返回即可
String res = "";
for (int i = 0; i < arr.length ;++i){
res += (arr[i] + "");
}
return res;
}
// 比较ab,ba大小 ,sum1是 ab ,sum2 是ba
public boolean compare(String sum1, String sum2){
int i = Integer.parseInt(sum1),j = Integer.parseInt(sum2);
return i >= j? true : false;
}
// 交换
public void swap(int[] arr,int i ,int j ){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
class Solution {
public:
/**
* 最大数
* @param nums int整型vector
* @return string字符串
*/
static bool cmp(string &a, string& b){
return a+b>b+a;
}
string solve(vector<int>& nums) {
// write code here
vector<string>vec;
//将整型的数字转化为字符串
for(int i=0;i<nums.size();i++){
vec.push_back(to_string(nums[i]));
}
//排序
sort(vec.begin(),vec.end(),cmp);
if(vec[0]=="0") return "0";
string res="";
for(int i=0;i<vec.size();i++){
res+=vec[i];
}
return res;
}
}; 比较它的两个参数的顺序。当第一个参数小于、等于或大于第二个参数时,返回一个负整数、零或正整数。
在前面的描述中,符号sgn(表达式)表示数学符号函数,它定义根据表达式的值是负、零还是正返回-1、0或1中的一个。
实现者必须确保所有x和y的sgn(compare(x, y)) == -sgn(compare(y, x))。(这意味着当且仅当compare(y, x)抛出异常时,compare(x, y)必须抛出异常。)
实现者还必须确保关系是可传递的:((compare(x, y)>0) && (compare(y, z)>0))意味着比较(x, z)>0。
最后,实现者必须确保对于所有的z, compare(x, y)==0意味着sgn(compare(x, z))==sgn(compare(y, z))。
通常情况下,但并不严格要求(compare(x, y)==0) == (x = (y))。一般来说,任何违反这一条件的比较国都应明确指出这一事实。推荐的语言是“注意:这个比较器强加的顺序与等号不一致。”
参数:
O1 -第一个被比较的对象。
O2——第二个要比较的物体。
返回:
作为第一个参数的负整数、零或正整数小于、等于或大于第二个参数。
public class Solution {
/**
* 最大数
* @param nums int整型一维数组
* @return string字符串
*/
public String solve (int[] nums) {
// write code here
String[] ss = new String[nums.length];
StringBuilder sb = new StringBuilder();
for(int i = 0; i < nums.length; i++) {
ss[i] = String.valueOf(nums[i]);
}
Arrays.sort(ss, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Integer.valueOf(o2 + o1) - Integer.valueOf(o1 + o2);
}
});
if(ss[0].equals("0")) return "0";
for(int i = 0; i < ss.length; i++) {
sb.append(ss[i]);
}
return sb.toString();
}
} class Solution {
public:
/**
* 最大数
* @param nums int整型vector
* @return string字符串
*/
static bool cmp(string a, string b){
if((a+b) > (b+a)){
return true;
}
else{
return false;
}
}
string solve(vector<int>& nums) {
// write code here
if(nums.size() == 0) return "0";
vector<string> s;
for(int i = 0; i < nums.size(); ++i){
s.push_back(to_string(nums[i]));
}
sort(s.begin(), s.end(), cmp);
if(s[0] == "0") return "0";
string res = "";
for(int i = 0; i < s.size(); ++i){
res += s[i];
}
return res;
}
};
import java.util.*; public class Solution { /** * 最大数 * @param nums int整型一维数组 * @return string字符串 */ public String solve (int[] nums) { String[] strArr = new String[nums.length]; for (int i = 0 ; i < nums.length ;i++){ strArr[i]= String.valueOf(nums[i]); } Arrays.sort(strArr, (o1, o2) -> Integer.parseInt(o2+o1) - Integer.parseInt(o1+o2)); StringBuilder maxString = new StringBuilder(); if(strArr[0].equals( "0")){ return "0"; } for (int i = 0 ; i < strArr.length;i++){ maxString.append(strArr[i]); } return maxString.toString(); // write code here } }
import java.util.*;
public class Solution {
/**
* 最大数
* @param nums int整型一维数组
* @return string字符串
*/
public String solve (int[] nums) {
// write code here
//冒泡排序,降序排列,小的数往后冒
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length - i - 1; j++){
//数值排序时,考虑十位数与个位数的拼接
//保存拼接的字符串,拼接字符串
String a = String.valueOf(nums[j]);
String b = String.valueOf(nums[j+1]);
String sum1 = a+b,sum2 = b+a;
//转换成基本数据类型才能比较
int temp1 = Integer.parseInt(sum1),temp2=Integer.parseInt(sum2);
//若temp1>temp2,则说明a(nums[j])在拼接时放在前面,nums[j]与nums[j+1]不用交换
//若temp1<temp2,则说明b(nums[j+1])在拼接时放在前面,发生交换
if(temp1<temp2){
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
String str = new String();
for(int i = 0; i < nums.length; i++){
str += nums[i];
}
if(str.length()!=0 && str.charAt(0) == '0'){
return "0";
}else{
return str;
}
}
} from functools import cmp_to_key
class Solution:
def solve(self , nums: List[int]) -> str:
def compare(x,y):
if x+y > y+x:
return -1
elif x+y < y+x:
return 1
return 0
nums_str = list(map(str,nums))
nums_str.sort(key=cmp_to_key(compare))
res = ''.join(nums_str).lstrip('0')
return res if res else '0' public String solve (int[] nums) {
// write code here
List<String> transList = new ArrayList<>();
for (int n : nums)
transList.add(String.valueOf(n));
transList.sort((o1, o2) -> -(o1 + o2).compareTo(o2 + o1));
String res = String.join("", transList);
return res.startsWith("0") ? "0" : res;
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 最大数
* @param nums int整型一维数组
* @return string字符串
*/
public String solve (int[] nums) {
// write code here
StringBuilder result = new StringBuilder();
// 冒泡排序,暴力解法
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length ; j++) {
String s3 = nums[i] + "" + nums[j] + "";
String s4 = nums[j] + "" + nums[i] + "";
// 比较拼接后的大小
if (Integer.parseInt(s3) < Integer.parseInt(s4)) {
int e = nums[i];
nums[i] = nums[j];
nums[j] = e;
}
}
}
if(nums[0] == 0){
return "0";
}
for (int i = 0; i < nums.length; i++) {
result.append(nums[i] + "");
}
return result.toString();
}
} public String solve (int[] nums) {
// write code here
StringBuilder res=new StringBuilder();
PriorityQueue<Integer> queue=new PriorityQueue<Integer>(new Comparator<Integer>(){
@Override
public int compare(Integer num1 ,Integer num2){
return (num2+""+num1).compareTo(num1+""+num2);
}
});
for(int num:nums){
queue.add(num);
}
while(!queue.isEmpty()){
res.append(queue.poll());
}
return res.charAt(0)=='0'?"0":res.toString();
}