当前位置:网站首页>[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;
}
}
边栏推荐
- The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
- Unity中几个重要类
- Hashcode and equals
- P2102 地砖铺设(dfs&贪心)
- When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
- IDEA编译JSP页面生成的class文件路径
- asp. Core is compatible with both JWT authentication and cookies authentication
- 题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
- C mouse event and keyboard event of C (XXVIII)
- Fedora/REHL 安装 semanage
猜你喜欢
[tomato assistant installation]
Custom event of C (31)
MySql数据库root账户无法远程登陆解决办法
颠覆你的认知?get和post请求的本质
Mlapi series - 04 - network variables and network serialization [network synchronization]
C form application of C (27)
Record the pit of NETCORE's memory surge
CertBot 更新证书失败解决
1291_Xshell日志中增加时间戳的功能
Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution
随机推荐
图应用详解
Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
C. The Third Problem(找规律)
绑定在游戏对象上的脚本的执行顺序
Viewing and verifying backup sets using dmrman
Fundamentals of SQL database operation
Detailed explanation of serialization and deserialization
Brief tutorial for soft exam system architecture designer | general catalog
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
TCP/IP协议里面的网关地址和ip地址有什么区别?
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
Global and Chinese markets for MRI safe implants 2022-2028: technology, participants, trends, market size and share Research Report
One question per day (Mathematics)
MySql數據庫root賬戶無法遠程登陸解决辦法
MySql数据库root账户无法远程登陆解决办法
How to execute an SQL statement in MySQL
In depth MySQL transactions, stored procedures and triggers
深入浅出node模板解析错误escape is not a function
2/10 parallel search set +bfs+dfs+ shortest path +spfa queue optimization