636.exclusive-time-of-functions.go 481 B

1234567891011121314151617181920212223
  1. func exclusiveTime(n int, logs []string) []int {
  2. res := make([]int, n)
  3. if len(logs) == 0 {
  4. return res
  5. }
  6. pre := -1 // All func start from 0.
  7. st := []int{0}
  8. for i := range logs {
  9. log := strings.Split(logs[i], ":")
  10. id, _ := strconv.Atoi(log[0])
  11. cur, _ := strconv.Atoi(log[2])
  12. res[id]++
  13. if log[1] == "start" {
  14. res[st[len(st)-1]] += cur - pre - 1
  15. st = append(st, id)
  16. } else {
  17. res[id] += cur - pre - 1
  18. st = st[:len(st)-1]
  19. }
  20. pre = cur
  21. }
  22. return res
  23. }