leetcode-javascript

1. Largest Number

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

  

 1 /**
 2  * @param {number[]} nums
 3  * @return {string}
 4  */
 5 var largestNumber = function(nums) {
 6     var str = nums.join().replace(/\,/g, ‘‘);
 7     var arr = [];
 8     for(var i = 0; i < str.length; i++) {
 9         arr.push(str.charAt(i));
10     }
11     nums = arr.sort(function(a, b) {
12         return b -a;
13     })
14     return nums.join().replace(/\,/g, ‘‘);
15 };
时间: 2024-08-06 07:56:40

leetcode-javascript的相关文章

[LeetCode][JavaScript]Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

[LeetCode][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start t

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod

[LeetCode][JavaScript]Coin Change

Coin Change You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination o

[LeetCode][JavaScript]Remove Duplicate Letters

Remove Duplicate Letters Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results

[LeetCode][JavaScript]Additive Number

Additive Number Additive number is a positive integer whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum o

[LeetCode][JavaScript]Unique Binary Search Trees II

Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1

[LeetCode][JavaScript]Binary Tree Preorder Traversal

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? https://leetcod

[LeetCode][JavaScript]Trapping Rain Water

Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map

[LeetCode][JavaScript]Reverse Integer

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread