Leetcode题目分类整理

一、数组

8) 双指针 ---- 滑动窗口

例题:

    3. Longest Substring Without Repeating Characters

描述:Given a string, find the length of the longest substring without repeating characters.

题解:时间:92.67%,空间:87.02%

 1     public int lengthOfLongestSubstring(String s) {   
 2         // check
 3         if(s == null || s.length() == 0) return 0;
 4         
 5         // initial 
 6         int[] freq = new int[256];
 7         Arrays.fill(freq, -1);
 8         int l = 0, r = 0, res = 0;
 9         
10         while(r < s.length()){
11             if(freq[s.charAt(r)] != -1) l = Math.max(l, freq[s.charAt(r)] + 1);
12             freq[s.charAt(r)] = r++;
13             res = Math.max(res, r - l);
14         }
15         
16         return res;
17     }

练习:

438. Find All Anagrams in a String

 76. Minimum Window Substring

二、查找问题 

1)查找有无:是否存在

2)查找对应关系:出现了几次

349. Intersection of Two Arrays   // 熟悉Set

描述:Given two arrays, write a function to compute their intersection.

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
 1     public int[] intersection(int[] nums1, int[] nums2) {
 2         if(nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) 
 3             return new int[0];
 4         
 5         Set<Integer> set1 = new HashSet<Integer>();
 6         Set<Integer> setTemp = new HashSet<Integer>();
 7         
 8         for(int num : nums1) set1.add(num);
 9         for(int num : nums2) if(set1.contains(num)) setTemp.add(num);
10             
11         int k = setTemp.size();
12         int[] res = new int[k];
13         for(int num : setTemp) res[--k] = num;
14         
15         return res;    
16     }

350. Intersection of Two Arrays II          // 熟悉 Map

描述:Given two arrays, write a function to compute their intersection.

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
 1     public int[] intersect(int[] nums1, int[] nums2) {
 2         if(nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0)
 3             return new int[0];
 4         
 5         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
 6         for(int num : nums1){
 7             map.put(num, map.getOrDefault(num, 0) + 1);
 8         }
 9         
10         List<Integer> list = new ArrayList<Integer>();
11         for(int num : nums2){
12             if(map.get(num) != null && map.get(num) > 0){
13                 list.add(num);
14                 map.put(num, map.get(num) - 1);
15             }
16         }
17         
18         int k = list.size();
19         int[] arr = new int[k];
20         for(int num : list) arr[--k] = num;
21         
22         return arr;
23     }

242. Valid Anagram

题目描述:Given two strings s and , write a function to determine if t is an anagram of s.

202. Happy Number

290. Word Pattern

205. Isomorphic Strings

451. Sort Characters By Frequency

查找表的经典问题:

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

时间:99.77%,空间:88.15% 

1     public int[] twoSum(int[] nums, int target) {
2         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
3         for(int i = 0; i < nums.length; i++){
4             int de = target - nums[i];
5             if(map.get(de) != null) return new int[]{map.get(de), i};
6             map.put(nums[i], i);
7         }
8         return new int[2];
9     }

15. 3Sum

18. 4Sum

16. 3Sum Closest

454. 4Sum II

描述:Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.

To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -2^28 to 2^28 - 1 and the result is guaranteed to be at most 2^31 - 1.

49. Group Anagrams

猜你喜欢

转载自www.cnblogs.com/tf-Y/p/10412101.html