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的字母从左到右写成行.每行最大宽度为 ...
随机推荐
- [Alpha]Scrum Meeting#9
github 本次会议项目由PM召开,时间为4月11日晚上10点30分 时长10分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写每日例会报告 撰写每日例会报告撰写并整理任务分配博客 SiMrua ...
- [ZJOI2019]Minimax搜索
先求出根节点的权值\(w\).根据套路,我们对于每个\(k\),计算\(w(s)\leq k\)的方案数,差分得到答案.为了方便,接下来考虑计算概率而不是方案数. 可以发现,对于一个给定的有解的子集, ...
- 第五届CCPC河南省赛参赛有感
10点开始,不过两次推迟了10分钟,也就是10点20开始.然后真的开始了,我还以为还会推迟10分钟. 比赛从密码输错开始,到瞎改代码疯狂提交结束. 输错密码,耽误了一点时间.点开签到题<文本修改 ...
- 【实战】Apache Shiro 1.2.4 RCE
poc: #coding: utf-8 import os import re import sys import base64 import uuid import subprocess impor ...
- (转)MySQL多源复制
原文:https://dev.mysql.com/doc/refman/5.7/en/replication-multi-source.html MySQL多源复制概述 MySQL多源复制使复制从接受 ...
- Attribute基本介绍
一.基础知识点 1.什么是Attribute? MSDN:公共语言运行时允许你添加类似关键字的说明,叫做Attribute,它可以对程序中的元素进行标注,如类型.字段.方法和属性等.Attribute ...
- springboot-22-自定义starter
先说下springboot的运行原理 springboot最主要的配置 是 @SpringBootApplication 然后这里面 @EnableAutoCOnfiguration 最为重要, 继续 ...
- sqlplus 调试存储过程
SQLPLUS里测试带返回参数的存储过程过程名p_test入参 aa varchar2出参 bb sys_refcursor 在SQLPLUS里如何将sys_refcursor 这个结果集获取出来呢 ...
- Elasticsearch集群和索引常用命令
https://www.cnblogs.com/pilihaotian/p/5846173.html REST API用途 ES提供了很多全面的API,大致可以分成如下几种: 1 检查集群.节点.索引 ...
- lazy初始化和线程安全的单例模式
1.双检锁/双重校验锁(DCL,即 double-checked locking) JDK 版本:JDK1.5 起 是否 Lazy 初始化:是 是否多线程安全:是 实现难度:较复杂 描述:这种方式采用 ...