Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
yeahQing
java-leetcode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
java-leetcode
/
src
/
com
/
yeahqing
/
jianzhioffer
/
_1_056_1
/
Solution.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
49 lines (45 loc) · 1.64 KB
main
Breadcrumbs
java-leetcode
/
src
/
com
/
yeahqing
/
jianzhioffer
/
_1_056_1
/
Solution.java
Copy path
Top
File metadata and controls
Code
Blame
49 lines (45 loc) · 1.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.yeahqing.jianzhioffer._1_056_1;
import com.yeahqing.structure.Interval;
import java.util.Arrays;
/**
* @author YeahQing
* @date 2022/9/28
*/
class Solution {
/**
* 剑指 Offer 56 - I. 数组中数字出现的次数 middle
* 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。
* @param nums = [4,1,4,6]
* @return int[]
*/
public int[] singleNumbers(int[] nums) {
// 1. 所有值做异或,只出现一次的两个数字异或一定不为0
int num = 0;
for (int n : nums) {
// num = a ^ b, a和b表示只出现一次的两个数字
num ^= n;
}
// 2. 找到最低位的1
int div = 1;
while ((div & num) == 0) {
div <<= 1; // 从最低位开始判断num哪一位为1
}
// 3. 按照这一位将原数组分组,就是把a和b分别分到两组,之后进行异或就可以得到两个只出现一次的数字
int a = 0, b = 0;
for (int n : nums) {
// 同样的数字与的结果一定是相同的,也就是会分到同一个组
if ((div & n) == 0) {
a ^= n;
} else {
b ^= n;
}
}
return new int[]{a, b};
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = Interval.createTestData("[4,1,4,6]");
int[] ret = solution.singleNumbers(nums);
System.out.println(Arrays.toString(ret));
}
}
You can’t perform that action at this time.