LeetCode 179. Largest Number

欢迎移步到我的个人博客

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

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

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

题意解析

给出一个非负整型列表,对列表里的整型进行排序,使得排序后将这些数字连在一起的数字最大。

例如,非负整型列表为[3, 30, 34, 5, 9],排序后最大的值为9534330

注意:结果可能是一个非常大的值,所以用string代替integer

解法分析

这道题的排序方式为假设有两个数AB,如果AB>BA则排序为AB,否则排序为BA
java中有一个接口为Comparable,如下。

public interface Comparable<T> {
    /**
     * Compares this object with the specified object for order.  Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
     * ...
     */
    public int compareTo(T o);

新建一个类只要实现了这个接口中的compareTo函数,就可以使用Arrays.sort进行排序。

解题代码

    public String largestNumber(int[] nums) {
        List<IntegerShadow> integerShadows = new ArrayList<IntegerShadow>();
        for (int num : nums) {
            if (num == 0) {
                integerShadows.add(new IntegerShadow(0, "0"));
                continue;
            }
            integerShadows.add(new IntegerShadow(num, String.valueOf(num)));
        }
        IntegerShadow[] integerShadows2 = new IntegerShadow[integerShadows
                .size()];
        integerShadows.toArray(integerShadows2);
        Arrays.sort(integerShadows2);
        StringBuilder sb = new StringBuilder();
        for (int i = integerShadows2.length - 1; i >= 0; i--) {
            sb.append(integerShadows2[i].ori);
        }
        if (sb.toString().matches("0+")) {
            return "0";
        } else {
            return sb.toString();
        }
    }

    class IntegerShadow implements Comparable<IntegerShadow> {
        public int ori;
        public String oriStr;

        public IntegerShadow(int o, String s) {
            this.ori = o;
            this.oriStr = s;
        }

        @Override
        public int compareTo(IntegerShadow o) {
            return (this.oriStr + o.oriStr).compareTo(o.oriStr + this.oriStr);
        }
    }
阅读更多
换一批

Largest prime number ever is found

12-03

http://www.newscientist.com/news/news.jsp?id=ns99994438rnrnLargest prime number ever is found rn rn rn15:11 02 December 03 rn rnNewScientist.com news service rn rnA 26-year-old graduate student in the US has made mathematical history by discovering the largest known prime number.rnrnThe new number is 6,320,430 digits long. It took just over two years to find using a distributed network of more than 200,000 computers.rnrnMichael Shafer a chemical engineering student at Michigan State University used his office computer to contribute spare processing power to the Great Internet Mersenne Prime Search (GIMPS). The project has more than 60,000 volunteers from all over the world taking part.rnrn"I had just finished a meeting with my advisor when I saw the computer had found the new prime," Shafer says. "After a short victory dance, I called up my wife and friends involved with GIMPS to share the great news." rnrnPrime numbers are positive integers that can only be divided by themselves and one. Mersenne primes are an especially rare type of prime that take the form 2 p-1, where p is also a prime number. The new number can be represented as 220,996,011-1. It is only the 40th Mersenne prime to have ever been found.rnrnrnBuilding blocks rnrnrnMersenne primes were first discussed by Euclid in 350 BC and have been central to the branch of mathematics known as number theory ever since. They are named after a 17th century French monk who first came up with an important conjecture about which values of p would yield a prime.rnrnPrimes are the building blocks of all positive numbers. They have practical uses too, for example by providing a way of exchanging the cryptographic keys that keep internet communications secure from eavesdropping. However, despite their significance, mathematicians do not understand the way prime numbers are distributed making it very difficult to identify new primes.rnrnMarcus du Sautoy, a mathematician at Oxford University and author of The Music of the Primes, says the discovery is unlikely to add much to our understanding of the way primes are distributed but is still significant. rnrn"It's a really good measure of what our computational capabilities are," he told New Scientist. "It's a really fun project. Everyone gets a different bit of the number universe to look at. It's a bit like the lottery."rnrn The World's No.1 Science & Technology News Service rn rn rn rnLargest prime number ever is found rn rn rn15:11 02 December 03 rn rnNewScientist.com news service rn rnA 26-year-old graduate student in the US has made mathematical history by discovering the largest known prime number.rnrnThe new number is 6,320,430 digits long. It took just over two years to find using a distributed network of more than 200,000 computers.rnrnMichael Shafer a chemical engineering student at Michigan State University used his office computer to contribute spare processing power to the Great Internet Mersenne Prime Search (GIMPS). The project has more than 60,000 volunteers from all over the world taking part.rnrn"I had just finished a meeting with my advisor when I saw the computer had found the new prime," Shafer says. "After a short victory dance, I called up my wife and friends involved with GIMPS to share the great news." rnrnPrime numbers are positive integers that can only be divided by themselves and one. Mersenne primes are an especially rare type of prime that take the form 2 p-1, where p is also a prime number. The new number can be represented as 220,996,011-1. It is only the 40th Mersenne prime to have ever been found.rnrnrnBuilding blocks rnrnrnMersenne primes were first discussed by Euclid in 350 BC and have been central to the branch of mathematics known as number theory ever since. They are named after a 17th century French monk who first came up with an important conjecture about which values of p would yield a prime.rnrnPrimes are the building blocks of all positive numbers. They have practical uses too, for example by providing a way of exchanging the cryptographic keys that keep internet communications secure from eavesdropping. However, despite their significance, mathematicians do not understand the way prime numbers are distributed making it very difficult to identify new primes.rnrnMarcus du Sautoy, a mathematician at Oxford University and author of The Music of the Primes, says the discovery is unlikely to add much to our understanding of the way primes are distributed but is still significant. rnrn"It's a really good measure of what our computational capabilities are," he told New Scientist. "It's a really fun project. Everyone gets a different bit of the number universe to look at. It's a bit like the lottery."rnrnMathematical curiosity rn rnThe GIMPS project uses a central computer server and free software to coordinate the activity of all its contributors. Contributing machines are each allocated different prime number candidates to test.rnrnSome people contribute to GIMPS out of mathematical curiosity or to test their computer hardware, while others just hope to go down in history as the discoverer of a massive prime. There is also a financial incentive with the Electronic Frontier Foundation, a non-profit US group, offering a $100,000 prize for the discovery of the first prime with 10 million digits.rnrnShafer's discovery was made on 17 November but it was not independently verified until now. "It's humbling to see so many people of varied lands, ages and vocations volunteering for this fun and amazing project," says Scott Kurowski, whose company Entropia manages the GIMPS server. rnrn"There are more primes out there," adds George Woltman, who started the GIMPS project in 1996. "And anyone with an internet-connected computer can participate."rn rn rnWill Knightrnrn

没有更多推荐了,返回首页