LeetCode_Stack_636. Exclusive Time of Functions 函数的独占时间【栈,字符串处理】

目录

一,题目描述

英文描述

中文描述

示例与说明

二,解题思路

三,AC代码

Java

四,解题过程

第一博


一,题目描述

英文描述

On a single-threaded CPU, we execute a program containing n functions. Each function has a unique ID between 0 and n-1.

Function calls are stored in a call stack: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is the current function being executed. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp.

You are given a list logs, where logs[i] represents the ith log message formatted as a string "{function_id}:{"start" | "end"}:{timestamp}". For example, "0:start:3" means a function call with function ID 0 started at the beginning of timestamp 3, and "1:end:2" means a function call with function ID 1 ended at the end of timestamp 2. Note that a function can be called multiple times, possibly recursively.

A function's exclusive time is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for 2 time units and another call executing for 1 time unit, the exclusive time is 2 + 1 = 3.

Return the exclusive time of each function in an array, where the value at the ith index represents the exclusive time for the function with ID i.

中文描述

有一个 单线程 CPU 正在运行一个含有 n 道函数的程序。每道函数都有一个位于  0 和 n-1 之间的唯一标识符。

函数调用 存储在一个 调用栈 上 :当一个函数调用开始时,它的标识符将会推入栈中。而当一个函数调用结束时,它的标识符将会从栈中弹出。标识符位于栈顶的函数是 当前正在执行的函数 。每当一个函数开始或者结束时,将会记录一条日志,包括函数标识符、是开始还是结束、以及相应的时间戳。

给你一个由日志组成的列表 logs ,其中 logs[i] 表示第 i 条日志消息,该消息是一个按 "{function_id}:{"start" | "end"}:{timestamp}" 进行格式化的字符串。例如,"0:start:3" 意味着标识符为 0 的函数调用在时间戳 3 的 起始开始执行 ;而 "1:end:2" 意味着标识符为 1 的函数调用在时间戳 2 的 末尾结束执行。注意,函数可以 调用多次,可能存在递归调用 。

函数的 独占时间 定义是在这个函数在程序所有函数调用中执行时间的总和,调用其他函数花费的时间不算该函数的独占时间。例如,如果一个函数被调用两次,一次调用执行 2 单位时间,另一次调用执行 1 单位时间,那么该函数的 独占时间 为 2 + 1 = 3 。

以数组形式返回每个函数的 独占时间 ,其中第 i 个下标对应的值表示标识符 i 的函数的独占时间。

示例与说明

示例 1:

示例 2:

示例 3:


示例 4:

示例 5:

提示:

  • 1 <= n <= 100
  • 1 <= logs.length <= 500
  • 0 <= function_id < n
  • 0 <= timestamp <= 10^9
  • 两个开始事件不会在同一时间戳发生
  • 两个结束事件不会在同一时间戳发生
  • 每道函数都有一个对应 "start" 日志的 "end" 日志

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/exclusive-time-of-functions
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二,解题思路

一个栈模拟题。

需要记录每个进程的编号和时间戳。可以采用内部类的形式实现单条日志的

  • 遇到start入栈日志,并更新栈顶线程的运行时间;
  • 遇到end,计算栈顶线程运行时间,弹出栈顶;

具体的可能看代码更直观一点。

三,AC代码

Java

class Solution {
    public int[] exclusiveTime(int n, List<String> logs) {
        int[] ans = new int[n];// int类型默认初始化为0
        Deque<Log> stack = new LinkedList<>();
        for (String log : logs) {
            String[] tem = log.split(":");
            
            if (tem[1].equals("start")) {
                Log l = new Log(Integer.parseInt(tem[0]), Integer.parseInt(tem[2]));
                if (!stack.isEmpty()) {
                    ans[stack.peek().getId()] += (l.getTimestamp() - stack.peek().getTimestamp());
                }
                stack.push(l);
                
            } else {
                ans[stack.peek().getId()] += (Integer.parseInt(tem[2]) - stack.peek().getTimestamp() + 1);
                stack.pop();// 默认是配对的,直接弹出即可
                if (!stack.isEmpty()) {
                    // 修改新的开始时间
                    stack.peek().setTimestamp(Integer.parseInt(tem[2])  + 1);
                }
            }
        }
        return ans;
        
    }
    class Log {
        int id;
        int timestamp;
        public Log(int id, int timestamp) {
            this.id = id;
            this.timestamp = timestamp;
        }
        public Log() {}
        public int getId() {return id;}
        public int getTimestamp() {return timestamp;}
        public void setTimestamp(int timestamp) {this.timestamp = timestamp;}
    }
}

四,解题过程

第一博

一发入魂,就是这么自信~ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值