ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-08-24 21:32:20  阅读:203  来源: 互联网

标签:function Exclusive Functions 函数 636 int timestamp ID stack


目录

一,题目描述

英文描述

中文描述

示例与说明

二,解题思路

三,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;}
    }
}

四,解题过程

第一博

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

标签:function,Exclusive,Functions,函数,636,int,timestamp,ID,stack
来源: https://blog.csdn.net/qq_41528502/article/details/119898762

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有