LeetCode-387.字符流中第一个不重复的字符 [C++][C][Java][Kotlin][Rust]

LeetCode-387. First Unique Character in a StringLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/first-unique-character-in-a-string/submissions/

题目描述

Given a string sfind the first non-repeating character in it and return its index. If it does not exist, return -1.

解题思路

【C++解法】

class Solution {
public:
    int firstUniqChar(string s) {
        int len = s.size(); //s.length()
        int hash[256] = {0};

        for (int i=0; i<len; i++) {
            hash[s[i]]++;
        }

        for (int i=0; i<len; i++) {
            if (hash[s[i]] == 1) {return i;}
        }
        
        return -1;
    }
};

【C解法】

int firstUniqChar(char * s){
    int len = strlen(s);
    int hash[256] = {0};
    for (int i=0; i<len; i++) {
        hash[s[i]]++;
    }
    for (int i=0; i<len; i++) {
        if (hash[s[i]] == 1) {return i;}
    }
    return -1;
}

【Java解法】

class Solution {
    public int firstUniqChar(String s) {
        int[] hash = new int[256];
        char[] chs = s.toCharArray();
        for (char ch : chs) {
            hash[ch]++;
        }
        for (int i = 0; i < s.length(); i++) {
            if (hash[chs[i]] == 1) {
                return i;
            }
        }
        return -1;
    }
}

【Kotlin解法】

class Solution {
    fun firstUniqChar(s: String): Int {
        var hash = IntArray(256)
        for (ch in s) {
            hash[ch.toInt()]++
        }
        for (i : Int in 0..s.length - 1) {
            if (hash[s[i].toInt()] == 1) {
                return i;
            }
        }
        return -1;
    }
}

【Rust解法】

位运算

impl Solution {
    pub fn first_uniq_char(s: String) -> i32 {
        let chars = s.as_bytes();
        let (mut m2l, mut ms) = (0u32, 0u32);
        let mut m2s;
        for &c in chars {
            let hash = 1 << (c & 0x1f);
            m2s = ms & hash; 
            ms |= hash;
            m2l |= m2s;
        }
        let once = ms ^ m2l;
        for i in 0..chars.len() {
            if 1 << (chars[i] & 0x1f) & once > 0 {
                return i as i32
            }
        }
        -1
    }
}

使用Vec

impl Solution {
    pub fn first_uniq_char(s: String) -> i32 {
        let mut char_vec = vec![0; 256];
        s.chars().for_each(|c| { char_vec[c as usize] += 1});
        for (i, c) in s.char_indices() {
            if char_vec[c as usize] == 1 {
                return  i as i32;
            }
        }
        -1
    }
}

使用HashMap

use std::collections::HashMap;
impl Solution {
    pub fn first_uniq_char(s: String) -> i32 {
        let mut count: HashMap<char, i32> = HashMap::new();
        for l in s.chars() {
            if count.contains_key(&l) {
                let val = count.get(&l).unwrap();
                count.insert(l, val + 1);
            } else {
                count.insert(l, 1);
            }
        }
        for (i, l) in s.chars().enumerate() {
            let val = *count.get(&l).unwrap();
            if val == 1 {
                return i as i32;
            }
        }
        return -1;
    }
}

 

《剑指offer》同类型题目 

《剑指offer》75--字符流中第一个不重复的字符[C++][C][Java][Kotlin]_贫道绝缘子的博客-CSDN博客_找到第一个不重复的字符找出字符流中第一个只出现一次的字符。https://blog.csdn.net/qq_15711195/article/details/96865670

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值