惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

面试 | 酷 壳 - CoolShell

Leetcode 编程训练 | 酷 壳 - CoolShell C++面试中string类的一种正确写法 | 酷 壳 - CoolShell 为什么我反对纯算法面试题 | 酷 壳 - CoolShell 一个fork的面试题 | 酷 壳 - CoolShell 给程序员新手的一些建议 | 酷 壳 - CoolShell 再谈“我是怎么招聘程序员的”(上) | 酷 壳 - CoolShell 再谈“我是怎么招聘程序员的”(下) | 酷 壳 - CoolShell 面试题:火车运煤问题 | 酷 壳 - CoolShell 又一个有趣的面试题 | 酷 壳 - CoolShell “火柴棍式”程序员面试题 | 酷 壳 - CoolShell 打印质数的各种算法 | 酷 壳 - CoolShell 输出从1到1000的数 | 酷 壳 - CoolShell 140个Google的面试题 | 酷 壳 - CoolShell 我是怎么招聘程序员的 | 酷 壳 - CoolShell
面试题:布尔变量 | 酷 壳 - CoolShell
陈皓 · 2010-06-23 · via 面试 | 酷 壳 - CoolShell

下面这篇文章是从StackOverflow来的。LZ面试的时候遇到了一道面试题:“如果有三个Bool型变量,请写出一程序得知其中有2个以上变量的值是true”,于是LZ做了下面的这样的程序:

boolean atLeastTwo(boolean a, boolean b, boolean c) {
    if ((a && b) || (b && c) || (a && c)) {
        return true;
    } else {
        return false;
    }
}

面试官接着问到,请对你的这个程序改进一下,但LZ不知道怎么改进,于是上StackOverflow上问了一下,下面是StackOverflow上的众网友的回答。再往下看的时候,希望你自己能先想一想怎么改进。

有人说,如果你有下面这样的代码?

    if (someExpression) {
        return true;
    } else {
        return false;
    }

你应该改成:

return someExpression;

所以,LZ的代码应该写成:

return ((a && b) || (b && c) || (a && c));

当然,解法不单单只有一种,还有下面的这些解决:

1)使用卡诺图

return a ? (b || c) : (b && c);

2)使用异或

return a ^ b ? c : a

3)按照字面

(a?1:0)+(b?1:0)+(c?1:0) >= 2

a&&b || b&&c || a&&c

4)把Bool当成0和1

a&b | b&c | c&a

a + b + c <= 2

5)如果bool不能当成0和1,则:

int howManyBooleansAreTrue =
(a ? 1 : 0)
+ (b ? 1 : 0)
+ (c ? 1 : 0);

return howManyBooleansAreTrue >= 2;

欢迎你留下你的想法。

Loading...