当前位置:网站首页>[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;
}
}
边栏推荐
- How can programmers resist the "three poisons" of "greed, anger and ignorance"?
- Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
- Ipv4中的A 、B、C类网络及子网掩码
- 查询mysql数据库中各表记录数大小
- Python book learning notes - Chapter 09 section 01 create and use classes
- 20、 EEPROM memory (AT24C02) (similar to AD)
- 729. 我的日程安排表 I(set or 动态开点线段树)
- Custom event of C (31)
- 自动化测试的好处
- Global and Chinese market of aircraft anti icing and rain protection systems 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
ESP32_ FreeRTOS_ Arduino_ 1_ Create task
Record the pit of NETCORE's memory surge
记一次excel XXE漏洞
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
Chinese brand hybrid technology: there is no best technical route, only better products
Thread sleep, thread sleep application scenarios
Slow SQL fetching and analysis of MySQL database
Maxay paper latex template description
Stable Huawei micro certification, stable Huawei cloud database service practice
How many of the 10 most common examples of istio traffic management do you know?
随机推荐
Leetcode32 longest valid bracket (dynamic programming difficult problem)
VPP性能测试
WPF effect Article 191 box selection listbox
Recommendation system (IX) PNN model (product based neural networks)
Python book learning notes - Chapter 09 section 01 create and use classes
Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
Hashcode and equals
MySQL master-slave replication
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
Data processing methods - smote series and adasyn
Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
Python book learning notes - Chapter 09 section 01 create and use classes
2328. 网格图中递增路径的数目(记忆化搜索)
Path of class file generated by idea compiling JSP page
Brief tutorial for soft exam system architecture designer | general catalog
Fundamentals of SQL database operation
2/12 didn't learn anything
电脑钉钉怎么调整声音
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)