"sword refers to offer" in the array of repeated digital problem solving report (Python)
tags (space delimited): Sword refers to an offer
Topic Address: Https://www.nowcoder.com/ta/coding-interviews topic Description:
All numbers in an array of length n are in the range of 0 to n-1. Some of the numbers in the array are duplicates, but there are several numbers that are not known to be duplicates. Nor do we know how many times each number repeats. Please find any duplicate numbers in the array. For example, if you enter an array {2,3,1,0,2,5,3} with a length of 7, the corresponding output is the first duplicate number 2. methods of Solving problems
Leetcode the original title, this idea is particularly ingenious, use the speed pointer, to determine whether there is a logical ring exists in the array. The return way of the topic needs attention. The other is attention to the speed of the pointer initialization mode.
Code:
#-*-Coding:utf-8-*-
class Solution:
# here to pay special attention ~ Find any duplicate value and assign to duplication[0]
# function returns TRUE/FALSE
def Duplicate (self, nums, duplication):
If Len (set (nums)) = = Len (nums):
duplication[0] =-1 return
False
Fast = Nums[nums[0]]
slow = nums[0] While
fast!= slow:
fast = Nums[nums[fast]]
slow = Nums[slow]
Fast = 0 While
fast!= slow:
fast = Nums[nums[fast]]
slow = Nums[slow]
duplication[0] = Nums[fast]
return True
Date
March 26, 2018-Learn to get up early, sleepy = =