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 C++ judge a number that appears only once?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to judge the number that appears only once on C++". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how C++ judges numbers that appear only once".

Arithmetic

In the use of bit operation XOR (1), two identical numbers are XOR 0, which is converted into 3-digit XOR operation bit 0, that is, we need to implement three 1s of the same bit bit, and the operation is 0, which converts the problem into the operation a?b?c = 0 of how to implement the operation of three numbers of the same bit.

Because the XOR operation of two digits is the addition of two numbers of the same bit bit, ignoring the carry situation, in other words, the addition of two numbers takes the remainder of 2. So the operation of a?b?c = 0 of three identical numbers becomes the remainder of the three-digit operation on 3. By the same token, 4 digits and n digits can be realized by this algorithm.

Topic: numbers that appear only once

Algorithm 1: mathematical formula

This topic can be translated into the following formula: 2c = 3 (a+b+c)-sum

Here, a _ () _ b _ (c) is an element that appears in an array, c is a number that appears once, and _ _ b _ _ is a number that appears three times.

Sum represents the sum of all the numbers in the array.

Note: the same algorithm is also suitable for an array with n repeats and a non-repeating number. The formula is: n (a+b+c)-sum = (nmai 1) c

Code implementation:

Func singleNumber (nums [] int) int {m: = make (map [int]) S1 for S2: = 0 for _, n:=range nums {_, ok: = m [n] if! ok {m [n] = n} S1 + = n} for _ VRV = range m {S2 + = v} res: = (3*s2-s1) / 2 return res} / / algorithm: suppose there are 3 a+b+c b, one c: formula: 3 (a+b+c)-sum = 2c

Algorithm 2: using bit operation

The guiding idea is: convert to the previous algorithm: the use of bit operation XOR (1), two identical numbers XOR is 0, which is converted into 3-digit "XOR" operation bit 0, that is, we need to achieve the same bit bit of 3 1s, the operation is 0, and the problem is converted into the operation of how to achieve the operation a?b?c = 0 of three numbers of the same bit.

Because the two-digit operation uses XOR, that is:

1 ^ 1 = 0 1 ^ 0 = 10 ^ 1 = 10 ^ 0 = 0 this is actually the addition of two numbers of the same bit bit, ignoring the carry case, in other words, the two numbers add up to the remainder of 2.

So the operation of a?b?c = 0 of three identical numbers becomes the remainder of the three-digit operation on 3.

Code implementation:

Func singleNumber (nums [] int) int {num,res: = 0num,res: = 0 for iGV > I) & 1} / / restore the calculation result to the corresponding bit bit res | = (num)% 3

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