解题思路也比较简单,你需要使用一个中间变量来存储,首先还是需要将进行处理的字符串转换为 char 的数组...
开个26个数的数组,然后先对字符串过一遍,统计每个字母出现的次数,然后从头再国一遍,第一个字母数为1的即为首先出现并且只出现一次的字母。 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 publicclassSolution { publicintfirstUniqChar(String s) { int[] a =newint[26]; for(inti =0; i < s.length...
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 给一个字符串,返回第一个不重复的字符的index,要是没有的话就返回-1 //第一次做,感觉巨简单,设一个set,一旦contains,就用striing.indexOf()---还能用上不熟悉的函数,简直完...
Java Code: importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){// Test the first_Uniq_Char function and print the resultStrings="wresource";System.out.println("Original String: "+s);System.out.println("First unique character of the above: "+first_Uniq_Char(s));}public...
利用Java 字符串集成操作函数解题,很巧妙,效率也很高。 其中: indexOf(): 返回该元素第一次出现的索引,没有则返回 -1 lastIndex(): 返回该元素最后一次出现的索引,没有则返回 -1 class Solution { public int firstUniqChar(String s) { int res = s.length(); for (int i = 'a'; i <= '...
/** * Return the first Uniq Char String without using Map * @param data * @return */ private String firstUniqCharString(String data) { // NULL CHECK if (data.equals("")) { return ""; } char[] strArray = data.toCharArray(); String retStr = ""; if (data.length() == 1) {...
class Solution { public int firstUniqChar(String s) { int ans = Integer.MAX_VALUE; for(char c='a'; c<='z';c++){ int index = s.indexOf(c); if(index!=-1&&index==s.lastIndexOf(c)){ ans = Math.min(ans,index); } } return ans==Integer.MAX_VALUE?-1:ans; } } 题目信息 ...
Java AI检测代码解析 class Solution { public int firstUniqChar(String s) { Map<Character, Integer> table = new HashMap<>(); for (int i = 0; i < s.length(); i++) { table.put(s.charAt(i), table.getOrDefault(s.charAt(i), 0) + 1); ...
first-unique-character-in-a-string https://leetcode.com/problems/first-unique-character-in-a-string/ public class Solution { public int firstUniqChar(String s) { int[] index = new int[26]; for (int i=0; i 0) { index[ch] = -...
Java的并发包提供了三个常用的并发队列实现,分别是:ArrayBlockingQueue、ConcurrentLinkedQueue 和 Linked...