当前位置:网站首页>leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
2022-07-02 01:51:00 【重you小垃】
优化1:遍历的时候,从后往前遍历,遍历到a[]和b[]都为true,则return
优化2: 用位运算将空间优化到O(1)
具体思路:
'A’65->90 97->122 122-65+1=58 需要用8字节也就是long long类型来存状态
添加ch:flag |= (ch - ‘A’)
判断大小写字母存在:flag =(flag>>(‘a’-‘A’) & flag)为true
得到ans:‘A’ + (flag的位数:64 - __builtin_clzll(flag)) - 1
class Solution {
public:
string greatestLetter(string s) {
long long flag = 0;
for (auto& ch : s) {
flag |= (1ll << ch - 'A'); //
}
flag &= (flag >> ('a' - 'A'));
return flag ? string(1, 63 - __builtin_clzll(flag) + 'A') : "";
}
};
易错点:1ll 左移
边栏推荐
- 遊戲思考15:全區全服和分區分服的思考
- Regular expression learning notes
- leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
- PR second training
- matlab 使用 resample 完成重采样
- matlab 使用 audioread 、 sound 读取和播放 wav 文件
- I Brief introduction of radio energy transmission technology
- 【C#】使用正则校验内容
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
- new和malloc的区别
猜你喜欢
Matlab uses resample to complete resampling
微信小程序中使用tabBar
如何用一款产品推动「品牌的惊险一跃」?
It's already 30. Can you learn programming from scratch?
【LeetCode 43】236. The nearest common ancestor of binary tree
5g/4g pole gateway_ Smart pole gateway
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
Convolutional neural network (including code and corresponding diagram)
MPLS experiment operation
并发编程的三大核心问题
随机推荐
Six lessons to be learned for the successful implementation of edge coding
KS006基于SSM实现学生成绩管理系统
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
Architecture evolution from MVC to DDD
Android high frequency network interview topic must know and be able to compare Android development environment
Construction and maintenance of business websites [10]
并发编程的三大核心问题
卷積神經網絡(包含代碼與相應圖解)
The concept, function, characteristics, creation and deletion of MySQL constraints
Cross domain? Homology? Understand what is cross domain at once
Redis环境搭建和使用的方法
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
[Floyd] post disaster reconstruction
Construction and maintenance of business websites [15]
Learn about servlets
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
leetcode373. 查找和最小的 K 对数字(中等)
uTools
Electronic Association C language level 1 33, odd even number judgment