Leetcode
349. Intersection of Two Arrays
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.
解题思路
简单集合操作set
|
|
350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
|
|
- What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
|
|
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
|
|
解题思路
简单map操作
|
|
242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.
Note:
You may assume the string contains only lowercase alphabets.
解题思路
简单的map操作
|
|
聪明人的简洁做法
|
|
202. Happy Number
Write an algorithm to determine if a number is “happy”.
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
|
|
解题思路
Set保存着82, 68, 100等等…一旦出现了重复值,那么这个数就不可能是happy number了
|
|
290. Word Pattern
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.
Examples:
|
|
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
解题思路
很简单的键值映射关系,这边有个坑是关于HashMap的get和put操作,最好看下源码
|
|
205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
|
|
Note:
You may assume both s and t have the same length.
解题思路
将字符映射其最近所存储的位置,一旦发现两者的存储位置不同,返回false
|
|
451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters.
Example:
|
|
解题思路
借助HashMap和TreeMap实现
HashMap用来统计每次字符出现的频率
TreeMap维护频率和字符列表,因为字符出现的频率可能重复,所以使用list来维护
|
|
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.
Example
|
|
解题思路
由于输入的数组并不是排序数组,所以直接使用对撞指针是不行的
使用Map找complement值
通常做法:首先将所有元素存入,接着遍历索引直到找到匹配的值;
有个小技巧:
只需要遍历一次,每次存入元素之前,就可以判断是否存在匹配的值,并且可以省去判断自否为重复索引的问题
|
|
15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
|
|
解题思路
先对数组进行排序,然后基于对撞指针的想法,将三个元素相加,寻找和为0的组合
|
|
18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
|
|
解题思路:
与上一题一样的思路,只不过多了一层循环
|
|
16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
|
|
解题思路:
类似于题目 3Sum;判别条件变了而已
|
|
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.
Example:
|
|
解题思路
空间换时间,题目也说了数组的长度为 0 <= N <= 500
暴力解法的复杂度为N的四次方,通过HashMap可以降为N的2次方
空间复杂度也是为N的2次方
|
|
49. Group Anagrams
Given an array of strings, group anagrams together.
For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],
Return:
|
|
Note: All inputs will be in lower-case.
解题思路:
将包含的相同字符集和个数的字符串进行分类
相同的Anagram字符串作为HashMap的Key,处理之前先进行排序,使其具有唯一性
|
|
447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
Example:
|
|
解题思路:
PointX与界面上的点距离,利用HashMap维护距离及其相同距离的个数
最后统计有多个对距离相同的边 的个数result
假设distance 为 a,并且个数为1,那么result += 0;
假设distance 为 b,并且个数为2,那么result += 2;
假设distance 为 c,并且个数为3, 那么result += 6。
以此类推(选择问题, 对于每个点m, 都有m-1种选择)
注意distance返回的值越界,不过该题不会越界, 10000 * 10000 小于 2的31次方-1
|
|
149. Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
解题思路:
详见注释
|
|
219. Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
解题思路:
滑动窗口+查找表
滑动窗口的最大为k, 在窗口k内若找到重复的元素,返回true
每次将元素加入Set中,一旦set大小超过k,也就是窗口大小大于k,
那么就可以删除窗口最左端元素
|
|
217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
解题思路:
简单的Set操作
|
|
220. Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
解题思路:
可以把k看做滑动窗口,对于一个新的数,可以想象成放到这个窗口的最左边或者最右边,如果这个数+t或-t之后落在窗口里,直接返回true。
|
|