Leetcode solution to a problem - a hash table data structures

Use of the hash table for storing data O (N) space complexity, and in O (1) time complexity to solve the problem.

  • In Java HashSet to store a collection, you can find an element is in the collection. If the element is finite and limited in scope, you can use a Boolean array to store an element exists. For example, only the elements of lowercase characters, can be used a length of 26 Boolean array to store a set of characters such that the space complexity is reduced to O (1).

  • The Java HashMap is mainly used to map relationships that link the two elements. HashMap elements can also be used for counting statistics, is a key element of this time, the count value. And HashSet Similarly, if the element finite and limited in scope, can be used for statistical array of integers. When the content of a compressed or other conversion, the content can be HashMap using the original content and the conversion link. For example, in a simplified system url Leetcdoe: 535. TinyURL the Encode and the Decode (Medium) , can be stored using HashMap url streamlined url mapping to the original, so that not only can display a simplified url, can be obtained according to the simplified url original url to locate the correct resources.

1 and the array for a given value of two numbers

1. Two Sum (Easy)

You can first sort the array, then two-hand method or the binary search method. Doing this time complexity is O (NlogN), the spatial complexity is O (1).

HashMap with a storage array elements and indexes are mapped in the access to the nums [i], determines whether there is a target in the HashMap - nums [i], if there is described target - the nums Index [i] where i and is looking for two number. The time complexity of this method is O (N), the spatial complexity exchange time is O (N), the use of space.

public int[] twoSum(int[] nums, int target) {
    HashMap<Integer, Integer> indexForNum = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (indexForNum.containsKey(target - nums[i])) { return new int[]{indexForNum.get(target - nums[i]), i}; } else { indexForNum.put(nums[i], i); } } return null; }

2. determines whether the array element comprising a repeating

217. Contains Duplicate (Easy)

public boolean containsDuplicate(int[] nums) {
    Set<Integer> set = new HashSet<>(); for (int num : nums) { set.add(num); } return set.size() < nums.length; }

3. The longest sequence harmony

594. Longest Harmonious Subsequence (Easy)

Input: [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].

The maximum number of poor in harmonious sequence and the minimum number of exactly 1, it should be noted that the sequence of elements is not necessarily a continuous element of the array.

public int findLHS(int[] nums) {
    Map<Integer, Integer> countForNum = new HashMap<>(); for (int num : nums) { countForNum.put(num, countForNum.getOrDefault(num, 0) + 1); } int longest = 0; for (int num : countForNum.keySet()) { if (countForNum.containsKey(num + 1)) { longest = Math.max(longest, countForNum.get(num + 1) + countForNum.get(num)); } } return longest; }

4. The longest contiguous sequence

128. Longest Consecutive Sequence (Hard)

Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Required to O (N) time complexity Solution.

public int longestConsecutive(int[] nums) {
    Map<Integer, Integer> countForNum = new HashMap<>(); for (int num : nums) { countForNum.put(num, 1); } for (int num : nums) { forward(countForNum, num); } return maxCount(countForNum); } private int forward(Map<Integer, Integer> countForNum, int num) { if (!countForNum.containsKey(num)) { return 0; } int cnt = countForNum.get(num); if (cnt > 1) { return cnt; } cnt = forward(countForNum, num + 1) + 1; countForNum.put(num, cnt); return cnt; } private int maxCount(Map<Integer, Integer> countForNum) { int max = 0; for (int num : countForNum.keySet()) { max = Math.max(max, countForNum.get(num)); } return max; }

Guess you like

Origin www.cnblogs.com/daimasanjiaomao/p/11009128.html