489.Print Numbers from N to 1
Easy
Given a non-negative integer
n, return the numbers from n down to 1, in decreasing order.
The decrementing loopDecrementing LoopWalking an index from n down to 1, appending each value as it goes — the mirror image of counting up. version is the direct analogue of counting up, just running backward. The range-then-reverseRange, Then ReverseGenerating the easy ascending sequence first, then reversing it — rather than hand-writing a descending loop. version builds the sequence everyone's more used to generating (ascending), then flips it — still O(n) overall, just composed from two simpler, well-known operations instead of one custom loop.
Example 1:
Input: n = 5
Output: [5,4,3,2,1]
Example 2:
Input: n = 1
Output: [1]
Example 3:
Input: n = 0
Output: []
+ 2 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ n ≤ 1000
n =
5