leetcode387
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.
找出第一次出现的不重复的字符
public class Solution {
public int firstUniqChar(String s) {
//97-122
int[] map = new int[123];
int i=0;
int length = s.length();
for(i=0;i<length;i++)
{
map[s.charAt(i)]++;
}
for(i=0;i<length;i++)
{
if(map[s.charAt(i)] == 1)
return i;
}
return -1;
}
}
除了两次for循环,暂时没有想到N时间复杂度的解法。
leetcode387的更多相关文章
- [Swift]LeetCode387. 字符串中的第一个唯一字符 | 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 ...
随机推荐
- 为什么要html语义化?
1.什么是HTML语义化? <基本上都是围绕着几个主要的标签,像标题(H1~H6).列表(li).强调(strong em)等等> 根据内容的结构化(内容语义化),选择合适的标签(代码语义 ...
- Python 修炼3
# 列表 功能方法 *补充(zip zip(list1,list2) 会形成一个[(),()]新的列表list1和list2一一对应得组成一个新的元素以元组最为单位) # 1.修改# li = [1, ...
- HDU 2177 取(2堆)石子游戏 (威佐夫博弈)
题目思路:威佐夫博弈: 当当前局面[a,b]为奇异局时直接输出0 否则: 1.若a==b,输出(0 0): 2.将a,b不停减一,看能否得到奇异局,若有则输出: 3.由于 ak=q*k(q为黄金分割数 ...
- Scope Directive
---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...
- Linux文件系统简介及常用命令
在linux系统中一切皆是文件,下面简要总结了一下linux文件系统中分区类型.文件系统类型以及常用命令. 一.分区类型1.主分区:最多只能有四个2.扩展分区:只能有一个,也可以看做是主分区的一种.即 ...
- freemarker(FTL)常见语法大全
[转载]freemarker(FTL)常见语法大全 FreeMarker的插值有如下两种类型:1,通用插值${expr};2,数字格式化插值:#{expr}或#{expr;format} ${boo ...
- 数据库及SQL优化
一.数据库结构的设计 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实施之前,完备的数据库模型的设计是必须的. ...
- 淘淘商城_day09_课堂笔记
今日大纲 实现购物车 基于Mysql实现读写分离 购物车 需求描述 用户可以在登录状态下将商品添加到购物车 用户可以在未登录状态下将商品添加到购物车 用户可以使用购物车一起结算下单 用户可以查询自己的 ...
- http验证CertificateValidation
private void btnopenURL_Click(object sender, EventArgs e) { string strUrl = txtopenURL.Text.Trim(); ...
- 简单的interface显式和隐式的实现
一,新建接口 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// < ...