首页 > 其他分享 >leetcode-387-easy

leetcode-387-easy

时间:2023-01-04 19:34:56浏览次数:38  
标签:return int Example Output easy 387 Input counts leetcode

First Unique Character in a String

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

Example 1:

Input: s = "leetcode"
Output: 0
Example 2:

Input: s = "loveleetcode"
Output: 2
Example 3:

Input: s = "aabb"
Output: -1
Constraints:

1 <= s.length <= 105
s consists of only lowercase English letters.

思路一:用长度为 26 的数组存储字符出现过的次数,最后遍历字符串,第一次出现次数为 1 的字符就是求解

    public int firstUniqChar(String s) {
        int[] counts = new int[26];

        for (int i = 0; i < s.length(); i++) {
            counts[s.charAt(i) - 'a']++;
        }

        for (int i = 0; i < s.length(); i++) {
            if (counts[s.charAt(i) - 'a'] == 1) {
                return i;
            }
        }

        return -1;
    }

标签:return,int,Example,Output,easy,387,Input,counts,leetcode
From: https://www.cnblogs.com/iyiluo/p/17025801.html

相关文章

  • leetcode-414-easy
    ThirdMaximumNumberGivenanintegerarraynums,returnthethirddistinctmaximumnumberinthisarray.Ifthethirdmaximumdoesnotexist,returnthemaxim......
  • leetcode-563-easy
    BinaryTreeTiltGiventherootofabinarytree,returnthesumofeverytreenode'stilt.Thetiltofatreenodeistheabsolutedifferencebetweenthesum......
  • leetcode-349-easy
    IntersectionofTwoArraysGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustbeuniqueandyoum......
  • leetcode-350-easy
    IntersectionofTwoArraysIIGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustappearasmany......
  • leetcode-367-easy
    ValidPerfectSquareGivenapositiveintegernum,returntrueifnumisaperfectsquareorfalseotherwise.Aperfectsquareisanintegerthatisthesquar......
  • 【转载】SRS、EasyDarwin、ZLMediaKit、Monibuca对比分析
    声明转自liuzhen007的《SRS、EasyDarwin、ZLMediaKit、Monibuca对比分析》作者:liuzhen007链接:https://juejin.cn/post/6926739029496954888来源:稀土掘金著作权归作......
  • 【栈】LeetCode 20. 有效的括号
    题目链接20.有效的括号思路碰见左括号就入栈,碰见右括号就和检查栈顶括号是否配队。遍历完后还要检查栈是否为空,确定括号数量是合法的。代码classSolution{publ......
  • 【逆波兰表达式】【栈】LeetCode 150. 逆波兰表达式求值
    题目链接150.逆波兰表达式求值思路从左到右遍历tokens遇到数字便放入栈中,遇到运算符便弹出栈顶的两个数字进行运算。代码classSolution{publicintevalRPN(......
  • EasyAR4.0稀疏空间地图室内导航
    现有的AR室内导航,一种方案是利用运动跟踪实现,但是偏移较大。比较靠谱或者说能满足商业使用的还是稀疏空间地图。(ARCore管叫云锚点)实现效果如下:EasyAR稀疏云地图室内导航......
  • EasyAR4.0使用说明(五)----3D物体跟踪
    3D物体跟踪总体上是和平面图像跟踪差不多的,设置,包括程序控制,识别多个对象。区别只是目标对象的不同。总体说明3D物体跟踪对3D物体的纹理,也就是表面的图案的丰富程度是有要求......