1 第一个只出现一次的字符
题目:在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。
示例:
s = “abaccdeff”
返回 “b”
s = “”
返回 " "
解题思路:
哈希表 的使用,思路为:
遍历字符串 s ,使用哈希表存储各字符的数量;
再遍历字符串 s ,在哈希表中找到第一个数量为 1 的字符,并返回。
方法一
class Solution {
public char firstUniqChar(String s) {
if(s.isEmpty()){
return ' ';
}
char[] chars = s.toCharArray();
Map<Character,Integer> map=new HashMap<>();
for(int i=0;i<chars.length;i++){
if(map.containsKey(chars[i])){
map.put(chars[i],map.get(chars[i])+1);
}else{
map.put(chars[i],1);
}
}
for(int i=0;i<chars.length;i++){
if (map.get(chars[i])==1){
return chars[i];
}
}
return ' ';
}
}
方法二
class Solution {
public char firstUniqChar(String s) {
HashMap<Character, Integer> dic = new HashMap<>();
char[] sc = s.toCharArray();
for(char c : sc) {
if(!dic.containsKey(c)) dic.put(c, 1);
else dic.put(c, dic.get(c) + 1);
}
for(char c : sc) {
if(dic.get(c) == 1) return c;
}
return ' ';
}
}
2 数组中出现次数超过一半的数字
题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。你可以假设数组是非空的,并且给定的数组总是存在多数元素。
示例 1:
输入: [1, 2, 3, 2, 2, 2, 5, 4, 2]
输出: 2
class Solution {
public int majorityElement(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
for (int i = 0; i < nums.length; i++) {
if (map.get(nums[i]) > (nums.length / 2)){
return nums[i];
}
}
return 0;
}
}
总结:return只要找到一个就返回。(题目不要求找出所有)
3 在排序数组中查找数字
题目:统计一个数字在排序数组中出现的次数。
示例 1:
输入: nums = [5,7,7,8,8,10], target = 8
输出: 2
示例 2:
输入: nums = [5,7,7,8,8,10], target = 6
输出: 0
方法一
public static int search(int[] nums, int target) {
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++){
if(map.containsKey(nums[i])){
map.put(nums[i],map.get(nums[i])+1);
}else{
map.put(nums[i],1);
}
}
for(int i=0;i<nums.length;i++){
if(nums[i]==target){
return map.get(nums[i]);
}
}
return 0;
}
}
4 数组中重复的数字
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例 :
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
使用哈希表解决
方法一
class Solution {
public int findRepeatNumber(int[] nums) {
Set<Integer>set=new HashSet<>();
int respect=-1;
for(int num:nums){
if(!set.add(num)){
respect=num;
}
}
return respect;
}
}
方法二
public static int select(int[] array){
Set<Integer>set=new HashSet<>();
for(int c:array){
if(set.contains(c)){
return c;
}else{
set.add(c);
}
}
return -1;
}
}
5. 只出现一次的数字 III
给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。
示例 :
输入: [1,2,1,3,2,5]
输出: [3,5]
注意:
1结果输出的顺序并不重要,对于上面的例子, [5, 3] 也是正确答案。
2 你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?
public class test81 {
public static void main(String[] args) {
int[] array={1,2,1,3,2,5};
int[] ints = select(array);
String s = Arrays.toString(ints);
System.out.println(s);
}
public static int[] select(int[] array) {
Map<Integer,Integer>map=new HashMap<>();
for(int c:array){
if(map.containsKey(c)){
map.put(c,map.get(c)+1);
}else{
map.put(c,1);
}
}
int[] output=new int[2];
int idx=0;
for(int c:map.keySet()){
if (map.get(c)==1){
output[idx++]=c;
}
}
return output;
}
}