数组 
      
    
      
      
      
        模拟 
      
    
   
  
    
      
       
     
  
  
    
      
    
    
      
       
     
  
    
题目描述 
给你一个正整数数组 nums ,请你返回一个数组  answer ,你需要将 nums 中每个整数进行数位分割后,按照 nums 中出现的 相同顺序  放入答案数组中。
对一个整数进行数位分割,指的是将整数各个数位按原本出现的顺序排列成数组。
    比方说,整数 10921 ,分割它的各个数位得到 [1,0,9,2,1] 。 
 
 
示例 1: 
输入: nums = [13,25,83,77]
输出: [1,3,2,5,8,3,7,7]
解释: 
- 分割 13 得到 [1,3] 。
- 分割 25 得到 [2,5] 。
- 分割 83 得到 [8,3] 。
- 分割 77 得到 [7,7] 。
answer = [1,3,2,5,8,3,7,7] 。answer 中的数字分割结果按照原数字在数组中的相同顺序排列。
 
示例 2: 
输入: nums = [7,1,3,9]
输出: [7,1,3,9]
解释: nums 中每个整数的分割是它自己。
answer = [7,1,3,9] 。
 
 
提示: 
    1 <= nums.length <= 1000 
    1 <= nums[i] <= 105  
 
解法 
方法一:模拟 
将数组中的每个数字进行数位分割,然后将分割后的数字依次放入答案数组中。
时间复杂度 \(O(n \times \log_{10} M)\) ,空间复杂度 \(O(n \times \log_{10} M)\) ,其中 \(n\)  为数组 \(nums\)  的长度,而 \(M\)  为数组 \(nums\)  中的最大值。
Python3 Java C++ Go TypeScript Rust C 
class   Solution : 
    def   separateDigits ( self ,  nums :  List [ int ])  ->  List [ int ]: 
        ans  =  [] 
        for  x  in  nums : 
            t  =  [] 
            while  x : 
                t . append ( x  %  10 ) 
                x  //=  10 
            ans . extend ( t [:: - 1 ]) 
        return  ans 
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 class  Solution   { 
     public   int []   separateDigits ( int []   nums )   { 
         List < Integer >   res   =   new   ArrayList <> (); 
         for   ( int   x   :   nums )   { 
             List < Integer >   t   =   new   ArrayList <> (); 
             for   (;   x   >   0 ;   x   /=   10 )   { 
                 t . add ( x   %   10 ); 
             } 
             Collections . reverse ( t ); 
             res . addAll ( t ); 
         } 
         int []   ans   =   new   int [ res . size () ] ; 
         for   ( int   i   =   0 ;   i   <   ans . length ;   ++ i )   { 
             ans [ i ]   =   res . get ( i ); 
         } 
         return   ans ; 
     } 
} 
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 class   Solution   { 
public : 
     vector < int >   separateDigits ( vector < int >&   nums )   { 
         vector < int >   ans ; 
         for   ( int   x   :   nums )   { 
             vector < int >   t ; 
             for   (;   x ;   x   /=   10 )   { 
                 t . push_back ( x   %   10 ); 
             } 
             while   ( t . size ())   { 
                 ans . push_back ( t . back ()); 
                 t . pop_back (); 
             } 
         } 
         return   ans ; 
     } 
}; 
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 func   separateDigits ( nums   [] int )   ( ans   [] int )   { 
     for   _ ,   x   :=   range   nums   { 
         t   :=   [] int {} 
         for   ;   x   >   0 ;   x   /=   10   { 
             t   =   append ( t ,   x % 10 ) 
         } 
         for   i ,   j   :=   0 ,   len ( t ) - 1 ;   i   <   j ;   i ,   j   =   i + 1 ,   j - 1   { 
             t [ i ],   t [ j ]   =   t [ j ],   t [ i ] 
         } 
         ans   =   append ( ans ,   t ... ) 
     } 
     return 
} 
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 function   separateDigits ( nums :   number []) :   number []   { 
     const   ans :   number []   =   []; 
     for   ( let   num   of   nums )   { 
         const   t :   number []   =   []; 
         while   ( num )   { 
             t . push ( num   %   10 ); 
             num   =   Math . floor ( num   /   10 ); 
         } 
         ans . push (... t . reverse ()); 
     } 
     return   ans ; 
} 
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 impl   Solution   { 
     pub   fn   separate_digits ( nums :   Vec < i32 > )   ->   Vec < i32 >   { 
         let   mut   ans   =   Vec :: new (); 
         for   & num   in   nums . iter ()   { 
             let   mut   num   =   num ; 
             let   mut   t   =   Vec :: new (); 
             while   num   !=   0   { 
                 t . push ( num   %   10 ); 
                 num   /=   10 ; 
             } 
             t . into_iter (). rev (). for_each ( | v |   ans . push ( v )); 
         } 
         ans 
     } 
} 
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 /** 
 * Note: The returned array must be malloced, assume caller calls free(). 
 */ 
int *   separateDigits ( int *   nums ,   int   numsSize ,   int *   returnSize )   { 
     int   n   =   0 ; 
     for   ( int   i   =   0 ;   i   <   numsSize ;   i ++ )   { 
         int   t   =   nums [ i ]; 
         while   ( t   !=   0 )   { 
             t   /=   10 ; 
             n ++ ; 
         } 
     } 
     int *   ans   =   malloc ( sizeof ( int )   *   n ); 
     for   ( int   i   =   numsSize   -   1 ,   j   =   n   -   1 ;   i   >=   0 ;   i -- )   { 
         int   t   =   nums [ i ]; 
         while   ( t   !=   0 )   { 
             ans [ j -- ]   =   t   %   10 ; 
             t   /=   10 ; 
         } 
     } 
     * returnSize   =   n ; 
     return   ans ; 
} 
 
 
 
 
方法二