[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 ...
随机推荐
- python文件处理指针的移动
控制文件指针移动 with open('a.txt',mode='rt',encoding='utf-8')as f: res=f.read(4) print(res) 强调:只有t模式下read(n ...
- 3:while、for 循环语句
循环就是重复的做一件事情.python 中的循环语句有 while 和 for. while 循环 while 循环必须得有一个计数器,否则会变成一个死循环. # 例如这段代码,这段程序运行之后会一直 ...
- 发现一个好办法-有问题可以到UNITY论坛搜索
特别专业的问题,较新技术,可以到UNITY论坛搜索或发问,那里,或许会有UNITY的官方技术支持回答 https://forum.unity.com/threads/remote-deep-profi ...
- Angular2学习笔记
Angular2 这里 Angular2 是指采用 TypeScript 语言的 Angular 2.0及以上版本.与采用 JavaScript 语言的 AngularJS 相比,Angular2 不 ...
- vscode项目配置 vue-loader-webpack
使用vsCode进行项目配置 一.准备工作 1.下载Visual Studio Code 下载地址 2.打开vscode,根据自己需求下载相应插件,可以提高工作效率. 点击下角选项(我作了红框标记), ...
- 记Dagger2使用过程中的一个BUG--compileGoogleDebugJavaWithJavac
项目编译可以通过,不过没有生成Dagger2的类,导致无法运行项目.. 错误提示 Error:(14, 41) 错误: 找不到符号 符号: 类 DaggerAppComponent 位置: 程序包 c ...
- WinForm textbox 全选
原地址:忘了 textBox1.KeyPress += anyTextBox_KeyPress; private void anyTextBox_KeyPress(object sender, Sys ...
- /src/log4j.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration S ...
- Illegal access: this web application instance has been stopped already. could not load **
启动tomcat的时候会报这样的错误: Illegal access: this web application instance has been stopped already. could n ...
- lcd 显示屏
1.lcd 接口信号: VSYNC : 一帧新数据的开始信号 HSYNC :一行新数据的开始信号 VCLK :像素的同步信号 VD[0:23] :传递数据的信号线 2. LCD 的显示原理 ( ...