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 }}
codingyes
algorithm-journey
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm-journey
/
src
/
class076
/
Code06_BooleanEvaluation.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
67 lines (63 loc) · 1.83 KB
main
Breadcrumbs
algorithm-journey
/
src
/
class076
/
Code06_BooleanEvaluation.java
Copy path
Top
File metadata and controls
Code
Blame
67 lines (63 loc) · 1.83 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
package
class076
;
// 布尔运算
// 给定一个布尔表达式和一个期望的布尔结果 result
// 布尔表达式由 0 (false)、1 (true)、& (AND)、 | (OR) 和 ^ (XOR) 符号组成
// 布尔表达式一定是正确的,不需要检查有效性
// 但是其中没有任何括号来表示优先级
// 你可以随意添加括号来改变逻辑优先级
// 目的是让表达式能够最终得出result的结果
// 返回最终得出result有多少种不同的逻辑计算顺序
// 测试链接 : https://leetcode.cn/problems/boolean-evaluation-lcci/
public
class
Code06_BooleanEvaluation
{
// 记忆化搜索
public
static
int
countEval
(
String
str
,
int
result
) {
char
[]
s
=
str
.
toCharArray
();
int
n
=
s
.
length
;
int
[][][]
dp
=
new
int
[
n
][
n
][];
int
[]
ft
=
f
(
s
,
0
,
n
-
1
,
dp
);
return
ft
[
result
];
}
// s[l...r]是表达式的一部分,且一定符合范式
// 0/1 逻 0/1 逻 0/1
// l l+1 l+2 l+3........r
// s[l...r] 0 : ?
// 1 : ?
// ans : int[2] ans[0] = false方法数 ans[0] = true方法数
public
static
int
[]
f
(
char
[]
s
,
int
l
,
int
r
,
int
[][][]
dp
) {
if
(
dp
[
l
][
r
] !=
null
) {
return
dp
[
l
][
r
];
}
int
f
=
0
;
int
t
=
0
;
if
(
l
==
r
) {
// 只剩一个字符,0/1
f
=
s
[
l
] ==
'0'
?
1
:
0
;
t
=
s
[
l
] ==
'1'
?
1
:
0
;
}
else
{
int
[]
tmp
;
for
(
int
k
=
l
+
1
,
a
,
b
,
c
,
d
;
k
<
r
;
k
+=
2
) {
// l ... r
// 枚举每一个逻辑符号最后执行 k = l+1 ... r-1 k+=2
tmp
=
f
(
s
,
l
,
k
-
1
,
dp
);
a
=
tmp
[
0
];
b
=
tmp
[
1
];
tmp
=
f
(
s
,
k
+
1
,
r
,
dp
);
c
=
tmp
[
0
];
d
=
tmp
[
1
];
if
(
s
[
k
] ==
'&'
) {
f
+=
a
*
c
+
a
*
d
+
b
*
c
;
t
+=
b
*
d
;
}
else
if
(
s
[
k
] ==
'|'
) {
f
+=
a
*
c
;
t
+=
a
*
d
+
b
*
c
+
b
*
d
;
}
else
{
f
+=
a
*
c
+
b
*
d
;
t
+=
a
*
d
+
b
*
c
;
}
}
}
int
[]
ft
=
new
int
[] {
f
,
t
};
dp
[
l
][
r
] =
ft
;
return
ft
;
}
}
You can’t perform that action at this time.