dengxinyi il y a 6 ans
Parent
commit
c72be397d0
1 fichiers modifiés avec 23 ajouts et 0 suppressions
  1. 23 0
      medium/636.exclusive-time-of-functions.go

+ 23 - 0
medium/636.exclusive-time-of-functions.go

@@ -0,0 +1,23 @@
+func exclusiveTime(n int, logs []string) []int {
+	res := make([]int, n)
+	if len(logs) == 0 {
+		return res
+	}
+	pre := -1 // All func start from 0.
+	st := []int{0}
+	for i := range logs {
+		log := strings.Split(logs[i], ":")
+		id, _ := strconv.Atoi(log[0])
+		cur, _ := strconv.Atoi(log[2])
+		res[id]++
+		if log[1] == "start" {
+			res[st[len(st)-1]] += cur - pre - 1
+			st = append(st, id)
+		} else {
+			res[id] += cur - pre - 1
+			st = st[:len(st)-1]
+		}
+		pre = cur
+	}
+	return res
+}