avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

2309. The best English letters with both appropriates and lowercases One question daily

  2024-01-01        
字数统计: 424字   |   阅读时长: 2min

2023-01-27 (3).png
2309. The best English letters with both appropriates and lowercases

Thought:

  1. Hash table:The first thing to think of the question is to solve it with a dictionary。But find that you can directly traverse the alphabet,fromZStart looking for,See if the applause exists,Just return directly。
    9f9081c1d70b0d0e0f736c400e4ddb3.png
    Running time exceeds99.9%
  2. Bit operation:We can use two integers mask1 and mask2 Record string separately s 中出现的小写字母and大写字母,in mask1 First i
    Position representation i Does a lowercase letter appear,and mask2 First i Position representation i Whether an uppercase letter appears。

Then we will mask1 and mask2 Perform and calculate,The results obtained mask First i Position representation i Whether the lethals of the letter appear at the same time。

As long as you get mask The highest level of binary representation 1 s position,Convert it to the corresponding capital letter。If all binary positions are not 1,Explain that there is no letter that appears at the same time,,Return to an empty string。

author:ylb
Link:https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/solutions/2077636/by-lcbin-zbg0/
source:Deduction(LeetCode)
著作权归author所有。商业转载请联系author获得授权,Non -commercial reprint Please indicate the source。

Code:

Traversing the alphabet
1
2
3
4
5
6
class Solution:
def greatestLetter(self, s: str) -> str:
for i in range(90, 64, -1):
if chr(i) in s and chr(i+32) in s:
return chr(i)
return ""
Hash table
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string greatestLetter(string s) {
unordered_set<char> strin(s.begin(),s.end());
for(char c = 'Z'; c >= 'A'; --c){
if(strin.count(c) && strin.count(char(c+32))){
return string(1,c);
}
}
return "";
}
};
Bit operation
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def greatestLetter(self, s: str) -> str:
# We can use two integers mask1 and mask2
# Record string separately s 中出现的小写字母and大写字母,in mask1 First i Position representation i Does a lowercase letter appear,
# and mask2 First i Position representation i Whether an uppercase letter appears。
mask1 = mask2 = 0
for i in s:
if i.islower():
mask1 |= 1 << (ord(i) - ord("a"))
else:
mask2 |= 1 << (ord(i) - ord("A"))
mask = mask1 & mask2
return chr(mask.bit_length() - 1 + ord("A")) if mask else ""
  • Python
  • answer
  • Hash table
  • One question daily
  • C++
  • Bit operation

扫一扫,分享到微信

微信分享二维码
One question daily 2299. Code inspection device II
2315. Statistical star number One question daily
目录
  1. 1. Thought:
  2. 2. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号