leetcode [#290] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (18)
目录1. 题目2. 解决方案3. 注意事项
题目Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Example:
pattern = “abba”, str = “dog cat cat dog” should return true.pattern = “abba”, str = “dog cat cat fish” should return false.pattern = “aaaa”, str = “dog cat cat dog” should return false.pattern = “abba”, str = “dog dog dog dog” should return false.Note:You may assume pat...阅读全文

leetcode [#341] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (12)
目录1. 题目2. 解决方案3. 注意事项
题目Given a nested list of integers, implement an iterator to flatten it.
Each element is either an integer, or a list – whose elements may also be integers or other lists.
Example:Given the list [[1,1],2,[1,1]],
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
Given the list [1,[4,[6]]],
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]....阅读全文

leetcode [#344] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (13)
目录1. 题目2. 解决方案3. 注意事项
题目Write a function that takes a string as input and returns the string reversed.
Example:Given s = “hello”, return “olleh”.
解决方案1234567public class Solution {    public String reverseString(String s) {        StringBuilder handler = new StringBuilder(s);        handler.reverse();        return handler.toString();    }}注意事项以下这种写法可以得到正确答案,但是会超时,无法通过。原因是String一旦创建就不可修改,因此遍历中每次为result衔接新增的部分都会创建新对象并将引用指向result,增大了开销。123456789101112public class Solution {    Stack stack = new...阅读全文

leetcode [#344] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (21)
目录1. 题目2. 解决方案3. 注意事项
题目Write a function that takes a string as input and reverse only the vowels of a string.
Example:Given s = “hello”, return “holle”.Given s = “leetcode”, return “leotcede”.
Note:The vowels does not include the letter “y”.
解决方案12345678910111213141516171819202122232425262728293031public class Solution {    public String reverseVowels(String s) {        int N = s.length();        if(s == null || N == 0) return "";        StringBuilder builderVowel = new StringBuilder();    ...阅读全文

leetcode [#349] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (16)
目录1. 题目2. 解决方案3. 注意事项
题目Given two arrays, write a function to compute their intersection.
Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Note:
Each element in the result must be unique.The result can be in any order.解决方案123456789101112131415161718192021222324252627public class Solution {    public int[] intersection(int[] nums1, int[] nums2) {        Arrays.sort(nums1);        Arrays.sort(nums2);        HashSet<Integer> set = new HashSet<Integer>();        int a = nums1.leng...阅读全文