输入一个正整数数组,输出由数组里面所有数字组成的最大正整数; 如 {4, 2, 3} 得 432 。
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param arrs int整型一维数组 正整数数组 * @return int整型 */ public int maxIntValue (int[] arrs) { // write code here Arrays.sort(arrs); int res = 0; for(int i = arrs.length - 1; i >= 0; i--) res = res * 10 + arrs[i]; return res; } }scala版
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param arrs int整型一维数组 正整数数组 * @return int整型 */ def maxIntValue(arrs: Array[Int]): Int = { // write code here arrs.sorted(Ordering[Int].reverse).mkString.toInt } }python版
class Solution: def maxIntValue(self , arrs ): # write code here return int(''.join(list(map(str, sorted(arrs, reverse=True)))))笔试的时候为了节约时间,还是怎么简单怎么来
"111" "35"
111>35 : 35111>11135
"35" > "111" : 35111>11135
classSolution {
public:
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param arrs int整型vector 正整数数组 * @return int整型 */ intmaxIntValue(vector& arrs) { // write code here vector s ; intn = arrs.size(); if(!n) return0; for( inti = 0 ; i< n ; i++) s.push_back( to_string(arrs[i])); sort( s.begin() , s.end()); intres =0; for( intj = s.size()-1; j >=0 ; j--) { for( inti = 0 ; i < s[j].size();i++) res = res * 10 + s[j][i]-'0'; } returnres; }
};