博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指Offer——字符串中第一个只出现一次的字符
阅读量:4227 次
发布时间:2019-05-26

本文共 847 字,大约阅读时间需要 2 分钟。

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

public class Solution {    /*    思路:考虑到这是求字符出现的次数,而字符时有限个数的,而且又涉及到字符是否重复出现,    因此考虑到用哈希表来记录字符是否出现过,那么我们用256长度的数组来表示256个字符,并    初始化为-1,表示还没有出现过,如果一个字符第一次出现,那么将字符对应的位置设为出现的    次序index,如果一个字符对应的位置大于等于0,那么将这个位置设置为,2,表示出现过不止    一次,字符串循环完毕后,找出大于0的最小的数字的位置对应的字符串就是第一个只出现一次    的字符串    */    public int FirstNotRepeatingChar(String str) {        if(str == null || str.length() == 0){            return -1;        }        //int index = 0; //用来表示字符出现的顺序        int len = str.length();        int[] count = new int[256];        for(int i = 0; i<256;i++){            count[i] = -1;        }        for(int i=0; i
=0 ){ count[c] = -2; } //index++; } int min = Integer.MAX_VALUE; for(int j = 0; j
=0){ if(count[j]

转载地址:http://wpnqi.baihongyu.com/

你可能感兴趣的文章
AutoCAD: Secrets Every User Should Know
查看>>
Combating Spyware in the Enterprise
查看>>
Microsoft Windows Vista Unveiled
查看>>
How to Cheat at Securing a Wireless Network
查看>>
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
查看>>
Smarty Php Template Programming And Applications
查看>>
Web Site Measurement Hacks
查看>>
The Best Damn Windows Server 2003 Book Period
查看>>
Cleaning Windows XP For Dummies
查看>>
The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
查看>>
Python in a Nutshell
查看>>
Microsoft Visual C++ .NET Professional Projects
查看>>
Excel 2007 Advanced Report Development
查看>>
Security Metrics: Replacing Fear, Uncertainty, and Doubt
查看>>
Accelerating Process Improvement Using Agile Techniques
查看>>
The New Language of Business: SOA & Web 2.0
查看>>
Programming Mobile Devices: An Introduction for Practitioners
查看>>
Designing for Networked Communications: Strategies and Development
查看>>
Building a Monitoring Infrastructure with Nagios
查看>>
Illustrated C# 2005
查看>>