问题描述 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 first…
这是悦乐书的第213次更新,第226篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第81题(顺位题号是387).给定一个字符串,找到它中的第一个非重复字符并返回它的索引. 如果它不存在,则返回-1.例如: 输入:"leetcode" 输出:0 输入:"loveleetcode", 输出:2 注意:您可以假设该字符串仅包含小写字母. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java…
题目描述: 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 low…
Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/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 =…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/first-unique-character-in-a-string/description/ 题目描述 Given a string, find the first non-repeating character in it and return it's index. I…
-------------------------------------------------- 最开始的想法是统计每个字符的出现次数和位置,如下: AC代码: public class Solution { public int firstUniqChar(String s) { Count c[]=new Count[26]; for(int i=0;i<s.length();i++){ int index=s.charAt(i)-'a'; if(c[index]==null) c[in…
387. First Unique Character in a String Easy 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…
Description Find the first unique character in a given string. You can assume that there is at least one unique character in the string. Example For "abaccdeff", return 'b'. 解题:返回仅出现一次,并且排在最前面的那个元素,而不是返回第一个不同的元素,一开始理解错了.如果仅仅对于ASCII中的256个字符,可以用数组…
原题链接在这里:https://leetcode.com/problems/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 = "lovele…
这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为100个单位,如果写一个字母会导致该行的宽度超过100个单位,则会写入下一行.给出一个数组宽度,一个数组,其中widths[0]是'a'的宽度,widths[1]是'b'的宽度,widths[25]是'z'的宽度. 现在回答两个问题:S中至少有一个字符有多少行,最后一行使用的宽度是多少?将答案作为长…
这是悦乐书的第228次更新,第240篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第95题(顺位题号是438).给定一个字符串s和一个非空字符串p,找到s中p的字谜的所有起始索引.字符串仅由小写英文字母组成,字符串s和p的长度不会大于20,100.输出顺序无关紧要.例如: 输入:s:"cbaebabacd" p:"abc" 输出:[0,6] 说明: 起始索引等于0的子字符串是"cba",它是"abc&quo…
这是悦乐书的第226次更新,第239篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第93题(顺位题号是434).计算字符串中的段数,其中段定义为非空格字符的连续序列.请注意,该字符串不包含任何不可打印的字符.例如: 输入:"Hello, my name is John" 输出:5 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 使用正则表达式,将其拆分成字符串数组…
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: 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: Y…
题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作value. 遍历string,如果这个 char 没有在 map 里,说明第一次出现,存入 char,index:                    如果这个 char 已经在 map 里,说明不是第一次出现,而且我们不在乎这种情况,更新 char, -1. 遍历hashmap,把最小的 inde…
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3939 访问. 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. s = "leetcode",返回 0. s = "loveleetcode",返回 2. 注意事项:您可以假定该字符串只包含小写字母. Given a string, find the first non-repeating ch…
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…
Problem: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Example: s = "leetcode" return 0. s = "loveleetcode", return 2. 本题的第一想法是开一个字母表计数器,遍历string s中的元素,并利用计数器对每一个字母计…
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 lowerca…
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…
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…
题目描述: 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. 解题思路: 开个26个数的数组,然后先对字符串过一遍,统计每个字母出现的次数,然后从头再国一遍,…
The ability to debug and overall thinking need to improve 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…
题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字…
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…
问题描述 Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. Java算法实现 public cl…
#-*- coding: UTF-8 -*- class Solution(object):    def firstUniqChar(self, s):        s=s.lower()        sList=list(s)        numCdic={}        for c in s:            numCdic[c]=numCdic[c]+1 if c in numCdic else 1        for i in range(len(sList)):   …
问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-direc…
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. 分析:找出字符串中第一次出现且不重复的字符,返回其字符的下标,如果不存在就返回-1. 用hashMap进行求…
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…
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…