Repeated digital problem-solving report (Python) __python in the array of "sword point offer"

Source: Internet
Author: User
"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 = =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.