classSolution: deffindPeaks(self, mountain: List[int]) -> List[int]: res = [] for i inrange(1,len(mountain)-1): if mountain[i] > mountain[i-1] and mountain[i] > mountain[i+1]: res.append(i) i += 1 return res
classSolution: defmaximumLength(self, s: str) -> int: group = defaultdict(list) count = 0 for i, chrinenumerate(s): count += 1 if i == len(s)-1orchr != s[i+1]: group[chr].append(count) count = 0 ans = 0 for a in group.values(): a.sort(reverse=True) a.extend([0,0]) ans = max(ans, a[0] - 2, min(a[0] - 1, a[1]), a[2]) if ans != 0: return ans else: return -1
给你一个下标从 0 开始的二维整数矩阵 grid,大小为 n * n ,其中的值在 [1, n^2] 范围内。除了 a 出现 两次,b缺失 之外,每个整数都 恰好出现一次 。
任务是找出重复的数字a 和缺失的数字 b 。
返回一个下标从 0 开始、长度为 2 的整数数组 ans ,其中 ans[0] 等于 a ,ans[1] 等于 b 。
思路
我的方法与题解比较不一样,不是最优解
这道题数据约定也不是很大,不会存在超时的风险。
假设不存在重复的数字,那么每个数字其实都是确定 1-n^2 的,用一个求和公式可以非常快的把所有数字之和找出来
然后 将这个和与实际的数组和相减,就可以得到 a 与 b 的 差
最后一步就是找出哪个是重复的,python直接sort数组就行了
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: deffindMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: num = [] ans = 0 ans_sum = (1 + len(grid)*len(grid))*len(grid)*len(grid) / 2 for i inrange(0,len(grid)): for j inrange(0,len(grid)): ans += grid[i][j] num.append(grid[i][j])
num.sort() for i inrange(0,len(num)-1): if num[i] == num[i+1]: return [num[i], num[i] + int(ans_sum) - ans]
classSolution: defdistributeCandies(self, n: int, limit: int) -> int: possible_choice = [] for i inrange(0, limit+1): possible_choice.append(i) ans = 0 for i in possible_choice: for j in possible_choice: for k in possible_choice: if i+j+k == n: ans += 1 return ans
classSolution: defdistributeCandies(self, candyType: List[int]) -> int: n = len(candyType) candyType.sort() cur = candyType[0] num_candyType = 1 for i inrange(0, n-1): if cur != candyType[i+1]: num_candyType += 1 cur = candyType[i+1] if num_candyType < n/2: return num_candyType else: returnint(n/2)
classSolution: defcountPairsOfConnectableServers(self, edges: List[List[int]], signalSpeed: int) -> List[int]: graph = [[] for _ inrange(len(edges) + 1)] # 变成邻接链表存储 for u, v, w in edges: graph[u].append((v, w)) graph[v].append((u, w)) defdfs(p: int, root: int, curr: int) -> int: res = 0 if curr == 0: res += 1 for v, cost in graph[p]: if v != root: res += dfs(v, p, (curr + cost) % signalSpeed) return res res = [0] * (len(edges) + 1) for i inrange(len(edges) + 1): pre = 0 for v, cost in graph[i]: count = dfs(v, i, cost % signalSpeed) res[i] += pre * count pre += count return res
classSolution: defminimumSteps(self, s: str) -> int: n = len(s) ans = 0 first_1 = 0
if'1'notin s: return0
while s[first_1] != '1': first_1 += 1 cnt_0 = 0 for i inrange(first_1, n): if s[i] == '0': cnt_0 += 1 pre = first_1 ans += cnt_0 if pre != n - 1: for i inrange(first_1 + 1, n): if s[i] == '1': cnt_0 -= i - pre - 1 ans += cnt_0 pre = i return ans
classSolution: defmaxOperations(self, nums: List[int]) -> int: n = nums[0]+nums[1] res = 0 whilelen(nums) >= 2and nums[0]+nums[1] == n: del nums[0:2] res += 1 return res
for i inrange(len(nums)-1, -1, -1): for j inrange(i+2, len(nums)+2): for k inrange(i+1, j): ans = temp_extend[i] * temp_extend[k] * temp_extend[j] ans += dp_list[i][k] + dp_list[k][j] dp_list[i][j] = max(dp_list[i][j], ans) return dp_list[0][len(nums) + 1]
classSolution: defcountBattleships(self, board: List[List[str]]) -> int: m = len(board) n = len(board[0]) ans = 0 for i inrange(m): for j inrange(n): if board[i][j] == 'X': if (i == 0or board[i-1][j] != 'X') and (j == 0or board[i][j-1] != 'X'): ans += 1 return ans
ans = -1 for i, s inenumerate(strs): check = True for j, t inenumerate(strs): if i != j and compare(s, t): check = False break if check: ans = max(ans, len(s)) return ans
classSolution: defmaxIncreasingCells(self, mat: List[List[int]]) -> int: g = defaultdict(list) for i, row inenumerate(mat): for j, x inenumerate(row): g[x].append((i, j))
row_max = [0] * len(mat) col_max = [0] * len(mat[0]) for _, pos insorted(g.items(), key=lambda p: p[0]): fs = [max(row_max[i], col_max[j]) + 1for i, j in pos] for (i, j), f inzip(pos, fs): row_max[i] = max(row_max[i], f) col_max[j] = max(col_max[j], f) returnmax(row_max)
classSolution: defcountBeautifulPairs(self, nums: List[int]) -> int: n = len(nums) ans = 0 for i inrange(n): for j inrange(i + 1, n): ans_f = int(str(nums[i])[0]) ans_l = nums[j] % 10 if gcd(ans_f, ans_l) == 1: ans += 1 return ans
classSolution: defgoodSubsetofBinaryMatrix(self, grid: List[List[int]]) -> List[int]: mask_to_idx = {} for i, row inenumerate(grid): mask = 0 for j, x inenumerate(row): mask |= x << j if mask == 0: return [i] mask_to_idx[mask] = i
for x, i in mask_to_idx.items(): for y, j in mask_to_idx.items(): if (x & y) == 0: returnsorted((i, j)) return []