-
- 有效的字母异位词
题目:判断两个字符串的字母是否一样
解题思路:通过字母在Ascll表对应的数字可以定义一个26容量大小的数组,将字符串包含的字母映射到数组中,再用另一个字符串对数组进行操作
代码如下:
class Solution { public boolean isAnagram(String s, String t) { int [] flag = new int[26]; for(int i = 0;i<s.length();i++){ flag[s.charAt(i)-'a']++; } for(int i = 0;i<t.length();i++){ flag[t.charAt(i)-'a']--; } for(int i = 0;i<26;i++){ if(flag[i]!=0){ return false; } } return true; } }
- 有效的字母异位词
-
- 两个数组的交集
题目:找出两个数组中都包含的数字
题解:两种方法一种是使用HashSet(二刷建议使用),题目要求数字的大小不超过1000,因此可以用一个1000容量的数组进行判断
- 两个数组的交集
-
- 快乐数
题解:显然这里需要一个函数来计算求出来的数符不符合要求,这道题的思路没错,但不能正确的编写出代码(建议二刷)
代码如下:
class Solution { public boolean isHappy(int n) { Set<Integer> record = new HashSet<>(); while (n != 1 && !record.contains(n)) { record.add(n); n = getNextNumber(n); } return n == 1; } private int getNextNumber(int n) { int res = 0; while (n > 0) { int temp = n % 10; res += temp * temp; n = n / 10; } return res; } }
- 快乐数
-
- 两数之和
题解:刷了好多边了,使用hash表,,再也不是那个人家夜里看海,你leetcode第一题做不出来的大菜鸟了,是小
- 两数之和