Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

面试题45. 把数组排成最小的数 #30

Open
fireairforce opened this issue Feb 29, 2020 · 0 comments
Open

面试题45. 把数组排成最小的数 #30

fireairforce opened this issue Feb 29, 2020 · 0 comments
Labels
medium New feature or request 剑指offer This doesn't seem right

Comments

@fireairforce
Copy link
Owner

这题贪心就可以了,把小的放前面,写个cmp函数就可以了:

/**
 * @param {number[]} nums
 * @return {string}
 */
var minNumber = function(nums) {
  let res = []
  for (let item of nums) {
    res.push(item + '')
  }
  res.sort((a, b) => {
    return parseInt(a + b) - parseInt(b + a)
  })
  return res.join('')
}

// compareString = (a, b) => {}
// console.log(minNumber([10, 2]))
// console.log(minNumber([3, 30, 34, 5, 9]))
@fireairforce fireairforce added medium New feature or request 剑指offer This doesn't seem right labels Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
medium New feature or request 剑指offer This doesn't seem right
Projects
None yet
Development

No branches or pull requests

1 participant