Find Anagram Mappings

 1 class Solution {
 2     public int[] anagramMappings(int[] A, int[] B) {
 3         Map<Integer, Integer> D = new HashMap();
 4         for (int i = 0; i < B.length; ++i)
 5             D.put(B[i], i);
 6 
 7         int[] ans = new int[A.length];
 8         int t = 0;
 9         for (int x: A)
10             ans[t++] = D.get(x);
11         return ans;
12     }
13 }

思考:用字典来解决问题,巧妙将数组编号转换为页码,然后快速查表

 771.Jewels and Stones

class Solution {
    public int numJewelsInStones(String J, String S) {
        int result = 0;
        HashMap<Character,Integer> jMap = new HashMap<>();
        for(int i=0;i<J.length();i++)
            jMap.put(J.charAt(i),i);
        for(int i=0;i<S.length();i++)
        {
            if(jMap.containsKey(S.charAt(i)))
                result++;
        }
        return result;
    }
}

  

相关文章:

  • 2022-12-23
  • 2021-12-23
  • 2022-02-19
  • 2021-04-25
  • 2022-01-21
  • 2021-09-14
  • 2022-12-23
  • 2022-03-01
猜你喜欢
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
  • 2021-04-08
相关资源
相似解决方案