当前位置:网站首页>[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
2022-07-06 04:14:00 【tomly2020】
The thirteenth day
1189 “ balloon ” Maximum number of
Give you a string text, You need to use text To piece together as many words as possible “balloon”( balloon ).
character string text Each letter in can only be used once at most . Please return the maximum number of words you can piece together “balloon”.
Method
Count all letters and... Of the given string balloon Number of relevant letters , The one with the least number of choices is the number of answers .
class Solution {
public int maxNumberOfBalloons(String text) {
int[] cnts = new int[26];
for (int i = 0; i < text.length(); ++i){
cnts[text.charAt(i) - 'a']++;
}
return Math.min(cnts['a' - 'a'], Math.min(cnts['b' - 'a'], Math.min(cnts['l' - 'a'] / 2, Math.min(cnts['o' - 'a'] / 2, cnts['n' - 'a']))));
}
}
201 Digital range bitwise AND
Here are two integers left and right , Indicates the interval [left, right] , Returns all numbers in this range Bitwise AND Result ( contain left 、right Endpoint ).
Method
We start from the lowest position , stay 32 Bit binary number , The lowest value can only be 0 perhaps 1, Because it is bitwise AND , Basis and nature , Only all numbers are not 0 when , Results can be returned 1, So it's easy to know , If left and right It's not equal , Then the binary value in the current position is 0, So we just need to left and right Move one bit to the right , Then judge whether it is equal or not , If equal , Then the binary value on the corresponding bit is 1.
class Solution {
public int rangeBitwiseAnd(int left, int right) {
int cmp = 1;
int res = 0;
for (int i = 0; i < 31; ++i){
if (right == left && ((right & 1) == 1)){
res = res | cmp;
}
right >>= 1;
left >>= 1;
cmp <<= 1;
if (left == 0 || right == 0) break;
}
return res;
}
}
边栏推荐
- 解决“C2001:常量中有换行符“编译问题
- When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
- 80% of the diseases are caused by bad living habits. There are eight common bad habits, which are both physical and mental
- Maxay paper latex template description
- Lambda expression learning
- Leetcode32 longest valid bracket (dynamic programming difficult problem)
- Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
- [Key shake elimination] development of key shake elimination module based on FPGA
- Script lifecycle
- Interface idempotency
猜你喜欢

Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control

Recommendation system (IX) PNN model (product based neural networks)

IDEA编译JSP页面生成的class文件路径

Record the pit of NETCORE's memory surge

SSTI template injection explanation and real problem practice

10个 Istio 流量管理 最常用的例子,你知道几个?

自动化测试的好处

Detailed explanation of serialization and deserialization

Chinese brand hybrid technology: there is no best technical route, only better products

DM8 archive log file manual switching
随机推荐
C (thirty) C combobox listview TreeView
Overturn your cognition? The nature of get and post requests
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
Determine which week of the month the day is
综合能力测评系统
使用JS完成一个LRU缓存
One question per day (Mathematics)
Codeforces Round #770 (Div. 2) B. Fortune Telling
Practical development of member management applet 06 introduction to life cycle function and user-defined method
BOM - location, history, pop-up box, timing
Proof of Stirling formula
P2102 地砖铺设(dfs&贪心)
Web components series (VII) -- life cycle of custom components
电脑钉钉怎么调整声音
【可调延时网络】基于FPGA的可调延时网络系统verilog开发
About some basic DP -- those things about coins (the basic introduction of DP)
【leetcode】22. bracket-generating
2/11 matrix fast power +dp+ bisection
Mysql数据库慢sql抓取与分析
Ipv4中的A 、B、C类网络及子网掩码