Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does LeetCode find out the number that appears only once?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how LeetCode can find out the number that appears only once. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

one

Topic description

Given a non-empty integer array, only one number appears once and the rest appear twice. Find the number that appears only once. For example, input [3Jing 4jue 5JI 4JO 3], output 5.

two

Knowledge point

Idea 1: set up a hash table to record the number of times each value appears

Doing exercises two days ago is to set up a hash table, and this is the first reaction of thinking inertia. Iterate through each value, establish a dictionary to record the number of occurrences, and return a value with the number of occurrences of 1.

Class Solution: def singleNumber (self, nums: List [int])-> int: countnum=dict () for i in nums: countnum [I] = countnum[ I] + 1 else: countnum [I] = 1 for e in countnum.items (): if v = = 1: return e idea 2: set difference

Set in python represents an unordered and unrepeatable set, and the difference between the sets can be directly calculated to get different values in the two sets.

Class Solution: def singleNumber (self, nums: List [int])-> int: nums.sort () return list (set (nums [:: 2])-set (nums [1:: 2])) [0] idea 3: XOR operation (bit operation)

Look at the methods that other people see in their ideas for solving problems. The rule of XOR operation is: if the values of an and b are different, the result is 1; if the values of an and b are the same, the result is 0. It is stored in binary system in the computer, so the result of XOR is as follows: 3 is 011, 5 is 101, XOR is 110, and then XOR with 3 is 011 ^ 110 = 101, that is, the desired result 5.

Class Solution: def singleNumber (self, nums: List [int])-> int: res = 0 for i in nums: res ^ = i return res

This is the end of this article on "how to find out the number that appears only once in LeetCode". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report