Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
flowingrain
LeetCode-java
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode-java
/
L137_SingleNumberII.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
68 lines (51 loc) · 2.32 KB
master
Breadcrumbs
LeetCode-java
/
L137_SingleNumberII.java
Copy path
Top
File metadata and controls
Code
Blame
68 lines (51 loc) · 2.32 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package LeetCode;
//位操作
public class L137_SingleNumberII {
/*public int singleNumber(int[] nums) {
int result=0;
for(int i=0;i<32;i++)//逐位对result进行设置
{
int sum=0;
for(int j=0;j<nums.length;j++){
//如果这些数字的当前位为1,就用sum记录当前位为1的数目
//如果是1向左平移i位,求且的结果就不等于1了
sum+=(nums[j]>>i) &1;
}
result|=(sum%3)<<i;
}
return result;
}*/
/*
我们想要达到的效果其实是——
a b
初始状态 : 0 0
第一次碰见某个数x: 0 x(把x记录在b中)
第二次碰见某个数x: x 0(把x记录在a中)
第三次碰见某个数x: 0 0(把a和b都清空,可以处理其他数)
还记得我们之前处理“所有元素都出现两次,只有一个特殊元素出现一次”的问题吗?其实我们那会想要达到的状态也是——
a
初始状态 : 0
第一次碰见某个数x: x(把x记录在a中)
第二次碰见某个数x: 0(把a清空)
然后我们刚好就找到了异或运算可以处理这个问题。
那么这次我们同样利用异或运算,看能不能设计出一种变换的方法让a和b按照上述变换规则,进行转换。
b=0时碰到x,就变成x;b=x时再碰到x,就变成0,这个不就是异或吗?所以我们也许可以设计b=b xor x。
但是当b=0时再再碰到x,这时候b还是要为0,但这时候不同的是a=x,而前两种情况都是a=0。所以我们可以设计成:b=(b xor x)&~a
同样道理,我们可以设计出:a=(a xor x)&~b
在这种变换规则下,a和b都能按照我们设定的状态来发生转化。最后那个只出现一次的元素必定存储在b中。
*/
public int singleNumber(int[] nums) {
int a =0;
int b =0;
for(int i=0;i<nums.length;i++){
b = b ^ nums[i] & ~a;
a = a ^ nums[i] & ~b;
}
return a|b;
}
public static void main(String[] args){
L137_SingleNumberII l137=new L137_SingleNumberII();
int[] nums={0,1,0,1,0,1,99};
System.out.println(l137.singleNumber(nums));
}
}
You can’t perform that action at this time.