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
Developer articles
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 }}
XILINN
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm
/
src
/
class030
/
Code02_GetMaxWithoutJudge.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
59 lines (52 loc) · 1.52 KB
main
Breadcrumbs
algorithm
/
src
/
class030
/
Code02_GetMaxWithoutJudge.java
Copy path
Top
File metadata and controls
Code
Blame
59 lines (52 loc) · 1.52 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
package
class030
;
// 不用任何判断语句和比较操作,返回两个数的最大值
// 测试链接 : https://www.nowcoder.com/practice/d2707eaf98124f1e8f1d9c18ad487f76
public
class
Code02_GetMaxWithoutJudge
{
// 必须保证n一定是0或者1
// 0变1,1变0
public
static
int
flip
(
int
n
) {
return
n
^
1
;
}
// 非负数返回1
// 负数返回0
public
static
int
sign
(
int
n
) {
return
flip
(
n
>>>
31
);
}
// 有溢出风险的实现
public
static
int
getMax1
(
int
a
,
int
b
) {
int
c
=
a
-
b
;
// c非负,returnA -> 1
// c非负,returnB -> 0
// c负数,returnA -> 0
// c负数,returnB -> 1
int
returnA
=
sign
(
c
);
int
returnB
=
flip
(
returnA
);
return
a
*
returnA
+
b
*
returnB
;
}
// 没有任何问题的实现
public
static
int
getMax2
(
int
a
,
int
b
) {
// c可能是溢出的
int
c
=
a
-
b
;
// a的符号
int
sa
=
sign
(
a
);
// b的符号
int
sb
=
sign
(
b
);
// c的符号
int
sc
=
sign
(
c
);
// 判断A和B,符号是不是不一样,如果不一样diffAB=1,如果一样diffAB=0
int
diffAB
=
sa
^
sb
;
// 判断A和B,符号是不是一样,如果一样sameAB=1,如果不一样sameAB=0
int
sameAB
=
flip
(
diffAB
);
int
returnA
=
diffAB
*
sa
+
sameAB
*
sc
;
int
returnB
=
flip
(
returnA
);
return
a
*
returnA
+
b
*
returnB
;
}
public
static
void
main
(
String
[]
args
) {
int
a
=
Integer
.
MIN_VALUE
;
int
b
=
Integer
.
MAX_VALUE
;
// getMax1方法会错误,因为溢出
System
.
out
.
println
(
getMax1
(
a
,
b
));
// getMax2方法永远正确
System
.
out
.
println
(
getMax2
(
a
,
b
));
}
}
You can’t perform that action at this time.