题目描述

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的更多相关文章

  1. LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)

    题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...

  2. 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 ...

  3. 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 ...

  4. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. *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 ...

随机推荐

  1. 通过zabbix来监控树莓派

    安装zabbix-agent(4.0版本) 配置zabbix-agent(使用主动模式) 使用zabbix-sender(主动推送自定义数据) 以下 执行命令和相关配置文件: wget https:/ ...

  2. 如何确定哪个SMB客户端/会话在Server 2008R2 Windows文件服务器上打开了特定文件?

    参考: http://www.kbase101.com/question/54969.html NetworkOpenedFiles v1.25  https://www.nirsoft.net/ut ...

  3. 讨论SQLite数据库损坏与修复

      版权声明:博客将逐步迁移到 http://cwqqq.com https://blog.csdn.net/cwqcwk1/article/details/45541409 昨晚,朋友和我反馈SQL ...

  4. kotlin中对象表达式

    在kotlin中,也有类似的功能,但不是匿名类,而是对象,需要使用object关键字,对象要继承的列需要与object之间的冒号(:)分隔. fun main(arg: Array<String ...

  5. Sql题目精选练习

    1.每日经典sql 1.1.1 根据三张关系表查询雇员中工资最高的雇员的员工姓名.工资和部门号. salary(工资表) employee(员工表) department(部门表) Sql语句: SE ...

  6. 在Mac 搭建robotframework 环境

    折腾来一下午,遇到了好多坑 坑 1.不要用pip 下载wxpython 2.不要用mac自带的python 3.不要自己下载wxpython 步骤: 1. 安装homebrew, /usr/bin/r ...

  7. 图解 HTTP 笔记(三)—— HTTP 报文内的 HTTP 信息

    本章主要讲解请求和响应是如何运作的 一.HTTP 报文 用于 HTTP 协议交互的信息被称为 HTTP 报文,客户端的 HTTP 报文叫做请求报文,服务器端的叫做响应报文. HTTP 报文大致可分为报 ...

  8. Consul 随记

    consul 包含多个组件,但是作为一个整体对外提供服务发现和服务配置工具: 提供的关键特性有: 服务发现:发现的是服务对应的IP地址和PORT端口号 健康检查:检查服务节点状态 Key/Value存 ...

  9. Entity Framework链接数据库设置

    本人不才,学习EntityFramwork同时做个记录供大家参考.不多说,直接上步骤 1.在WebConfig中添加如下代码段 <connectionStrings> <add na ...

  10. centos安装软件

    rpm指令, 该指令安装文件后缀.rpm的可执行程序 yum指令 安装软件源代码,后缀为 .tar.gz(用gzip压缩过的tar包) rpm rpm软件包格式为 (一)查询系统装已经安装的软件信息 ...