哈希映射
由两数之和到哈希映射
两数之和:
给定一个整数数组
n
u
m
s
nums
nums 和一个整数目标值
t
a
r
g
e
t
target
target,请你在该数组中找出和为目标值
t
a
r
g
e
t
target
target 的那 两个 整数,并返回它们的数组下标。(你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。)
例:
input: nums = [2,7,11], target = 9
output: [0,1]
暴力遍历的时间复杂度为
O
(
n
2
)
O(n^2)
O(n2)
注意到题目中数组中不存在重复元素,问题可以转换为查找某一元素设否出现过
哈希表:根据键(Key)而直接访问在存储器存储位置的数据结构
基本语法:
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
hashtable.cantainsKey(int key);
hashtable.get(int key);
hashtable.put(int key, int value);
储存示意:
| key | 2 | 7 | 11 |
|---|---|---|---|
| value | 0 | 1 | 2 |
代码实现:
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
}
动态规划
从最大数组和到动态规划
最大数组和:
给你一个整数数组
n
u
m
s
nums
nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。(子数组 是数组中的一个连续部分。)
例
input: nums = [-2,1-3,4,-1,2,1,-5,4]
output: 6
暴力解法,考虑到一般情况下两端为整数,但超出时间限制
动态规划要找到子问题的无后效性,有点像数学归纳法
子问题:
以第
i
i
i个数结尾的连续子数组的最大和,记为
f
(
i
)
f(i)
f(i),即,
m
a
x
i
∈
[
0
,
n
−
1
]
f
(
i
)
max_{i \in [0,n-1]} {f(i)}
maxi∈[0,n−1]f(i)
转移方程:
f
(
i
)
=
m
a
x
{
f
(
i
−
1
)
+
n
u
m
s
[
i
]
,
n
u
m
s
[
i
]
}
f(i)=max\{f(i-1)+nums[i],nums[i] \}
f(i)=max{f(i−1)+nums[i],nums[i]}
在本题中只需定义一个变量保存所出现的最大和的连续数组
代码实现:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int pre = 0, maxAns = nums[0];
for (const auto &x: nums) {
pre = max(pre + x, x);
maxAns = max(maxAns, pre);
}
return maxAns;
}
};
java中的列表
从合并区间到List类
List实现
//List 的ArrayList 实现
List<String> list1 = new ArrayList<>();
// List 的LinkedList 实现
List<String> list2 = new LinkedList<>();
合并区间:
以数组
i
n
t
e
r
v
a
l
s
intervals
intervals 表示若干个区间的集合,其中单个区间为
i
n
t
e
r
v
a
l
s
[
i
]
=
[
s
t
a
r
t
i
,
e
n
d
i
]
intervals[i] = [start_i, end_i]
intervals[i]=[starti,endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。
例
input: intervals = [[1,3],[2,6],[8,10],[15,18]]
output: [[1,6],[8,10],[15,18]]
算法
我们用数组 merged 存储最终的答案。
首先,我们将列表中的区间按照左端点升序排序。然后我们将第一个区间加入 merged 数组中,并按顺序依次考虑之后的每个区间:
如果当前区间的左端点在数组 merged 中最后一个区间的右端点之后,那么它们不会重合,我们可以直接将这个区间加入数组 merged 的末尾;
否则,它们重合,我们需要用当前区间的右端点更新数组 merged 中最后一个区间的右端点,将其置为二者的较大值。
所用到的List相关语句:
//数组按照左端点排序
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
//新建一个储存数组的列表
List<int[]> merged = new ArrayList<int[]>();
//在列表末尾增添元素
merged.add(new int[]{L, R});
//计算列表大小
merged.size();
//按照索引取值
merged.get(merged.size() - 1)[1];
//将列表转换为数组
merged.toArray(new int[merged.size()][]);
常用List方法
List接口包括Collection接口的所有方法。 这是因为Collection是List的超级接口。
Collection接口中还提供了一些常用的List接口方法:
add() - 将元素添加到列表
addAll() - 将一个列表的所有元素添加到另一个
get() - 有助于从列表中随机访问元素
iterator() - 返回迭代器对象,该对象可用于顺序访问列表的元素
set() - 更改列表的元素
remove() - 从列表中删除一个元素
removeAll() - 从列表中删除所有元素
clear() - 从列表中删除所有元素(比removeAll()效率更高)
size() - 返回列表的长度
toArray() - 将列表转换为数组
contains() - 如果列表包含指定的元素,则返回true
参考链接
https://www.cainiaojc.com/java/java-list.html
https://leetcode.cn/problems/two-sum/
https://leetcode.cn/problems/maximum-subarray/
https://leetcode.cn/problems/merge-intervals/
1630




被折叠的 条评论
为什么被折叠?



