leetcode笔记11 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.
my answer:
主要思路:
首先把字符串变成字母列表,然后遍历列表字母建立一个计数字典和索引字典,再次遍历字母列表查找第一个出现的计数为1的字母的索引
出错点:
1 得到计数字典uni和索引字典ind之后,直接在uni里查找值为1的字母,这样出错的原因是,形成计数字典之后,字母顺序已经改变,所得字母并不是第一个出现的
2 忽略了不存在的情况,对于不存在的情况,即所有计数均大于1,这里使用的判断方法是对大于1的计数,如果最后得数等于list的长度则说明不存在
3 做逻辑运算时总是写成=,应该是双等号==
知识点:
for index,t in enumerate(list) 可同时遍历list 的值和索引
for k,v in uni.items() 可同时遍历字典uni的key 与 value
leetcode笔记11 First Unique Character in a String的更多相关文章
- Leetcode算法比赛----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 ...
- 【LeetCode】387. First Unique Character in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode算法题-First Unique Character in a String(Java实现)
这是悦乐书的第213次更新,第226篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第81题(顺位题号是387).给定一个字符串,找到它中的第一个非重复字符并返回它的索引. ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- LeetCode之387. First Unique Character in a String
-------------------------------------------------- 最开始的想法是统计每个字符的出现次数和位置,如下: AC代码: public class Solu ...
- LeetCode_387. First Unique Character in a String
387. First Unique Character in a String Easy Given a string, find the first non-repeating character ...
- 209. First Unique Character in a String
Description Find the first unique character in a given string. You can assume that there is at least ...
- LeetCode First Unique Character in a String
原题链接在这里:https://leetcode.com/problems/first-unique-character-in-a-string/ 题目: Given a string, find t ...
- LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
随机推荐
- UVa 1218 - Perfect Service(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- ThinkPHP中前台输出变量
1. foreach <foreach name="list" item="vo" > {$key}|{$vo.id}:{$vo.name} & ...
- 跟着大神学Mongo,Mongodb主从复制本机简单操作总结
原文链接:http://www.cnblogs.com/huangxincheng/archive/2012/03/04/2379755.html 本机安装MongoDB不在介绍,本文Mongo小菜鸟 ...
- init/loadView/viewDidLoad/initWithNibName/awakeFromNib/initWithCoder的用法
init/loadView/viewDidLoad/viewDidUnload 这么细节的东西想来大家都不在意,平时也不会去关系,但是在面试时却常常被提到,所以了解viewController的生命周 ...
- EF Core中关于System.Linq.Dynamic.Core的使用(转载)
项目中经常用到组合条件查询,根据用户配置的查询条件进行搜索,拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦.有个System.Linq.Dynamic.Core用起来比 ...
- ES6中常用新特性讲解
1.不一样的变量声明:const和let ES6推荐使用let声明局部变量,相比之前的var(无论声明在何处,都会被视为声明在函数的最顶部) let和var声明的区别: var x = '全局变量'; ...
- ffmpeg 简单使用总结
FFMPEG 生成指定长度的空白音频: ffmpeg -f lavfi -i aevalsrc=0 -t seconds -q:a 9 -acodec libmp3lame out.mp3 FFMPE ...
- MySQL学习之用户管理
用户权限管理 用户权限管理:在不同的项目中给不同的角色(开发者)不同的操作权限,为了保证数据库数据的安全. 简单点说:有的用户可以访问并修改这个数据,而有些用户只能去查看数据,而不能修改数据.就如同博 ...
- HTML | video的封面平铺方法
<video style="object-fit:fill;"></video>
- 正则表达式-Regular expression学习笔记
正则表达式 正则表达式(Regular expression)是一种符号表示法,被用来识别文本模式. 最近在学习正则表达式,今天整理一下其中的一些知识点 grep - 打印匹配行 grep 是个很强大 ...