剑指offer
ruzewei
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
剑指Offer:字符串的排列
class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: # res用来存放结果 if not nums: return [] res = [] used = [0] * len(nums) def backtracking(nums, used, path): # 终止条件 .原创 2021-08-14 15:48:14 · 274 阅读 · 0 评论 -
剑指Offer:构建乘积数组
Python Reduce (out of time) class Solution(object): def constructArr(self, a): """ :type a: List[int] :rtype: List[int] """ B = [] if len(a) == 0: return B else: for i in原创 2021-08-13 22:40:14 · 148 阅读 · 0 评论 -
剑指Offer:数组中重复的数字
class Solution(object): def findRepeatNumber(self, nums): """ :type nums: List[int] :rtype: int """ num_dict = dict() for x in nums: if x not in num_dict: num_dict[x] = 1 .原创 2021-08-13 22:00:35 · 110 阅读 · 0 评论 -
剑指 Offer II 004. 只出现一次的数字
字典法 class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ num_dict = dict() for x in nums: if x in num_dict: num_dict[x] += 1原创 2021-08-13 21:32:09 · 179 阅读 · 0 评论 -
剑指Offer:数组中只出现一次的数字
位运算 class Solution(object): def singleNonDuplicate(self, nums): """ :type nums: List[int] :rtype: int """ n = 0 for x in nums: n ^= x return n 二分法 class Solution(object): def原创 2021-08-13 20:53:08 · 103 阅读 · 0 评论 -
剑指Offer:数字在排序数组中出现的次数
class Solution(object): def singleNumbers(self, nums): """ :type nums: List[int] :rtype: List[int] """ x, y, n, m = 0, 0, 0, 1 for item in nums: n ^= item while n & m == 0: .原创 2021-08-13 19:10:45 · 123 阅读 · 0 评论 -
剑指Offer:数组中的逆序对
class Solution(object): def reversePairs(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) <= 1: return 0 temp = [x for x in nums] return self.mergeSort(temp, num.原创 2021-08-13 12:50:15 · 127 阅读 · 0 评论 -
剑指Offer:把数组排成最小的数
class Solution(object): def minNumber(self, nums): """ :type nums: List[int] :rtype: str """ if len(nums) == 0: return None compare = lambda a, b : cmp(str(a) + str(b), str(b) + str(a)) .原创 2021-08-12 16:53:08 · 102 阅读 · 0 评论 -
剑指Offer:连续子数组的最大和
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ sub_sum = nums[0] max_sum = nums[0] for i in range(1, len(nums)): if sub_sum < 0: .原创 2021-08-12 14:55:16 · 106 阅读 · 0 评论 -
剑指Offer:数组中出现次数超过一半的数字
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ thresh = len(nums) // 2 cnt_dict = dict() for i in range(len(nums)): if nums[i] in...原创 2021-08-11 17:18:33 · 118 阅读 · 0 评论 -
剑指Offer:调整数组顺序使奇数位于偶数前面
Brutal Force class Solution(object): def exchange(self, nums): """ :type nums: List[int] :rtype: List[int] """ res = [] length = len(nums) for i in range(length): if nums[i] % 2 == 1原创 2021-08-11 16:53:39 · 109 阅读 · 0 评论 -
剑指Offer:二维数组中的查找
Brutal Force class Solution(object): def findNumberIn2DArray(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ res = False row_num = len(matrix)原创 2021-08-05 19:46:37 · 134 阅读 · 0 评论
分享