Sort by Last Digit
Return the numbers of nums ordered by their last digit, from 0 to 9. Numbers with the same last digit keep the order they had in nums, so the sort must be stable.
For example, [21, 13, 40, 31] becomes [40, 21, 31, 13]: 21 and 31 both end in 1, and 21 came first.
Example 1
nums [21,13,40,31]
returns [40,21,31,13]
40 ends in 0 and goes first. 21 and 31 end in 1 and keep their order. 13 ends in 3.
Example 2
nums [5,15,25]
returns [5,15,25]
All three end in 5, so nothing moves.
Example 3
nums []
returns []
An empty list stays empty.
Constraints
0 <= len(nums) <= 300 <= nums[i] <= 999
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Sorting and searching problems
- Strictly Increasing CheckBeginner
- Top Three ValuesBeginner
- Where It LandsBeginner
- Widest Gap After SortingBeginner
- Binary SearchEasy
- Can Attend All MeetingsEasy
- Can Make Arithmetic ProgressionEasy
- Count Between in a Sorted ListEasy