LC 387. First Unique Character in a String
题目描述
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode"
return 0. s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
参考答案
class Solution {
public:
int firstUniqChar(string s) {
int count[] = {};
for(char c:s){
count[c-'a']++;
}
for(size_t i = ; i<s.size();i++){
if(count[s[i] - 'a'] == ) return i;
}
return -;
/*
unordered_map<char,int> map;
int min = 0;
for(size_t i = 0 ; i < s.length();++i){
if(map.count(s[i])>0){
map[s[i]] = -1;
}else{
map[s[i]] = i;
}
}
for(size_t i = 0 ; i < s.length();++i){
if(map[s[i]] != -1){
return map[s[i]];
}
}
return -1;*/
}
};
补充说明
第一次自己想出了accepted的答案,虽然和第一名的大佬差了很多,但是感觉还是很兴奋的。
答案中,使用了 count [ c - 'a' ] ,巧妙地将字母表示成为index。
LC 387. First Unique Character in a String的更多相关文章
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- LeetCode 387. First Unique Character in a String
Problem: Given a string, find the first non-repeating character in it and return it's index. If it d ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- 18. leetcode 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [LeetCode&Python] Problem 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [leetcode]387. First Unique Character in a String第一个不重复字母
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- Java [Leetcode 387]First Unique Character in a String
题目描述: Given a string, find the first non-repeating character in it and return it's index. If it does ...
- *387. First Unique Character in a String (linkedhashmap + string) debug manunally instead of using leetcode
The ability to debug and overall thinking need to improve Given a string, find the first non-repeati ...
随机推荐
- MySQL数据分析-(14)表补充:字符集
大家好,我是jacky朱元禄,很高兴继续跟大家学习<MySQL数据分析实战>,本节课程jacky分享的主题是表补充之字符集 在分享课程之前,jacky在跟大家强调一下逻辑的重要性,我们学习 ...
- Java三大特征--多态
1.定义 允许不同类的对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式. 2.存在条件 2.1存在继承关系 2.2子类重写了父类方法 2.3父类类型的变量指向子类对象的 ...
- 二十三、Linux任务计划及周期性任务执行:at、crontab命令
一.概述 未来的某时间点执行一次某任务:at, batch周期性运行某任务:crontab 这两个任务的执行结果:会通过邮件发送给用户 (本地终端用户之间的邮件通知) centos 5,6,7默认开启 ...
- C# 窗体 类似framest 左侧点击右侧显示 左侧菜单右侧显示
首先托一个splitContainer调节大小位置 然后进行再新创建一个窗体名为add 在左侧拖入button按钮 进入代码阶段 更改属性 public Main() { InitializeComp ...
- JAVA基础知识|lambda与stream
lambda与stream是java8中比较重要两个新特性,lambda表达式采用一种简洁的语法定义代码块,允许我们将行为传递到函数中.之前我们想将行为传递到函数中,仅有的选择是使用匿名内部类,现在我 ...
- 黑马vue---46、vue使用过渡类名实现动画
黑马vue---46.vue使用过渡类名实现动画 一.总结 一句话总结: vue动画的过渡类名的时间点中没有设置样式的话就是默认的样式 使用 transition 元素,把 需要被动画控制的元素,包裹 ...
- 安装Chrome扩展程序xpath
最近工作用到xpath,直接从浏览器复制下来路径时常会出错而且长度很长,于是我想到之前用过的一款chrome插件,可以直接编写xpath语句,并实时出现解析出的结果,检验xpath语句是否编写正确.效 ...
- rc.local 注意事項,call python script, file position
如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...
- Android 显示系统:飞思卡尔平台图形界面与GPU硬件加速
图形是Android平台中的一个大主题,包含java/jni图形框架和2d/3d图形引擎(skia.OpenGL-ES.renderscript). 本文档描述了飞思卡尔设备上的一般Android图形 ...
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_17-CMS前端工程创建-单页面应用介绍
查看运行起来的页面的源代码 这个webpck打包生成的文件. 单页面应用的优缺点: 优点: 1.用户操作体验好,用户不用刷新页面,整个交互过程都是通过Ajax来操作. 2.适合前后端分离开发,服务端提 ...