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 to use java to view numbers that appear only once

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article "how to use java to view numbers that appear only once" is not quite understood by most people except programmers. Today, in order to make you better understand "how to use java to view numbers that appear only once", the editor summarizes the following content, which has certain reference value, detailed steps are clear, and details are handled properly. I hope you can gain something through this article. Now let's take a look at the details.

Given an integer array nums, there are exactly two elements that appear only once, and all other elements appear twice. Find out the two elements that appear only once.

Example:

Input: output:

Answer:

1public int [] singleNumber (int [] nums) {

2 int diff = 0

3 for (int num: nums) {

4 diff ^ = num

5}

6 diff & =-diff

7 int [] rets = {0,0}

8 for (int num: nums) {

9 if ((num & diff) = = 0) {

10 rets [0] ^ = num

11} else {

12 rets [1] ^ = num

13}

14}

15 return rets

16}

Parsing:

There are many solutions to this problem, only this one solution is listed here. If you want to know more, you can refer to the solutions of the previous two questions. The title says that only two numbers appear once, and the others appear twice. We put all the values into XOR once, and the result of XOR is that the two XOR values only appear once, because the result of both XOR values is 0. Because the two values are different, their XOR result must not be 0, that is to say, there must be a 1 on a bit where the result is converted into binary, so we can divide the array into two parts according to this 1. and the two that appear only once will definitely not be in the same group, according to this feature we can easily find these two values.

What can Java be used for? Java is mainly used in: 1. Web development; 2. Android development; 3. Client development; 4. Web page development; 5. Enterprise application development; 6. Java big data development; 7. Game development and so on.

Thank you for your reading. I hope you have a certain understanding of the key question of "how to use java to view numbers that appear only once". The specific usage still needs to be understood by hands-on experiments. Go and try it. If you want to read more articles about relevant knowledge points, welcome to follow the industry information channel!

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