preface

Nuggets team number online, help you Offer impromptu! Click for details

Topic describes

Their thinking

  • This is a sorting problem
  • We just need to sort the input integer array in ascending order
  • Then return the first k numbers, using JS’s own sort method
  • Slice method was used to intercept the first K digits

The problem solving code

var getLeastNumbers = function(arr, k) {
    arr.sort((num1,num2) = > num1 - num2);
    return arr.slice(0,k)
};
Copy the code

Conclusion (this topic gives us the enlightenment of thinking)

  • Lesson 1: Learn to intercept using slice
  • Lesson 2: Learn to sort arrays using sort