491.Print Odd Numbers from 1 to N
Easy
Given a non-negative integer
n, return every odd number from 1 up to and including n, in increasing order.
The check-every-numberCheck Every NumberWalking through every integer from 1 to n and testing i % 2 != 0 before deciding whether to keep it. version mirrors the even-number check exactly, just flipping the condition. The step-by-2Step by 2Starting at 1 and incrementing by 2 each time, so every value visited is already odd. version again skips the wasted half of the work by construction, starting at 1 instead of 2.
Example 1:
Input: n = 10
Output: [1,3,5,7,9]
Example 2:
Input: n = 1
Output: [1]
Example 3:
Input: n = 2
Output: [1]
+ 3 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ n ≤ 1000
n =
10