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

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

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

《剑指offer》75同类型题目

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 s, find 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 
【C解法】 
int firstUniqChar(char * s){
    int len = strlen(s);
    int hash[256] = {0};
    for (int i=0; i 
【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 = 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;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5659093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存