leetcode-387-字符串中的第一个唯一字符

问题:

package com.example.demo;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Test387 {

    /**
     * 方法一:借助set
     */
    public int firstUniqChar(String s) {
        if (s == null || s.length() == 0) {
            return -1;
        }
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            String sub = s.substring(i, i + 1);
            map.put(sub, map.getOrDefault(sub, 0) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            String sub = s.substring(i, i + 1);
            if (map.get(sub) == 1) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 方法二:利用桶站位
     * 问题提示:都是小写字母,所以新建的数据也就是26个长度
     */
    public int firstUniqChar1(String s) {
        int len = s.length();
        int[] bucket = new int[26];
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            bucket[c - ‘a‘]++;
        }

        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if(bucket[c - ‘a‘] == 1){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Test387 t = new Test387();
        int asdad = t.firstUniqChar1("asdad");
        System.out.println(asdad);
    }
}

原文地址:https://www.cnblogs.com/nxzblogs/p/11274542.html

时间: 2024-08-07 01:43:59

leetcode-387-字符串中的第一个唯一字符的相关文章

前端与算法 leetcode 387. 字符串中的第一个唯一字符

目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode",

leetcode 387. 字符串中的第一个唯一字符(First Unique Character in a String)

目录 题目描述: 示例: 解法: 题目描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 示例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母. 解法: class Solution { public: int firstUniqChar(string s) { vector<int> count(128, 0); in

leetcode——387. 字符串中的第一个唯一字符

简单题 class Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ k=[] i=0 while i<len(s): if s[i] in k: i+=1 elif s.count(s[i])==1: return i else: k.append(s[i]) i+=1 return -1 执行用时 :176 ms, 在所有 pyt

力扣(LeetCode)字符串中的第一个唯一字符 个人题解

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母. 这题的思想还是比较简单明了的,通过hashmap记录所有的键值对应关系,即字符出现的次数,然后再回过头循环一遍来判断出现的次数是否符合题意. 要循环两次,空间和时间上都差强人意,但是是比较清晰的办法. 而且通过评论区的提醒,当字符串足

【leetcode 简单】 第九十题 字符串中的第一个唯一字符

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. class Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ s_len=len(s) for i in &qu

字符串中的第一个唯一字符

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母. class Solution { public int firstUniqChar(String s) { char[] strs=s.toCharArray(); int index=-1; if(strs.length==0) r

【初级算法】14. 字符串中的第一个唯一字符

题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母. 1.解题思路: 关于本题解题思路很简单,直接统计相关的26个英文字母的个数即可,非常简单. typedef struct counter{ int id; int cnt; }counter; class Solution {

微软算法100题17 字符串中找到第一个只出现一次的字符

第17 题:题目:在一个字符串中找到第一个只出现一次的字符.如输入abaccdeff,则输出b 思路:要找出只出现一次的字符,很明显需要统计所有字符出现的次数,然后找出次数为一的那一个,统计次数最先想到的是hashTable,但此题有更好的办法,因为每个char其实对应一个唯一的ASCII值,所以可以构造一个包含所有字符ASCII值的int数组,来映射字符与出现次数的关系,同时一个char是8个字节,总共有256个可能,所以该数组的大小应为256 1 package com.rui.micros

从第一个字符串中删除第二个字符串中出现过的所有字符

// 从第一个字符串中删除第二个字符串中出现过的所有字符 #include <stdio.h> char* remove_second_from_first( char *first, char *second ) { if( first == NULL || second == NULL ) { printf("first or/and second should not be NULL\n"); return NULL; } char flag[256]={0}; ch