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't exist, return -1.
Examples:
s = "leetcode"
return 0.
s = "loveleetcode",
return 2.
Java算法实现
public class Solution {
public int firstUniqChar(String s) {
TreeSet<Character>set=new TreeSet<Character>();
Map<Character,Integer>unique=new HashMap<Character, Integer>();
for(int i=0;i<s.length();i++){
Character ch=s.charAt(i);
if(!set.contains(ch)){ //利用Set中每个元素都是唯一的这个属性,记录元素是否出现过
set.add(ch);
unique.put(ch, i);
}
else{
if(unique.containsKey(ch)){
unique.remove(ch);
}
}
}
int index=-1;
Object[] values=unique.values().toArray();
if(values.length>0){
index=(int) values[0];
for(int i=1;i<values.length;i++){
if((int)values[i]<index){
index=(int)values[i];
}
}
}
return index;
}
}
Leetcode算法比赛----First Unique Character in a String的更多相关文章
- LeetCode算法题-First Unique Character in a String(Java实现)
这是悦乐书的第213次更新,第226篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第81题(顺位题号是387).给定一个字符串,找到它中的第一个非重复字符并返回它的索引. ...
- 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 does ...
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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算法题-Number of Lines To Write String(Java实现)
这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...
随机推荐
- (C/C++) STL 標頭檔 algorithm (一)
因為解題需求認識了一些STL相關funciton. 分別是 max_element (ForwardIterator first, ForwardIterator last); min_element ...
- vue环境搭建简介
简单整理下vue的安装的新建项目 安装node.js和npm 参考其他教程 安装vue npm install vue 安装脚手架 vue-cli npm install --global vue-c ...
- 3.1)DFM-塑胶件设计总章
本章目的:各种塑胶工艺了解,DFM-塑胶件的设计准则是依据哪种工艺. 1.塑胶概念 塑胶的定义(美国塑料工业协会): 塑胶主要由碳.氧.氢和氮及其他有机或无机元素所构成,成品为固体,在制造过程中是熔融 ...
- UVA_11020 Efficient Solutions 【平衡二叉搜索树set用法】
一.题面 UVA11020 二.分析 最近脑子有点不好使吧,这题还想了很久. 对于给定的两个值要满足题面中的条件,那么我们可以把这两个值转化到平面中的坐标去理解. 首先,需要考虑的是维护的所有点其实是 ...
- 基于Allwinner的Audio子系统分析(Android-5.1)
前言 一直想总结下Audio子系统的博客,但是各种原因(主要还是自己懒>_<),一直拖到现在才开始重新整理,期间看过H8(Android-4.4),T3(Android-4.4),A64( ...
- sql geography类型(地理坐标) 赋值
sql 语句赋值 update [lishui].[dbo].[t_NearbyService] ,, ) ,[locationbaidu]=geography::Point(,, ) where [ ...
- (转)zabbix之生产案例
原文: https://www.abcdocker.com/abcdocker/category/zabbix/ 原文: https://chegva.com/1170.html
- python-Event事件处理进程同步
#!/usr/bin/python from multiprocessing import Process,Event import os,time def A(e): print "blo ...
- 事务实现,redo,undo,锁
事务(Transaction)是数据库区别于文件系统的重要特性之一.在文件系统中,如果你正在写文件,但是操作系统突然崩溃了,这个文件就很有可能被破坏.当然,有一些机制可以把文件恢复到某个时间点.不过, ...
- ZOJ 2971 Give Me the Number
Give Me the Number Numbers in English are written down in the following way (only numbers less than ...