179. Largest Number

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BigFatSheep/article/details/79973272

####问题描述
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.

题目链接:

####思路分析
给一数组,将它合成一个最大的数,以string形式返回。

关键在于要将首位最大的数放在前面,这就需要自己设计一个sort的comparator,sort的条件是对比两个string相加的值,将大的放在前面,这就是一个冒泡排序,只不过STL给我们简化了。

然后将结果res string的前面不合理的0都去掉就可以了。

We want to compare the digit of each numbers and sort with bigger digit in the left to front. So we could convert each number to string first, then compare each number’s string value and sort bigger string to front. After sort, use a string builder to contain the final result.

remember to solve a corner case like all zero value in nums array.
####代码

class Solution {
    public String largestNumber(int[] nums) {
        if (nums.length == 0){
            return "0";
        }
        String[] strs = new String[nums.length];
        for (int i = 0; i < nums.length; i++){
            strs[i] = String.valueOf(nums[i]);
        }
        Comparator<String> cmp = new Comparator<String>(){
            @Override
            public int compare(String str1, String str2){
                String s1 = str1 + str2;
                String s2 = str2 + str1;
                return s2.compareTo(s1);
            }
        };
        Arrays.sort(strs, cmp);
        //corner case if nums only has a bunch of zero.
        if (strs[0].charAt(0) == '0'){
            return "0";
        }
        StringBuilder sb = new StringBuilder();
        for (String str : strs){
            sb.append(str);
        }
        return sb.toString();
    }
}
class Solution {
public:
    string largestNumber(vector<int>& nums) {
        vector<string> str;
        for (auto i : nums)
            str.push_back(to_string(i));
        sort(str.begin(), str.end(), [](string& s1, string& s2){ return s1 + s2 > s2 + s1;});
        string res = "";
        for (auto s : str)
            res += s;
        while (res[0] == '0' && res.length() > 1)
            res.erase(0, 1);
        return res;
    }
};

时间复杂度:O(n)O(n)
空间复杂度:O(n)O(n)


####反思
关键在于sort的comparator的设计,在函数内部inline重载。

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

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试