Array & Hash
Array & Hash 🌼
Note
Interface
An interface
is a completely “abstract class“ that is used to group related methods with empty bodies
1 | // interface |
To access the interface methods, the interface must be “implemented” (kinda like inherited) by another class with the implements
keyword (instead of extends
). The body of the interface method is provided by the “implement” class:
1 | // Interface |
Notes on Interfaces:
- Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an “Animal” object in the MyMainClass)
- Interface methods do not have a body - the body is provided by the “implement” class
- On implementation of an interface, you must override all of its methods
- Interface methods are by default
abstract
andpublic
- Interface attributes are by default
public
,static
andfinal
- An interface cannot contain a constructor (as it cannot be used to create objects)
Why And When To Use Interfaces?
To achieve security - hide certain details and only show the important details of an object (interface).
Java does not support “multiple inheritance” (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).
Lambda expression
Lambda Expressions were added in Java 8.
A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
Syntax
The simplest lambda expression contains a single parameter and an expression:
1 | parameter -> expression |
To use more than one parameter, wrap them in parentheses:
1 | (parameter1, parameter2) -> expression |
Expressions are limited. They have to immediately return a value, and they cannot contain variables, assignments or statements such as if
or for
. In order to do more complex operations, a code block can be used with curly braces. If the lambda expression needs to return a value, then the code block should have a return
statement.
1 | (parameter1, parameter2) -> { code block } |
Example
Use a lambda expression in the ArrayList
‘s forEach()
method to print every item in the list:
1 | import java.util.ArrayList; |
To use a lambda expression in a method, the method should have a parameter with a single-method interface as its type. Calling the interface’s method will run the lambda expression:
Example
Create a method which takes a lambda expression as a parameter:
1 | interface StringFunction { |
Double colon :: operator
They behave exactly as the lambda expressions. The only difference it has from lambda expressions is that this uses direct reference to the method by name instead of providing a delegate to the method.
Syntax:
1 | <Class name>::<method name> |
Example: To print all elements of the stream:
Using Lambda expression:
1
stream.forEach( s-> System.out.println(s));
1 | // Java code to print the elements of Stream |
Output:
1 | Geeks |
Using double colon operator:
1 | stream.forEach( System.out::println); |
When and how to use double colon operator?
Method reference or double colon operator can be used to refer:
- a static method,
- an instance method, or
- a constructor.
How to use method reference in Java:
Static method
Syntax:
1
(ClassName::methodName)
Example:
1
SomeClass::someStaticMethod
1 | // Java code to show use of double colon operator |
Output:
1 | Geeks |
- Instance method
Syntax:
1 | (objectOfClass::methodName) |
Example:
1 | System.out::println |
Program:
1 | // Java code to show use of double colon operator |
Output:
1 | Geeks |
https://www.geeksforgeeks.org/double-colon-operator-in-java/
Leetcode
1. Two Sum
Easy
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have *exactly* one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
1 | Input: nums = [2,7,11,15], target = 9 |
Example 2:
1 | Input: nums = [3,2,4], target = 6 |
Example 3:
1 | Input: nums = [3,3], target = 6 |
🌟Coding:
1 | class Solution { |
49. Group Anagrams
Medium
Given an array of strings strs
, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
1 | Input: strs = ["eat","tea","tan","ate","nat","bat"] |
Example 2:
1 | Input: strs = [""] |
Example 3:
1 | Input: strs = ["a"] |
🌟Coding:
1 | class Solution { |
⭕ For List, it’s addAll;
For HashMap, it’s putAll
⭕ List<List
⭕ It’s Arrays.toString(hash), instead of hash.Arrays.toString()
1 | map.computeIfAbsent(key, k -> new ArrayList<>()); |
the above is equal to the following:
1 | if(! map.containsKey(mid)){ |
347. Top K Frequent Elements
Medium
Given an integer array nums
and an integer k
, return the k
most frequent elements. You may return the answer in any order.
Example 1:
1 | Input: nums = [1,1,1,2,2,3], k = 2 |
Example 2:
1 | Input: nums = [1], k = 1 |
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
k
is in the range[1, the number of unique elements in the array]
.- It is guaranteed that the answer is unique.
Follow up: Your algorithm’s time complexity must be better than O(n log n)
, where n is the array’s size.
1 | class Solution3 { |
⭕ The syntax of the merge()
method is:
1 | hashmap.merge(key, value, BiFunction remappingFunction) |
Parameters: This method accepts three parameters:
- Key: which is the key for which we have a particular value. If two keys have the same value they are merged.
- Value: which is the index corresponding to the particular key which is stored in the bucket.
- BiFunction: which is the function having two arguments to be used for calculating the new mapping from the old value and given value.
Return Value: This method returns the key along with its value if the key is not present or is associated with null. Else if the key already holds any value, it merges the old value with the new value using the mapping technique.
Array:
704. Binary Search
Easy
Given an array of integers nums
which is sorted in ascending order, and an integer target
, write a function to search target
in nums
. If target
exists, then return its index. Otherwise, return -1
.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
1 | Input: nums = [-1,0,3,5,9,12], target = 9 |
1 | class Solution { |
Wrong version:
1 | class Solution { |
leedcode master:
!!!!!!!!!!!!!!!!! 二分法第1种写法
我们定义 target 是在一个在左闭右闭的区间里,也就是[left, right] (这个很重要非常重要)。
区间的定义这就决定了二分法的代码应该如何写,因为定义target在[left, right]区间,所以有如下两点:
- while (left <= right) 要使用 <= ,因为left == right是有意义的,所以使用 <=
- if (nums[middle] > target) right 要赋值为 middle - 1,因为当前这个nums[middle]一定不是target,那么接下来要查找的左区间结束下标位置就是 middle - 1
左闭右闭区间
1 | class Solution { |
!!!!!!!!!!!!!!!!! 二分法第二种写法
如果说定义 target 是在一个在左闭右开的区间里,也就是[left, right) ,那么二分法的边界处理方式则截然不同。
有如下两点:
- while (left < right),这里使用 < ,因为left == right在区间[left, right)是没有意义的
- if (nums[middle] > target) right 更新为 middle,因为当前nums[middle]不等于target,去左区间继续寻找,而寻找区间是左闭右开区间,所以right更新为middle,即:下一个查询区间不会去比较nums[middle]
27. Remove Element
Easy
Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The order of the elements may be changed. Then return the number of elements in nums
which are not equal to val
.
Consider the number of elements in nums
which are not equal to val
be k
, to get accepted, you need to do the following things:
- Change the array
nums
such that the firstk
elements ofnums
contain the elements which are not equal toval
. The remaining elements ofnums
are not important as well as the size ofnums
. - Return
k
.
Example 1:
1 | Input: nums = [3,2,2,3], val = 3 |
1 | class Solution { |
844. Backspace String Compare
Easy
Given two strings s
and t
, return true
if they are equal when both are typed into empty text editors. '#'
means a backspace character.
Note that after backspacing an empty text, the text will continue empty.
Example 1:
1 | Input: s = "ab#c", t = "ad#c" |
wrong version:
1 | class Solution { |
correct:
- two pointers:
1 | class Solution { |
stack:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// string builder
class Solution {
public boolean backspaceCompare(String s, String t) {
StringBuilder ssb = new StringBuilder(); // 模拟栈
StringBuilder tsb = new StringBuilder(); // 模拟栈
// 分别处理两个 String
for (char c : s.toCharArray()) {
if (c != '#') {
ssb.append(c); // 模拟入栈
} else if (ssb.length() > 0){ // 栈非空才能弹栈
ssb.deleteCharAt(ssb.length() - 1); // 模拟弹栈
}
}
for (char c : t.toCharArray()) {
if (c != '#') {
tsb.append(c); // 模拟入栈
} else if (tsb.length() > 0){ // 栈非空才能弹栈
tsb.deleteCharAt(tsb.length() - 1); // 模拟弹栈
}
}
return ssb.toString().equals(tsb.toString());
}
}
// stack
class Solution {
public boolean backspaceCompare(String s, String t) {
return buildStack(s).equals(buildStack(t));
}
private Stack<Character> buildStack(String str) {
Stack<Character> stack = new Stack<>();
for (char c : str.toCharArray()) {
if (c != '#') {
stack.push(c);
} else if (!stack.isEmpty()) {
stack.pop();
}
}
return stack;
}
}977. Squares of a Sorted Array
Easy
Given an integer array
nums
sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.Example 1:
1
2
3
4Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74easy way:
class Solution {
public int[] sortedSquares(int[] nums) {
for(int i = 0; i < nums.length; i++){
nums[i] = nums[i] * nums[i];
}
Arrays.sort(nums);
return nums;
}
}
two pointers:
//leetcode master version
class Solution {
public int[] sortedSquares(int[] nums) {
int left = 0;
int right = nums.length - 1;
int index = nums.length - 1;
int[] result = new int[nums.length];
while(left <= right){
if(nums[left] * nums[left] < nums[right] * nums[right]){
result[index] = nums[right] * nums[right];
index--;
right --;
}else{
result[index] = nums[left] * nums[left];
index--;
left ++;
}
}
return result;
;
}
}
// my verison
class Solution {
public int[] sortedSquares(int[] nums) {
int[] res = new int[nums.length];
int left = 0 ;
int right = nums.length - 1;
for(int i = nums.length - 1 ; i >= 0; i--){
if(nums[left]*nums[left] <= nums[right]*nums[right]){
res[i] = nums[right]*nums[right];
right --;
}else{
res[i] = nums[left]*nums[left];
left ++;
}
}
return res;
}
}
two pointer(Wrong):
class Solution {
public int[] sortedSquares(int[] nums) {
int[] res = new int[nums.length];
int left = 0 ;
int right = nums.length - 1;
for(int i = 0 ; i < res.length; i++){
if(nums[left]*nums[left] <= nums[right]*nums[right]){
res[i] = nums[left]*nums[left];
left ++;
}else{
res[i] = nums[right]*nums[right];
right --;
}
}
return res;
}
}双指针法
数组其实是有序的, 只不过负数平方之后可能成为最大数了。
那么数组平方的最大值就在数组的两端,不是最左边就是最右边,不可能是中间。
此时可以考虑双指针法了,i指向起始位置,j指向终止位置。
定义一个新数组result,和A数组一样的大小,让k指向result数组终止位置。
如果
A[i] * A[i] < A[j] * A[j]
那么result[k--] = A[j] * A[j];
。如果
A[i] * A[i] >= A[j] * A[j]
那么result[k--] = A[i] * A[i];
。
209. Minimum Size Subarray Sum
- sliding window
Medium
Given an array of positive integers nums
and a positive integer target
, return the minimal length of a
subarray
whose sum is greater than or equal to target
. If there is no such subarray, return 0
instead.
Example 1:
1 | Input: target = 7, nums = [2,3,1,2,4,3] |
1 | 不要以为for里放一个while就以为是O(n^2)啊, 主要是看每一个元素被操作的次数,每个元素在滑动窗后进来操作一次,出去操作一次,每个元素都是被操作两次,所以时间复杂度是 2 × n 也就是O(n)。 |
okay, so for sliding window, you move the right border first, once the sum is bigger than the target, then you move the left border until the sum is smaller than the target. once the sum is smaller, then once again move the right border.
这里还是以题目中的示例来举例,s=7, 数组是 2,3,1,2,4,3,来看一下查找的过程:
59. Spiral Matrix II
Medium
Given a positive integer n
, generate an n x n
matrix
filled with elements from 1
to n2
in spiral order.
Example 1:
1 | Input: n = 3 |
1 | // leetcode master |
[toc]