zoukankan      html  css  js  c++  java
  • 时间空间效率的平衡:第一个只出现一次的字符位置

    在一个字符串(1<=字符串长度<=10000,全部由大小写字母组成)中找到第一个只出现一次的字符,并返回它的位置

    import java.util.LinkedHashMap;
    public class Solution {
        public int FirstNotRepeatingChar(String str) {
            LinkedHashMap<Character, Integer> map = new LinkedHashMap<Character, Integer>();
            for (int i = 0; i < str.length(); i++) {
                if (map.containsKey(str.charAt(i))) {
                    int time = map.get(str.charAt(i));
                    map.put(str.charAt(i), ++time);
                } else {
                    map.put(str.charAt(i), 1);
                }
            }
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (map.get(c) == 1) {
                    return i;
                }
            }
            return -1;
        }
    }
  • 相关阅读:
    WPF基础篇之静态资源和动态资源
    15-Node-数据库
    15-Node
    12-Git
    总-S04-03 项目-大事件
    00-PHP难点
    08-PHP基础
    15-ES6
    16-Vue-webpack
    00-Web难点
  • 原文地址:https://www.cnblogs.com/SaraMoring/p/5840310.html
Copyright © 2011-2022 走看看