LeetCode Task03 查找

1. LeetCode 349 Intersection Of Two Arrays

  • 题目描述
    给定两个数组nums,求两个数组的公共元素。

如nums1 = [1,2,2,1],nums2 = [2,2]
结果为[2]
结果中每个元素只能出现一次
出现的顺序可以是任意的

  • 代码实现
class Solution {
   public int[] intersection(int[] nums1, int[] nums2) {
       HashSet<Integer> set1=new HashSet<>();
       HashSet<Integer> set2=new HashSet<Integer>();     
       for(int i:nums1){
           set1.add((Integer)i);
       }
       for(int i:nums2){
           if(set1.contains(i)){
               set2.add(i);
           }
       }
       int[] res=new int[set2.size()];
       int index=0;
       for(Integer i:set2){
           res[index++]=i;
       }
       return res;
   }
}

2. LeetCode 350 Intersection Of Two Arrays 2

  • 题目描述
    给定两个数组nums,求两个数组的交集。

如nums1 = [1,2,2,1],nums2 = [2,2]
结果为[2,2]
出现的顺序可以是任意的

  • 代码实现
class Solution {
   public int[] intersect(int[] nums1, int[] nums2) {
   if (nums1.length == 0  ||  nums2.length == 0)
       {
           return new int[0];
       }
       Map<Integer, Integer> map = new HashMap<Integer, Integer>();
       for(int e1 : nums1)
       {
           int count = map.getOrDefault(e1, 0) + 1;

           map.put(e1, count);
       }
       int[] insertArray = new int[nums1.length];
       int index = 0;
       for (int e2 : nums2)
       {
           int count = map.getOrDefault(e2, 0);
           if(count > 0)
           {
               insertArray[index++] = e2;
               count--;
               if(count > 0)
               {
                   map.put(e2, count);
               }
               else
               {
                   map.remove(e2);
               }
           }
       }
       return Arrays.copyOfRange(insertArray, 0, index);
   }
}
var foo = 'bar';

3. LeetCode 242 Intersection Of Two Arrays 2

  • 题目描述
    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

示例1:
输入: s = “anagram”, t = “nagaram”
输出: true

  • 代码实现
      int n = s.length(), res = 0;
       for (int i = 0; i < 26; i++) {
           List<Integer> l = new ArrayList<Integer>();
           l.add(-1);
           for (int j = 0; j < n; j++) {
               if (s.charAt(j) - 'A' != i) {
                   l.add(j);
               }
           }
           if (l.size() <= k + 1) {
               res = n;
               break;
           }
           l.add(n);
           for (int j = k + 1; j < l.size(); j++) {
               res = Math.max(res, l.get(j) - l.get(j - k - 1) - 1);
           }
       }
       return res;

4. LeetCode 202 Happy number

  • 题目描述
    编写一个算法来判断一个数是不是“快乐数”。
    一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数.

输入: 19
输出: true
解释:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

  • 代码实现
 class Solution {
   public boolean isHappy(int n) {
       Set<Integer> set = new HashSet<>();
       set.add(n);
       while(n != 1){
           n = change(n);
           if(set.contains(n)) return false;
           set.add(n);
       }
       return true;
   }
   
   public int change(int n){
       int sum = 0;
       int num;
       while(n != 0){
           num = n%10;
           n /= 10;
           sum += num*num;
       }
       return sum;
   }
}

5. Decollete 290 Word Pattern

  • 题目描述
    给出一个模式(pattern)以及一个字符串,判断这个字符串是否符合模式

示例1:
输入: pattern = “abba”,
str = “dog cat cat dog”
输出: true
示例 2:
输入:pattern = “abba”,
str = “dog cat cat fish”
输出: false
示例 3:
输入: pattern = “aaaa”, str = “dog cat cat dog”
输出: false
示例 4:
输入: pattern = “abba”, str = “dog dog dog dog”
输出: false

  • 代码实现
