方法一:利用一个字典和一个列表解决,字典记录每个字符出现的次数,列表记录出现过的字符,从前到后遍历这个string 遇到新字符(字典中没有的key),在字典中添加key为该字符,value为1,列表中append该字符 遇到出现过的字符(字典中有该key),字典中该key的value加1 然后,遍历列表,打印出value为1的字符即可 代码如下 1 ...
Given a string s, return the first non-repeating character in it and return its index. If it does not exist, return -1. 中文 针对给定的一个字符串 s,你需要写一个算法,返回给定字符串中不重复字符的位置(index),如果所有的字符在给定的字符串中都有重复的话,那么你应该返回 -1。 样例 下面给出...
Given a string "teeter", the first non repeating character would be 'r'. in "toothless", it would be 'h'. I'm wondering about the most efficient way to get this done? One option is to use a hash table, with the characters in the string as keys, and frequencies of each character...
lastIndexOf(c)){ ans = Math.min(ans,index); } } return ans==Integer.MAX_VALUE?-1:ans; } } 题目信息 Given a strings,find the first non-repeating character in it and return its index. If it does not exist, 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. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note:You may assume the string contain only lowercase letters. ...
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”,...
You are given a string str consisting of lowercase Latin letters. Find the first non-repeating character in str. Note: You have to traverse the string only once. See original problem statement here Tets Case: Input: prepbytes Output: 1 Explanation: In the string 'prepbytes', we start trave...
英文 Given a string s, return the first non-repeating character in it and return its index. If...
Write a Java program to find the first non-repeating character in a string.Visual Presentation:Sample Solution:Java Code:// Importing necessary Java utilities. import java.util.*; // Define a class named Main. public class Main { // Main method to execute the program. public static void ...
原题链接在这里: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 = "loveleetcode", return 2. 题解: ...