31 ms Java Ugly Code


  • 0
    M
    class Solution {
        class StackCell {
            int time;
            boolean period;
            StackCell(int time, boolean period) {
                this.time = time;
                this.period = period;
            }
        }   
        public int[] exclusiveTime(int n, List<String> logs) {
            Stack<StackCell> stack = new Stack<>();
            int[] result = new int[logs.size()/2];
            int index = result.length - 1;
            for (String s: logs) {
                int[] temp = help(s);
                int id = temp[0];
                int type = temp[1];
                int time = temp[2];
                if (type == 0) {
                    stack.push(new StackCell(time, false));
                } else {
                    int timePeriod = 0;
                    while (stack.peek().period == true) {
                        timePeriod += stack.pop().time;
                    }
                    int startTime = stack.pop().time;
                    result[id] += time - startTime - timePeriod + 1;
                    stack.push(new StackCell(time - startTime + 1, true));
                }       
            }
            while (result[index] == 0) {
                index--;
            }
            return Arrays.copyOf(result, index + 1);
        }
        private int[] help(String s) {
            int[] result = new int[3];
            int indexOfCo = s.indexOf(":");
            int indexOfSecCo = s.indexOf(":", indexOfCo + 1);
            result[0] = Integer.parseInt(s.substring(0,indexOfCo));
            result[1] = s.charAt(indexOfCo + 1) == 's'?0:1;
            result[2] = Integer.parseInt(s.substring(indexOfSecCo + 1));
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.