[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 exist, return -1.
Examples:
s = "leetcode"
return 0. s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
思路
1. use int array to simplify a hashmap
2. one pass to mark each char's occurrence
3. second pass to check 1st index whose char's occurrence == 1
代码
class Solution {
public int firstUniqChar(String s) {
int map [] = new int[256];
for(int i = 0; i < s.length(); i ++)
map [s.charAt(i) ] ++;
for(int i = 0; i < s.length(); i ++)
if(map [s.charAt(i) ] == 1)
return i;
return -1;
}
}
[leetcode]387. First Unique Character in a String第一个不重复字母的更多相关文章
- [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 387. First Unique Character in a String (字符串中的第一个唯一字符)
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...
- 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 ...
- 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 ...
- 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 ...
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- 387 First Unique Character in a String 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...
- [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 ...
- *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 ...
随机推荐
- React Native,flexbox布局
Flexbox布局 flex:使组件在可利用的空间内动态地扩张或收缩.flex:1会使组件撑满空间.当有多个组件都指定了flex的值,那么谁的flex值大谁占得空间就大,占得大小的比例就是flex值的 ...
- python oracle 查询返回字典
from: https://sourceforge.net/p/cx-oracle/mailman/message/27145597/ I'd do it with a "row facto ...
- C++复习:类和对象
类和对象 基本概念 1)类.对象.成员变量.成员函数 2)面向对象三大概念 封装.继承.多态 3)编程实践 类的定义和对象的定义,对象的使用 求圆形的面积 定义Teacher类 ...
- LeetCode OJ 22. Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 树莓派上的软件安装和卸载命令汇总 [ZT]
转自:http://www.eeboard.com/bbs/thread-40823-1-1.html基础命令 安装软件 apt-get install softname1 softname2 sof ...
- 一个关于EasyUI超恶心的BUG。。。Cannot read property 'options' of undefined
控制台Console抛出的异常: jquery.easyui.min.js:9148 Uncaught TypeError: Cannot read property 'options' of und ...
- oracle理解和导入导出
搞过sql server的程序员很难理解oracle的表空间.我在这里简单说一下吧, oracle中的表空间就相当于sql server中的实例,用户就相当于sql server中的库. 所以在ora ...
- MySQL 各种主流 SQLServer 迁移到 MySQL 工具对比
原地址:https://www.cnblogs.com/overblue/p/5796887.html Mss2sql:这个比较好用
- 吴裕雄 python 机器学习-DMT(2)
import matplotlib.pyplot as plt decisionNode = dict(boxstyle="sawtooth", fc="0.8" ...
- django中使用mysql数据库的事务
django中怎么使用mysql数据库的事务 Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起 ...