2553. Separate the Digits in an Array
MediumView on LeetCode
Time: O(n * digits)
Space: O(n * digits)
Problem Overview
Separate digits of each number and insert in order.
Intuition
Separate digits of each number and insert in order. For each number, expand its digits.
Algorithm
- 1For each num: extract digits (from most significant). Insert all digits in order.
Common Pitfalls
- •Maintain relative order of original elements, and digit order within each element.
2553.cs
C#
// Approach: For each number extract digits in order and append to result list.
// Time: O(n * digits) Space: O(n * digits)
public class Solution
{
public int[] SeparateDigits(int[] nums)
{
List<int> ans = new List<int>();
foreach (var num in nums)
{
foreach (var c in num.ToString())
ans.Add(c - '0');
}
return ans.ToArray();
}
}Was this solution helpful?
Related Problems
- 498. Diagonal Traverse(Medium)
- 799. Champagne Tower(Medium)
- 832. Flipping an Image(Easy)
- 838. Push Dominoes(Medium)
- 874. Walking Robot Simulation(Medium)
- 885. Spiral Matrix III(Medium)