# 179. Largest Number

### Leetcode

<https://leetcode.com/problems/largest-number/>

### 題目

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

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

### 解答 <a href="#ti-jie" id="ti-jie"></a>

* **方法 1**

利用 sort&#x20;

```
var largestNumber = function(nums) {
  nums.sort((a, b) => {
    a = a.toString();
    b = b.toString();

    if(a+b > b+a) return -1;
    else return 1;
  })

  let str = nums.join('').toString();
  if(str[0] === '0') return '0';
  return str;
};
```

Runtime: 84 ms, faster than 53.50% of JavaScript online submissions for Largest Number.

Memory Usage: 40.6 MB, less than 44.90% of JavaScript online submissions for Largest Number.

### 測資

```
let nums = [3,30,34,5,9];
nums = [0, 0];
nums = [34323,3432];
nums = [999999991,9];

console.log(largestNumber(nums))
```
