剑指offer_第一个只出现一次的字符
来源:互联网 发布:大数据商业计划书 编辑:程序博客网 时间:2024/06/18 00:55
/*在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置*/import java.util.ArrayList;import java.lang.Character;class FirstNotRepeatingChar{public static int firstNotRepeatingChar(String str) {if (str==null||str.length()<=0){return -1;}for (int i=0; i<str.length(); i++){char c=str.charAt(i);if (str.indexOf(c)==str.lastIndexOf(c))return i;}return 0; }public static int firstNotRepeatingChar2(String str) {if (str==null||str.length()<=0){return -1;}int[] countOfEachChar=new int[123];//z的ASCII码为122for (int i=0; i<str.length(); i++){countOfEachChar[str.charAt(i)]++;}for (int i=0; i<str.length; i++){if (countOfEachChar[str.charAt(i)]==1){return i;}/*if (countOfEachChar[i]==1){return str.indexOf((char)i);}//这样写的话google返回的是e的位置,因为是按照countOfEachChar数组从前到后遍历的,不对*/}return 0; }public static void main(String[] args) {System.out.println(firstNotRepeatingChar2("google"));}}
阅读全文
0 0
- 剑指offer_第一个只出现一次的字符
- 剑指offer_第一个只出现一次的字符
- 剑指offer_第一个只出现一次的字符
- 剑指offer_字符流中第一个只出现一次的字符
- 剑指Offer_面试题35_第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 第一个只出现一次的字符
- 归并排序算法
- APUE读书笔记-第十三章-守护进程
- 4.3.2深度定时任务(TimerTask in Depth)
- 线程池学习笔记
- 如何成为一个及格的码农
- 剑指offer_第一个只出现一次的字符
- 常用图片格式介绍(了解
- leetcode 24 swapPairs
- from google.protobuf.internal import enum_type_wrapper ImportError: No module named google.protobuf
- 【Android】Android性能优化
- 设计模式之享元模式
- Unity5实战使用C#和Unity开发多平台游戏pdf
- 4.4总结
- 时间类型的转换