1. 把数组中的 0 移到末尾

283. Move Zeroes (Easy)

Leetcode / 力扣

class Solution {
public void moveZeroes(int[] nums) {
int id=0;
for(int num:nums){
if(num!=0)nums[id++]=num;
}
while(id<nums.length){
nums[id++]=0;
}
}
}

跨计算节点

2. 改变矩阵维度

566. Reshape the Matrix (Easy)

Leetcode / 力扣

class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int m=nums.length;
int n=nums[0].length;
if(m*n!=r*c)return nums;
int[][] res=new int[r][c];
int idx=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
res[i][j]=nums[idx/n][idx%n];
idx++;
}
}
return res;
}
}

3. 找出数组中最长的连续 1

485. Max Consecutive Ones (Easy)

Leetcode / 力扣

class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int res=0;
int count=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==1){
count++;
res=Math.max(res,count);
}
else count=0;
}
return res;
}
}

4. 有序矩阵查找

240. Search a 2D Matrix II (Medium)

Leetcode / 力扣

[
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
]
class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int m=nums.length;
int n=nums[0].length;
if(m*n!=r*c)return nums;
int[][] res=new int[r][c];
int idx=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
res[i][j]=nums[idx/n][idx%n];
idx++;
}
}
return res;
}
}

5. 有序矩阵的 Kth Element

378. Kth Smallest Element in a Sorted Matrix ((Medium))

Leetcode / 力扣

class Solution {
public int kthSmallest(int[][] matrix, int k) {
int m=matrix.length,n=matrix[0].length;
int lo=matrix[0][0],hi=matrix[m-1][n-1];
while(lo<=hi){
int mid=lo+(hi-lo)/2;
int cnt=0;
for(int i=0;i<m;i++){
for(int j=0;j<n && matrix[i][j]<=mid;j++){
cnt++;
}
}
if(cnt<k)lo=mid+1;
else hi=mid-1;
}
return lo;
}
}

6. 一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出重复的数和丢失的数

645. Set Mismatch (Easy)

Leetcode / 力扣

class Solution {
public int[] findErrorNums(int[] nums) {
HashMap<Integer,Integer>map=new HashMap<>();
int[] arr=new int[2];
for(int n:nums){
map.put(n,map.getOrDefault(n,0)+1);
}
for(int i=1;i<=nums.length;i++){
if(map.containsKey(i)){
if(map.get(i)==2)arr[0]=i;
}
else arr[1]=i;
}
return arr;
}
}

7. 找出数组中重复的数,数组值在 [1, n] 之间

287. Find the Duplicate Number (Medium)

Leetcode / 力扣

要求不能修改数组,也不能使用额外的空间。

二分查找解法:

class Solution {
public int findDuplicate(int[] nums) {
int l=1,h=nums.length-1;
while(l<=h){
int mid=l+(h-l)/2;
int cnt=0;
for(int i=0;i<nums.length;i++){
if(nums[i]<=mid)cnt++;
}
if(cnt>mid)h=mid-1;
else l=mid+1;
}
return l;
}
}

8. 数组相邻差值的个数

667. Beautiful Arrangement II (Medium)

Leetcode / 力扣

class Solution {
public int[] constructArray(int n, int k) {
int[] res=new int[n];
int numk=k+1,numt=1;
//下标段[0,k]中,偶数填充[1、2、3...]
for(int i=0;i<=k;i+=2){
res[i]=numt++;
}
//下标段[0,k]中,奇数下标填充[k+1,k,k-1...]
for(int i=1;i<=k;i+=2){
res[i]=numk--;
}
//其余的部分顺序填充
for(int i=k+1;i<n;i++){
res[i]=i+1;
}
return res;
}
}

9. 数组的度

697. Degree of an Array (Easy)

Leetcode / 力扣

Input: [1,2,2,3,1,4,2]
Output: 6
class Solution {
public int findShortestSubArray(int[] nums) {
Map<Integer,Integer> left=new HashMap<>(),right=new HashMap<>(),count=new HashMap<>();
//一个map记录左边的节点,一个map记录右边的节点,利用count进行计数
for(int i=0;i<nums.length;i++){
int x=nums[i];
if(left.get(x)==null)left.put(x,i);
right.put(x,i);
count.put(x,count.getOrDefault(x,0)+1);
} int ans=nums.length;
int degree=Collections.max(count.values());
for(int x: count.keySet()){
if(count.get(x)==degree){
ans=Math.min(ans,right.get(x)-left.get(x)+1);
}
}
return ans;
}
}

10. 对角元素相等的矩阵

766. Toeplitz Matrix (Easy)

Leetcode / 力扣

1234
5123
9512
class Solution {
public boolean isToeplitzMatrix(int[][] matrix) {
for(int i=0;i<matrix.length-1;i++){
for(int j=0;j<matrix[0].length-1;j++){
if(matrix[i][j]!=matrix[i+1][j+1]){
return false;
}
}
}
return true;
}
}
  1. 如果矩阵存储在磁盘上,并且磁盘内存是有限的,因此一次最多只能将一行矩阵加载到内存中,该怎么办?
  2. 如果矩阵太大以至于只能一次将部分行加载到内存中,该怎么办?

11. 嵌套数组

565. Array Nesting (Medium)

Leetcode / 力扣

