数组题总结
hot100
无重复字符的最长子串
题目链接:
3.无重复字符的最长子串
代码:
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0) return 0;
Map<Character,Integer> map = new HashMap<Character,Integer>();
int max = 0;
int left = 0;
for (int i = 0; i < s.length();i ++)
{
if (map.containsKey(s.charAt(i)))
{
left = Math.max(left,map.get(s.charAt(i)) + 1);
}
map.put(s.charAt(i),i);
max = Math.max(max,i - left + 1);
}
return max;
}
}
找到字符串中所有字母异位词
题目链接:
438.找到字符串中所有字母异位词
代码:
class Solution {
public List<Integer> findAnagrams(String s, String p) {
int m = s.length(), n = p.length();
if (m < n) return new ArrayList<>();
int[] hash = new int[26];
List<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i ++){
hash[p.charAt(i) - 'a'] ++;
}
int left = 0;
for (int right = 0; right < m; right ++){
hash[s.charAt(right) - 'a'] --;
while (hash[s.charAt(right) - 'a'] < 0){
hash[s.charAt(left) - 'a'] ++;
left ++;
}
if (right - left + 1 == n){
res.add(left);
}
}
return res;
}
}
和为k的子数组
题目链接:
560.和为k的子数组
代码:
class Solution {
public int subarraySum(int[] nums, int k) {
int n = nums.length;
Map<Integer,Integer> map = new HashMap<>();
int pre = 0;
int res = 0;
map.put(0,1);
for (int i = 0; i < n; i ++)
{
pre += nums[i];
if (map.containsKey(pre - k))
{
res += map.get(pre - k);
}
map.put(pre,map.getOrDefault(pre,0) + 1);
}
return res;
}
}
滑动窗口最大值
题目链接:
239.滑动窗口最大值
代码:
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> queue = new LinkedList<>();
int n = nums.length;
int[] res = new int[n - k + 1];
for (int j = 0, i = 1 - k; j < n; i ++, j ++) {
if (i > 0 && queue.peekFirst() == nums[i - 1]) {
queue.removeFirst();
}
while (! queue.isEmpty() && queue.peekLast() < nums[j]) {
queue.removeLast();
}
queue.addLast(nums[j]);
if (i >= 0) {
res[i] = queue.peekFirst();
}
}
return res;
}
}
最大子数组和
题目链接:
53.最大子数组和
代码:
class Solution {
public int maxSubArray(int[] nums) {
int res = Integer.MIN_VALUE;
int count = 0;
for (int i = 0; i < nums.length; i ++){
count += nums[i];
res = Math.max(res,count);
if (count <= 0) count = 0;
}
return res;
}
}
合并区间
题目链接:
56.合并区间
代码:
class Solution {
public int[][] merge(int[][] intervals) {
if (intervals.length == 0) return new int[0][2];
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
List<int []> res = new ArrayList<>();
for (int i = 0; i < intervals.length; i ++)
{
int L = intervals[i][0], R = intervals[i][1];
if (res.size() == 0 || res.get(res.size() - 1)[1] < L)
{
res.add(new int[] {L,R});
}
else
{
res.get(res.size() - 1)[1] = Math.max(res.get(res.size() - 1)[1],R);
}
}
return res.toArray(new int[res.size()][]);
}
}
轮转数组
题目链接:
189.轮转数组
代码:
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
int[] newNums = new int[n];
for (int i = 0; i < n; i ++)
{
newNums[(i + k) % n] = nums[i];
}
System.arraycopy(newNums,0,nums,0,n);
}
}
除自身以外数组的乘积
题目链接:
238.除自身以外数组的乘积
代码:
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] f = new int[n];
int[] b = new int[n];
int[] res = new int[n];
f[0] = 1;
b[n - 1] = 1;
for (int i = 1; i < n; i ++)
{
f[i] = f[i - 1] * nums[i - 1];
}
for (int i = n - 2; i >= 0; i --)
{
b[i] = b[i + 1] * nums[i + 1];
}
for (int i = 0; i < n; i ++)
{
res[i] = f[i]*b[i];
}
return res;
}
}
缺失的第一个正数
题目链接:
41.缺失的第一个正数
代码:
class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; i ++)
{
if (nums[i] <= 0) nums[i] = n + 1;
}
for (int i = 0; i < n; i ++)
{
int num = Math.abs(nums[i]);
if (num <= n)
{
nums[num - 1] = -Math.abs(nums[num - 1]);
}
}
for (int i = 0; i < n; i ++)
{
if (nums[i] > 0) return i + 1;
}
return n + 1;
}
}
矩阵置零
题目链接:
73.矩阵置零
代码:
class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
for (int i = 0; i < m; i ++)
{
for (int j = 0; j < n; j ++)
{
if (matrix[i][j] == 0)
{
row[i] = true;
col[j] = true;
}
}
}
for (int i = 0; i < m; i ++)
{
for (int j = 0; j < n; j ++)
{
if (row[i] || col[j])
{
matrix[i][j] = 0;
}
}
}
}
}
螺旋矩阵
题目链接:
54.螺旋矩阵
代码:
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
int u = 0, d = matrix.length - 1;
int l = 0, r = matrix[0].length - 1;
while(true){
for (int i = l; i <= r; i ++){
res.add(matrix[u][i]);
}
if (++u > d) break;
for (int i = u; i <= d; i ++){
res.add(matrix[i][r]);
}
if (--r < l) break;
for (int i = r; i >= l; i --){
res.add(matrix[d][i]);
}
if (--d < u) break;
for (int i = d; i >= u; i --){
res.add(matrix[i][l]);
}
if (++l > r) break;
}
return res;
}
}
旋转图像
题目链接:
48.旋转图像
代码:
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// 水平翻转
for (int i = 0; i < n / 2; i ++)
{
for (int j = 0; j < n; j ++)
{
int tmp = matrix[i][j];
matrix[i][j] = matrix[n - i - 1][j];
matrix[n - i - 1][j] = tmp;
}
}
// 主对角线翻转
for (int i = 0; i < n; i ++)
{
for (int j = 0; j < i; j ++)
{
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
}
}
搜素二维矩阵2
题目链接:
240.搜素二维矩阵2
代码:
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
int m = matrix.length, n = matrix[0].length;
int row = 0, col = n - 1;
while (row < m && col >= 0)
{
if (target == matrix[row][col]) return true;
else if (target < matrix[row][col]) col --;
else row ++;
}
return false;
}
}