Java.Algo.Arrays.SeparateDigits

We can convert integer to string and then get chars, but more effective way is to use / and % operators, here is an example

package org.example;


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * https://leetcode.com/problems/separate-the-digits-in-an-array/
 */
public class Main {
    public static void main(String[] args) {
        separateDigits(new int[]{13, 25, 83, 77});
    }

    public static int[] separateDigits(int[] nums) {
        List<Integer> list = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums.length; i++) {
            convert(nums[i], list, stack);
        }

        return list.stream().mapToInt(i -> i).toArray();
    }

    private static void convert(int num, List<Integer> result, Stack<Integer> stack) {
        int nextNum = num;
        while (nextNum != 0) {
            int remainder = nextNum % 10;
            nextNum = nextNum / 10;
            stack.push(remainder);
        }
        while (!stack.isEmpty()) {
            result.add(stack.pop());
        }
    }
}

more effective example

class Solution {
    public int[] separateDigits(int[] nums) {
        int size = 0;
        for (int i : nums) {
            while (i > 0) {
                size++;
                i /= 10;
            }
        }

        int[] arr = new int[size];
        for (int i = nums.length - 1; i >= 0; i--) {
            int temp = nums[i];
            while (temp > 0) {
                arr[--size] = temp % 10;
                temp /= 10;
            }
        }
        return arr;
    }
}
This entry was posted in Без рубрики. Bookmark the permalink.