class Solution {
public int arrayNesting(int[] nums) {
int max=0;
for(int i=0;i<nums.length;i++){
int cnt=0;
for(int j=i;nums[j]!=-1;){
cnt++;
int t=nums[j];
nums[j]=-1; //标记该位置已经被访问
j=t;
}
max=Math.max(max,cnt);
}
return max;
}
}

12. 分隔数组

769. Max Chunks To Make Sorted (Medium)

Leetcode / 力扣

class Solution {
public int maxChunksToSorted(int[] arr) {
if(arr==null)return 0;
int ret=0;
int right=arr[0];
for(int i=0;i<arr.length;i++){
right=Math.max(right,arr[i]);
if(right==i)ret++;
}
return ret;
}
}

Leedcode算法专题训练(数组与矩阵)的更多相关文章

  1. Leedcode算法专题训练(排序)

    排序 快速排序 用于求解 Kth Element 问题,也就是第 K 个元素的问题. 可以使用快速排序的 partition() 进行实现.需要先打乱数组,否则最坏情况下时间复杂度为 O(N2). 堆 ...

  2. Leedcode算法专题训练(搜索)

    BFS 广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点.需要注意的是,遍历过的节点不能再次被遍历. 第一层: 0 -> {6,2,1,5} ...

  3. Leedcode算法专题训练(动态规划)

    递归和动态规划都是将原问题拆成多个子问题然后求解,他们之间最本质的区别是,动态规划保存了子问题的解,避免重复计算. 斐波那契数列 1. 爬楼梯 70. Climbing Stairs (Easy) L ...

  4. Leedcode算法专题训练(分治法)

    归并排序就是一个用分治法的经典例子,这里我用它来举例描述一下上面的步骤: 1.归并排序首先把原问题拆分成2个规模更小的子问题. 2.递归地求解子问题,当子问题规模足够小时,可以一下子解决它.在这个例子 ...

  5. Leedcode算法专题训练(二分查找)

    二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...

  6. Leedcode算法专题训练(贪心)

    1. 分配饼干 455. 分发饼干 题目描述:每个孩子都有一个满足度 grid,每个饼干都有一个大小 size,只有饼干的大小大于等于一个孩子的满足度,该孩子才会获得满足.求解最多可以获得满足的孩子数 ...

  7. Leedcode算法专题训练(双指针)

    算法思想 双指针 167. 两数之和 II - 输入有序数组 双指针的典型用法 如果两个指针指向元素的和 sum == target,那么得到要求的结果: 如果 sum > target,移动较 ...

  8. Leedcode算法专题训练(位运算)

    https://www.cnblogs.com/findbetterme/p/10787118.html 看这个就完事了 1. 统计两个数的二进制表示有多少位不同 461. Hamming Dista ...

  9. Leedcode算法专题训练(哈希表)

    Java 中的 HashSet 用于存储一个集合,可以查找元素是否在集合中.如果元素有穷,并且范围不大,那么可以用一个布尔数组来存储一个元素是否存在.例如对于只有小写字符的元素,就可以用一个长度为 2 ...

随机推荐

  1. Angular性能优化实践——巧用第三方组件和懒加载技术

    应该有很多人都抱怨过 Angular 应用的性能问题.其实,在搭建Angular项目时,通过使用打包.懒加载.变化检测策略和缓存技术,再辅助第三方组件,便可有效提升项目性能. 为了帮助开发者深入理解和 ...

  2. 远程过程调用框架——gRPC

    gRPC是一款基于http协议的远程过程调用(RPC)框架.出自google.这个框架可以用来相对简单的完成如跨进程service这样的需求开发. 资料参考: https://blog.csdn.ne ...

  3. redis源码之SDS

    1:SDS介绍 我们在redis中执行命令 set key name 的时候,key和name都是字符串类型,而且字符串(string)在redis中是会经常用到的类型,那redis是如何保存字符串的 ...

  4. HTTP状态响应码解析

    # HTTP响应状态码 ## 1xx:临时响应 #### 表示临时响应并需要请求者继续执行操作的状态代码. 100 **继续**请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等 ...

  5. 从HashMap面试聊聊互联网内卷

    微信公众号:大黄奔跑 关注我,可了解更多有趣的面试相关问题. 写在之前 毫无疑问,回想2020年有什么词出现在眼前最多的,无疑是"996"和"内卷",从马老师的 ...

  6. POJ1852-换向思考

    蚂蚁碰撞后反向与穿越的时间一样. 穷竭搜索---->想象力 #include<stdio.h> int main(void){ int n,len,ansNum,mintime,ma ...

  7. MHA架构搭建中遇到的问题

    1. 两个包:mha4mysql-manager-0.56-0.el6.noarch.rpm 和 mha4mysql-node-0.56-0.el6.norch.rpm 地址:https://code ...

  8. 01.从0实现一个JVM语言之架构总览

    00.一个JVM语言的诞生过程 文章集合以及项目展望 源码github地址 这一篇将是架构总览, 将自顶向下地叙述自制编译器的要素; 文章目录 01.从0实现一个JVM语言之架构总览 架构总览目前完成 ...

  9. 微信小程序一周时间表

    <view class="dateView"> <image class="dateLeft" bindtap="prevWeek& ...

  10. Java 常见对象 03

    常见对象·StringBuffer类 StringBuffer类概述 * A:StringBuffer类概述 * 通过 JDk 提供的API,查看StringBuffer类的说明 * 线程安全的可变字 ...