Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a79dc8

Browse files
committed
update offer 03
1 parent 19bb098 commit 7a79dc8

File tree

13 files changed

+15
-12
lines changed

13 files changed

+15
-12
lines changed

‎Data Structures and Algorithms/recursion.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def fib(n):
5656
def fibRecursion(n): ## upDown
5757
def upDown(n):
5858
if n < 1: return 0
59-
if n <= 2:
60-
return 1
59+
if n <= 2: return 1
6160
if dp[n]: return dp[n]
6261
dp[n] = upDown(n-1) + upDown(n-2)
6362
return dp[n]

‎剑指 offer/03.py‎ renamed to ‎剑指offer/03.py‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#==================================================
2-
#==> Title:
2+
#==> Title: 剑指 Offer 03. 数组中重复的数字
33
#==> Author: Zhang zhen
44
#==> Email: hustmatnoble.gmail.com
55
#==> GitHub: https://github.com/MatNoble
@@ -10,25 +10,29 @@
1010
https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/
1111
"""
1212

13+
from collections import Counter
1314
class Solution:
1415
def findRepeatNumber(self, nums):
16+
# 计数法
17+
# return Counter(nums).most_common(1)[0][0]
18+
19+
# 哈希表
1520
# dict = set()
1621
# for num in nums:
1722
# if num in dict: return num
1823
# dict.add(num)
1924
# return -1
20-
def swap(nums, i, j):
21-
temp = nums[i]
22-
nums[i] = nums[j]
23-
nums[j] = temp
24-
n = len(nums)
25-
i = 0
25+
26+
# 原地置换 环
27+
i, n = 0, len(nums)
2628
while i < n:
27-
if nums[i] == i:
29+
if nums[i] == i:
2830
i += 1
2931
continue
30-
if nums[nums[i]] == nums[i]: return nums[i]
31-
swap(nums, i, nums[i])
32+
if nums[nums[i]] == nums[i]:
33+
return nums[i] # 环入口
34+
j = nums[i] # 归位
35+
nums[i], nums[j] = nums[j], nums[i]
3236
return -1
3337

3438
mat = Solution()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /