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 ...
随机推荐
- Windows 消息以及消息处理算法--线程和消息队列详解
Windows以消息驱动的方式,使得线程能够通过处理消息来响应外界. Windows 为每个需要接受消息和处理消息的线程建立消息队列(包括发送消息队列,登记消息队列,输入消息队列,响应消息队列),其中 ...
- D3DFVF_XYZ和D3DFVF_XYZRHW的区别
D3DFVF_XYZ和D3DFVF_XYZRHW的区别是:1.D3DXYZ默认的坐标系统用户区中心是 (0,0) 而rhw的左上角是 (0,0)2.D3DXYZ默认的非光照的,而RHW默认就是高洛夫的 ...
- Python使用grequests并发发送请求
目录 前言 grequests简单使用 grequests和requests性能对比 异常处理 前言 requests是Python发送接口请求非常好用的一个三方库,由K神编写,简单,方便上手快.但是 ...
- Ubuntu14.04 挂载u盘
插入u盘后, $cat /proc/partitions 发现多了 sdb sdb4 sdb是统称,所以新插入的U盘就是/dev/sdb4 $ls /dev |grep sdb sdb sdb4 查看 ...
- 表单事件集锦-input
最近在写一个手机端提交表单的项目,里面用了不少input标签,因为项目不太忙,所以,想做的完美点,但是遇到了一些问题,比如:页面中的必填项如果有至少一项为空,提交按钮就是不能提交的状态,所以需要对所有 ...
- Web开发中 MTV模式与MVC模式的区别 联系 概念
MTV 与 MVC模式的区别 联系 概念: MTV: 所谓MTV指的就是: M:model (模型),指的是ORM模型. T:template (模板),一般Python都是使用模板渲染的方式来把HT ...
- Cesium中级教程6 - 3D Models 三维模型
3D Models 三维模型 本教程将教您如何通过Primitive API转换.加载和使用Cesium中的三维模型.如果你是Cesium的新用户,可能需要阅读三维模型部分的(空间数据可视化教程)[h ...
- Razor字符串处理
需要注意的是低版本是不支持C# 6语法中的string interpolation的 <label> @if (!string.IsNullOrEmpty(Model.BudgetValu ...
- EasyUI之toolTip
<a class="easyui-tooltip" title="提示框" href="http://www.baidu.com"&g ...
- 用alert打印js对象
用alert查看对象: function writeObj(obj){ var description = ""; for(var i in obj){ var property= ...