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

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers 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...