242. 有效的字母异位词
class Solution {
public boolean isAnagram(String s, String t) {
int[] res = new int[26];
for(char c : s.toCharArray()){
res[c-'a'] += 1;
}
for(char c : t.toCharArray()){
res[c-'a'] -= 1;
}
for(int i : res){
if(i!=0){
return false;
}
}
return true;
}
}
383. 赎金信
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] arr = new int[26];
int temp;
for(int i = 0;i<magazine.length();i++){
temp = magazine.charAt(i) - 'a';
arr[temp]++;
}
for(int i = 0;i<ransomNote.length();i++){
temp = ransomNote.charAt(i) - 'a';
if(arr[temp]>0){
arr[temp]--;
}else{
return false;
}
}
return true;
}
}
49. 字母异位词分组
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String,List<String>> map = new HashMap<String,List<String>>();
for(String str : strs){
char[] arr = str.toCharArray();
Arrays.sort(arr);
String key = new String(arr);
List<String> list = map.getOrDefault(key,new ArrayList<String>());
list.add(str);
map.put(key,list);
}
return new ArrayList<List<String>>(map.values());
}
}
hashmap.getOrDefault(Object key, V defaultValue)
:获取指定key对应的value,如果找不到key,则返回设置的默认值。
438. 找到字符串中所有字母异位词
虽然只击败了5%,但毕竟是我能想到的最简单的方法
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<>();
int lenS = s.length();
int lenP = p.length();
if(lenP>lenS){
return res;
}
char[] comapre = p.toCharArray();
Arrays.sort(comapre);
String cp = new String(comapre);
for(int i = 0;i+lenP<=lenS;i++){
String strTemp = s.substring(i,i+lenP);
char[] tmp = strTemp.toCharArray();
Arrays.sort(tmp);
String tmpcp = new String(tmp);
if(tmpcp.equals(cp)){
res.add(i);
}
}
return res;
}
}
349. 两个数组的交集
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
//拿到题目一定要先看题目的要求,以及返回值
if(nums1 == null || nums1.length == 0||nums2.length==0||nums2==null){
return new int[0];
}
Set<Integer> set = new HashSet<>();
Set<Integer> res = new HashSet<>();
for(int i:nums1){
set.add(i);
}
for(int i:nums2){
if(set.contains(i)){
res.add(i);
}
}
int[] resArr = new int[res.size()];
int index = 0;
for(int i:res){
resArr[index++] = i;
}
return resArr;
}
}
350. 两个数组的交集 II
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
//先短数组
if(nums2.length<nums1.length){
return intersect(nums2,nums1);
}
Map<Integer, Integer> map = new HashMap<>();
for(int num:nums1){
int count = map.getOrDefault(num,0) +1;
map.put(num,count);
}
int[] temp = new int[nums1.length];
int index = 0;
for(int num: nums2){
int count = map.getOrDefault(num,0);
if(count>0){
temp[index++] = num;
count--;
if(count>0){
map.put(num,count);
}else{
map.remove(num);
}
}
}
return Arrays.copyOfRange(temp,0,index);
}
}
用到了Arrays
的一个copyOfRange(T[] original,int from,int to)
函数,这个函数需要三个参数,原数组,然后是数组的起始索引,会返回一个新数组。
202. 快乐数
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;
}
}
1. 两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
if(nums==null || nums.length==0){
return res;
}
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0;i<nums.length;i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
}
map.put(nums[i],i);
}
return res;
}
}