零神大数据:
1880,Check if Word Equals Summation of Two Words,check-if-word-equals-summation-of-two-words,1187.1641565458
1881,Maximum Value after Insertion,maximum-value-after-insertion,1381.2168789318
1882,Process Tasks Using Servers,process-tasks-using-servers,1979.1112273597
1883,Minimum Skips to Arrive at Meeting On Time,minimum-skips-to-arrive-at-meeting-on-time,2587.8725248485
1880. Check if Word Equals Summation of Two Words
签到题。按题目要求转换字符串到数字,再进行判断。
1 2 3 4 5 6 7 8 9
classSolution: defisSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool: deftoDigit(w): ans = 0 for i in w: ans = ans * 10 + ord(i) - ord('a') return ans return toDigit(firstWord) + toDigit(secondWord) == toDigit(targetWord)
时间复杂度: O(N),
空间复杂度: O(N).
1881. Maximum Value after Insertion
贪心。正数时插入到第一个比x小的数,负数时插入到第一个比x大的数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defmaxValue(self, n: str, x: int) -> str: # insert into the position where the first digit < x # negative first > x if n[0] == '-': for i inrange(1, len(n)): iford(n[i]) - ord('0') > x: return n[:i] + str(x) + n[i:] return n + str(x) else: for i inrange(len(n)): iford(n[i]) - ord('0') < x: return n[:i] + str(x) + n[i:] return n + str(x)