在一个长为
字符串中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)
数据范围:
,且字符串只有字母组成。
要求:空间复杂度
,时间复杂度
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @return int整型
*/
public int FirstNotRepeatingChar (String str) {
// write code here
if(str==null||str.length()==0)
return -1;
if(str.length()==1)
return 0;
for(int i=0;i<str.length();i++){
if(str.replaceAll(str.charAt(i)+"","").length()==str.length()-1){
return i;
}
}
return -1;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @return int整型
*/
public int FirstNotRepeatingChar (String str) {
// write code here
int[] a=new int[str.length()];
ArrayList<Character> li=new ArrayList<>();
for(int i=0;i<str.length();i++){
if(!li.contains(str.charAt(i))){
li.add(str.charAt(i));
a[i]=1;
}
else{
a[str.indexOf(str.charAt(i))]++;
}
}
for(int i=0;i<a.length;i++){
if(a[i]==1)return i;
}
return -1;
}
} public int FirstNotRepeatingChar (String str) {
// write code here
//存放出现的字符个数
int[] chars = new int[58];
//存放第一次出现时的下标
int[] charsStartIndex = new int[58];
for(int j = 0; j < str.length(); j ++){
if(charsStartIndex[str.charAt(j) - 'A'] == 0){
charsStartIndex[str.charAt(j) - 'A'] = j;
}
chars[str.charAt(j) - 'A']++;
}
//找到第一个只出现一次的字符下标
int minIndex = Integer.MAX_VALUE;
for(int i = 0; i < 58; i++){
if(chars[i] == 1){
minIndex = Math.min(charsStartIndex[i], minIndex);
}
}
return minIndex == Integer.MAX_VALUE ? -1 : minIndex;
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @return int整型
*/
public int FirstNotRepeatingChar (String s) {
int[] count = new int[52];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 'a' && c <= 'z') {
count[c - 'a']++;
} else if (c >= 'A' && c <= 'Z') {
count[c - 'A' + 26]++;
}
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c >= 'a' && c <= 'z' && count[c - 'a'] == 1) || (c >= 'A' && c <= 'Z' && count[c - 'A' + 26] == 1)) {
return i;
}
}
return -1;
}
} public int FirstNotRepeatingChar (String str) {
// write code here
int [] newStr = new int[52]; // 创建一个数组,下标与字符一一对应
Arrays.fill(newStr, 0);
// 根据使用数组设计简易的哈希表(书中描述的方法)
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) <= 'Z') {
int index = str.charAt(i) - 'A'; // 大写字母的对应的索引值
newStr[index] += 1; // 统计字符出现次数
} else {
int index = str.charAt(i) - 'a' + 26; // 小写字母对应的索引值
newStr[index] += 1; // 统计字符出现次数
}
}
for (int i = 0; i < str.length(); i++) {
// 找到对应字符的索引,值是否为1
if (str.charAt(i) <= 'Z' && newStr[str.charAt(i) - 'A'] == 1) {
return i;
} else if(str.charAt(i) > 'Z' && newStr[str.charAt(i) - 'a' + 26] == 1){
return i;
}
}
return -1;
} public int FirstNotRepeatingChar (String str) {
// write code here
HashMap<Character ,Integer> map=new HashMap<>();
for(int i=0;i<str.length();i++){
map.put(str.charAt(i) ,map.getOrDefault(str.charAt(i),0)+1);
}
for(int i=0;i<str.length();i++){
if(map.get(str.charAt(i))==1){
return i;
}
}
return -1;
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @return int整型
*/
public int FirstNotRepeatingChar (String str) {
int index=-1;
if(str==null || str.length()<=0){
return index;
}else{
char res = '\0';
Map<Character, Integer> map = new LinkedHashMap<>();
for(Character i:str.toCharArray()){
map.put(i, map.getOrDefault(i,0)+1);
}
for(Map.Entry<Character, Integer> entry:map.entrySet()){
if(entry.getValue()==1){
res=entry.getKey();
break;
}
}
for(int i=0; i<str.length(); i++){
if(res==str.charAt(i)){
index=i;
}
}
}
return index;
}
} public int FirstNotRepeatingChar (String str) {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>(str.length());
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (map.containsKey(c)) {
map.remove(c);
} else {
map.put(c, i);
}
}
Optional<Map.Entry<Character, Integer>> first = map.entrySet().stream().findFirst();
if (!first.isPresent()) {
return -1;
}
return first.get().getValue();
} import java.util.List;
import java.util.stream.Collectors;
import java.util.HashMap;
public class Solution {
public int FirstNotRepeatingChar(String str) {
List<Character> collect = str.chars().mapToObj(c -> (char) c).collect(
Collectors.toList());
for (int i = 0; i < collect.size(); i++) {
int indexOne = collect.indexOf(Character.valueOf(collect.get(i)));
int lastIndexOf = collect.lastIndexOf(Character.valueOf(collect.get(i)));
if (indexOne == lastIndexOf) {
return indexOne;
}
}
return -1;
}
} public class Solution {
public int FirstNotRepeatingChar(String str) {
for(int i = 0; i < str.length(); i++){
char x = str.charAt(i);
String c = String.valueOf(x);
String ss = str.substring(0,i) + str.substring(i+1);
if(!ss.contains(c)){
return i;
}
}
return -1;
}
} public class Solution {
public int FirstNotRepeatingChar(String str) {
for (int i = 0; i < str.length(); i ++) {
int j = 0;
for (; j < str.length(); j++) {
if(i == j) continue;
if (str.charAt(i) == str.charAt(j)) break;
}
if (j == str.length()) return i;
}
return -1;
}
} public int FirstNotRepeatingChar(String str) {
if ( str.isEmpty() ) {
return -1;
}
HashMap<Character, Integer> map = new HashMap<>();//用于保存每个字符已经对应的出现次数
for (int i = 0 ; i < str.length() ; i++) {//统计各个字符出现的次数
map.put(str.charAt(i),map.getOrDefault(str.charAt(i),0)+1);
}
for (int i = 0 ; i < str.length() ; i++) {//遍历map 找出第出现次数为1的字符
if (map.get(str.charAt(i)) < 2) {
return i;
}
}
return -1;
} import java.util.*;
public class Solution {
public int FirstNotRepeatingChar(String str) {
if (str == null || str.equals("")) {
return -1;
}
Map<Character, Integer> map = new HashMap<>();
int length = str.length();
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
if (map.containsKey(c)) {
map.put(c, -1);
} else {
map.put(c, i);
}
}
int ans = Integer.MAX_VALUE;
for (Integer value : map.values()) {
if (value != -1) {
ans = Math.min(ans, value);
}
}
return ans == Integer.MAX_VALUE ? -1 : ans;
}
} import java.util.*;
public class Solution {
public int FirstNotRepeatingChar(String str) {
Map<Character,Integer> map = new LinkedHashMap<>();
for(Character ch:str.toCharArray()){
map.put(ch,map.getOrDefault(ch,0)+1);
}
int num = -1;
for(Character temp:map.keySet()){
if(map.get(temp)==1){
num = str.indexOf(temp);
break;
}
}
return num;
}
}