当前位置:网站首页>The first character of leetcode sword offer that only appears once (12)
The first character of leetcode sword offer that only appears once (12)
2022-07-03 14:59:00 【& eternal Galaxy &】
Title Description
character string s Find the first character that appears only once . without , Return a single space . s Contains only lowercase letters .
Example 1:
Input :s = "abaccdeff" Output :'b'
Example 2:
Input :s = "" Output :' '
python3 Realization
Coding ideas : Slicing method (python alone possess ), It can also be used. hash map
class Solution:
def firstUniqChar(self, s: str) -> str:
rs = " "
for i in range(len(s)):
if s[i] not in s[:i] and s[i] not in s[i+1:]:
return s[i]
return rs
c++ Realization
Encoding idea : Storage frequency using hash table
class Solution {
public:
char firstUniqChar(string s) {
unordered_map<int, int> freq;
for(char c:s){
++freq[c];
}
for(int i=0; i<s.size();i++){
if(freq[s[i]] == 1)
return s[i];
}
return ' ';
}
};
边栏推荐
- Zzuli:1042 sum of sequence 3
- 【微信小程序】WXSS 模板样式
- Zzuli:1046 product of odd numbers
- Implement Gobang with C language
- Output student grades
- C language DUP function
- Vs+qt application development, set software icon icon
- QT program font becomes larger on computers with different resolutions, overflowing controls
- Talking about part of data storage in C language
- Yolov5系列(一)——網絡可視化工具netron
猜你喜欢
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
Byte practice surface longitude
[ue4] Niagara's indirect draw
How to color ordinary landscape photos, PS tutorial
Bucket sorting in C language
4-24--4-28
Remote server background hangs nohup
QT - draw something else
【微信小程序】WXSS 模板样式
Tencent internship interview sorting
随机推荐
NOI OPENJUDGE 1.5(23)
Tensor ellipsis (three points) slice
Zzuli:1044 failure rate
Global and Chinese market of trimethylamine 2022-2028: Research Report on technology, participants, trends, market size and share
Analysis of gene family characteristics - chromosome location analysis
[graphics] real shading in Unreal Engine 4
[opengl] pre bake using computational shaders
Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
C language dup2 function
远程服务器后台挂起 nohup
7-3 rental (20 points)
什么是embedding(把物体编码为一个低维稠密向量),pytorch中nn.Embedding原理及使用
mmdetection 学习率与batch_size关系
Yolov5进阶之九 目标追踪实例1
How does vs+qt set the software version copyright, obtain the software version and display the version number?
Global and Chinese market of solder bars 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of Bus HVAC systems 2022-2028: Research Report on technology, participants, trends, market size and share
Zzuli:1046 product of odd numbers
Tensor 省略号(三个点)切片
Introduction to opengl4.0 tutorial computing shaders