Created
May 11, 2026 15:42
-
-
Save tatsuyax25/8eab5bd6fdbb8ac14903d9ff52d9859c to your computer and use it in GitHub Desktop.
Given an array of positive integers nums, return an array answer that consists of the digits of each integer in nums after separating them in the same order they appear in nums. To separate the digits of an integer is to get all the digits it has in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number[]} | |
| */ | |
| var separateDigits = function(nums) { | |
| const res = []; | |
| for (let n of nums) { | |
| // If the number is a single digit, just append it | |
| if (n < 10) { | |
| res.push(n); | |
| continue; | |
| } | |
| const stack = []; | |
| // Extract digits from right to left using modulo/division | |
| while (n > 0) { | |
| stack.push(n % 10); | |
| n = Math.floor(n / 10); | |
| } | |
| // Digits were collected in reverse order, so pop to restore left->right | |
| while (stack.length) { | |
| res.push(stack.pop()); | |
| } | |
| } | |
| return res; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment