Algorithms—179.Largest Number

原创 2015年07月10日 18:29:57

思路:比较粗暴,写一个比较方法,然后冒泡排序,最后合并。


public class Solution {
	public String largestNumber(int[] nums) {
		if (nums.length == 1) {
			return String.valueOf(nums[0]);
		}
		int temp = 0;
		for (int i = nums.length - 1; i > 0; i--) {
			for (int j = 0; j < i; ++j) {
				if (new Solution().compare(nums[j], nums[j + 1])) {
					temp = nums[j];
					nums[j] = nums[j + 1];
					nums[j + 1] = temp;
				}
			}
		}
		StringBuffer sb = new StringBuffer();
		if (nums[nums.length - 1] == 0) {
			return "0";
		}
		for (int i = nums.length - 1; i >= 0; i--) {
			sb.append(String.valueOf(nums[i]));
		}
		return sb.toString();
	}

	public boolean compare(int a, int b) {
		char[] ca=(String.valueOf(a)+String.valueOf(b)).toCharArray();
		char[] cb=(String.valueOf(b)+String.valueOf(a)).toCharArray();
		for (int i = 0; i < ca.length; i++) {
			if (ca[i]>cb[i]) {
				return true;
			}
			if (ca[i]<cb[i]) {
				return false;
			}
		}
		return false;
	}
}


耗时:392ms,发现同一个算法测试情况可能会相差100ms多。。。。真是瞎了狗眼。


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode]179.Largest Number

【题目】 Given a list of non negative integers, arrange them such that they form the largest number...

179. Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For exa...

LeetCode题解(Week2):179. Largest Number

原题目 Given a list of non negative integers, arrange them such that they form the largest number. Fo...

LeetCode(179) Largest Number

题目如下: Given a list of non negative integers, arrange them such that they form the largest number. F...

leetcode_[python/C++] 179. Largest Number(数字组合成最大数)

题目链接 【题目】 Given a list of non negative integers, arrange them such that they form the largest numb...

Leetcode 179. Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.For examp...

179.Largest Number

近期开始在LeetCode上开始刷题,第一道题是Largest Number,题目内容如下: Given a list of non negative integers, arrange them...

Leetcode 179 Largest Number 构造最大数字

给出一组非负数字,对它们进行整理排列使它们构成最大的数字。例如,给出[3, 30, 34, 5, 9],可以构成的最大的数字是9534330。

<LeetCode OJ> 179. Largest Number

179. Largest Number My Submissions Question Total Accepted: 37152 Total Submissions: 208760 Diff...

leetcode179. Largest Number

179. Largest NumberGiven a list of non negative integers, arrange them such that they form the large...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)