zoukankan      html  css  js  c++  java
  • leetcode387

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

    Examples:

    s = "leetcode"
    return 0.
    
    s = "loveleetcode",
    return 2.
    找出第一次出现的不重复的字符
    public class Solution {
        public int firstUniqChar(String s) {
            //97-122
            int[] map = new int[123];
            int i=0;
            int length = s.length();
            for(i=0;i<length;i++)
            {
                map[s.charAt(i)]++;
            }
            for(i=0;i<length;i++)
            {
                if(map[s.charAt(i)] == 1)
                    return i;
            }
            return -1;
        }
    }

    除了两次for循环,暂时没有想到N时间复杂度的解法。

  • 相关阅读:
    客户端加锁
    三次握手
    ForkJoinTask
    主从Reactor多线程模型
    Happen-before
    Enum
    Java 8
    Netty
    分布式一致性算法
    VisualStudio 2013 快捷键
  • 原文地址:https://www.cnblogs.com/linkstar/p/5958600.html
Copyright © 2011-2022 走看看