2

JZ-034-第一个只出现一次的字符位置

 2 years ago
source link: https://segmentfault.com/a/1190000041164717
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JZ-034-第一个只出现一次的字符位置

发布于 今天 17:36

第一个只出现一次的字符位置

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)

题目链接: 第一个只出现一次的字符位置

/**
 * 标题:第一个只出现一次的字符位置
 * 题目描述
 * 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)
 * 题目链接:
 * https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&&tqId=11187&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz34 {

    /**
     * 字符串遍历
     *
     * @param str
     * @return
     */
    public int firstNotRepeatingChar(String str) {
        int[] cnts = new int[256];
        for (int i = 0; i < str.length(); i++) {
            cnts[str.charAt(i)]++;
        }
        for (int i = 0; i < str.length(); i++) {
            if (cnts[str.charAt(i)] == 1) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {

    }
}

【每日寄语】 能坚持别人不能坚持的,才能拥有别人不能拥有的。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK