一开始本来打算省时间用特么的有道翻译, 结果直接懵逼了。其实还是自己看题目, 结合例子理解一下很快的。
1. 题1(3分)
题目
888. Fair Candy Swap Difficulty: Easy Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has. Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.) Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the size of the candy bar that Bob must exchange. If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.
Example 1: Input: A = [1,1], B = [2,2] Output: [1,2] Example 2: Input: A = [1,2], B = [2,3] Output: [1,2] Example 3: Input: A = [2], B = [1,3] Output: [2,3] Example 4: Input: A = [1,2,5], B = [2,4] Output: [5,4] Note: 1 <= A.length <= 10000 1 <= B.length <= 10000 1 <= A[i] <= 100000 1 <= B[i] <= 100000 It is guaranteed that Alice and Bob have different total amounts of candy. It is guaranteed there exists an answer.
我的答案
这个就是一个简单的算术题。。没什么技术难度
class Solution { public int[] fairCandySwap(int[] A, int[] B) { int totalA = 0; int totalB = 0; for(int a : A) { totalA += a; } for(int b : B) { totalB += b; } int[] results = new int[2]; for(int a : A) for(int b : B) { if(totalA - a + b == totalB + a - b){ results[0] = a; results[1] = b; } } return results; } }
2. 题2(5分)
890. Find and Replace Pattern Difficulty: Medium You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. (Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.) Return a list of the words in words that match the given pattern. You may return the answer in any order.
Example 1: Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb" Output: ["mee","aqq"] Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}. "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.
我的答案
我的思路比较粗暴,一开始我是想把word都用数字 , 比如把 “abb”写成“011”,就是定义第一个字母为0,第二个字母如果和第一个字母一样就append 一个 0,出现了第二个不同的字母就append 1, 第三个不同的就append 2.类推, 但是这个有个问题就是如果超过10个字母就会 1010会混淆。因为有26个字母,所以我就用了char来做这个事情。
class Solution { public List<String> findAndReplacePattern(String[] words, String pattern) { String numPattern = parseAspatternString(pattern); List<String> results = new ArrayList<>(); for(String word : words) { if(numPattern.equals(parseAspatternString(word))) { results.add(word); } } return results; } private String parseAspatternString(String str) { char[] chars = str.toCharArray(); StringBuilder sb = new StringBuilder(); Map<Character, Character> map = new HashMap<>(); for(char c : chars) { if(map.get(c) == null){ char ch = (char)(map.size() + 97); sb.append(ch); map.put(c, ch); } else { sb.append(map.get(c)); } } return sb.toString(); } }
第三题是个二叉树没看懂题目
第四题是个List的子序列问题,题目很粗暴就是遍历排列组合。我第一时间想到了Python。但是我只会切片操作。。不会用迭代器