// A code block
       String[] strings = str.split(" ");
       if (pattern.length() != strings.length) {
           return false;
       }
       Map<Character, String> map = new HashMap<>();
       for (int i = 0; i < pattern.length(); i++) {
           char c = pattern.charAt(i);
           if (!map.containsKey(c)) {
               map.put(c, strings[i]);
           } else {
               if (!map.get(c).equals(strings[i])) {
                   return false;
               }
           }
       }
       Set<String> set = new HashSet<>();
       map.forEach((key, value) -> {
           set.add(value);
       });
       if (set.size() != map.size()) {
           return false;
       }
       return true;
   }
·`
# 6.  LeetCode 205 Isomorphic Strings
- 题目描述
给定两个字符串 s 和 t,判断它们是否是同构的。
如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。
所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身
> 示例 1:
输入: s = "egg", t = "add"
输出: true
示例 2:
输入: s = "foo", t = "bar"
输出: false
示例 3:
输入: s = "paper", t = "title"
输出: true
- 代码实现
```java
class Solution {
   public boolean wordPattern(String pattern, String str) {
       String[] strings = str.split(" ");
       if (pattern.length() != strings.length) {
           return false;
       }
       Map<Character, String> map = new HashMap<>();
       for (int i = 0; i < pattern.length(); i++) {
           char c = pattern.charAt(i);
           if (!map.containsKey(c)) {
               map.put(c, strings[i]);
           } else {
               if (!map.get(c).equals(strings[i])) {
                   return false;
               }
           }
       }
       Set<String> set = new HashSet<>();
       map.forEach((key, value) -> {
           set.add(value);
       });
       if (set.size() != map.size()) {
           return false;
       }
       return true;
   }
}

7. LeetCode 451 Sort Characters By Frequency

  • 题目描述
    给定一个字符串,请将字符串里的字符按照出现的频率降序排列。

示例 1:
输入:
“tree”
输出:
“eert”

  • 代码实现
class Solution {
   public String frequencySort(String s) {
       char[] chs = s.toCharArray();
       Map<Character, Integer> map = new HashMap<>();
       int maxTimes = -1;
       //统计每个字母的频次,并存入哈希表
       for(char c : chs){
           if(!map.containsKey(c)){
               map.put(c, 1);
           }else{
               map.put(c, map.get(c) + 1);
           }
           maxTimes = map.get(c) > maxTimes ? map.get(c) : maxTimes;
       }
       //新建一个桶,将字母存入索引为它的频次的桶里
       ArrayList<Character>[] buckets = new ArrayList[maxTimes + 1];
       for(char c : map.keySet()){
           int frequency = map.get(c);
           if(buckets[frequency] == null){
               buckets[frequency] = new ArrayList<>();
           }
           buckets[frequency].add(c);
       }
       //倒着遍历桶,将桶里的字母取出来,并按照它的频次插入字符数组中
       int p = 0;
       for(int i = maxTimes; i >= 0; i--){
           if(buckets[i] != null){
               for(char c : buckets[i]){
                   //buckets[i]这个桶里的字母的频次为i,因此要插入i个到结果集中
                   for(int j = 0; j < i; j++){
                       //复用chs作为结果集
                       chs[p++] = c;
                   }
               }
           }
       }
       return new String(chs);
   }
}

class Solution {
   public String frequencySort(String s) {
       char[] chs = s.toCharArray();
       Map<Character, Integer> map = new HashMap<>();
       int maxTimes = -1;
       //统计每个字母的频次,并存入哈希表
       for(char c : chs){
           if(!map.containsKey(c)){
               map.put(c, 1);
           }else{
               map.put(c, map.get(c) + 1);
           }
           maxTimes = map.get(c) > maxTimes ? map.get(c) : maxTimes;
       }
       //新建一个桶,将字母存入索引为它的频次的桶里
       ArrayList<Character>[] buckets = new ArrayList[maxTimes + 1];
       for(char c : map.keySet()){
           int frequency = map.get(c);
           if(buckets[frequency] == null){
               buckets[frequency] = new ArrayList<>();
           }
           buckets[frequency].add(c);
       }
       int p = 0;
       for(int i = maxTimes; i >= 0; i--){
           if(buckets[i] != null){
               for(char c : buckets[i]){
                   for(int j = 0; j < i; j++){
                       //复用chs作为结果集
                       chs[p++] = c;
                   }
               }
           }
       }
       return new String(chs);
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值