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. Kyle Tedford :人,一定要懂得转弯

    每个人都渴望成功,但成功之路不仅仅只有一条. 有的时候,有一条路人满为患,每个人都挤破脑袋想要过去,然而能过去者,却寥寥无几.但有的人,却知道适时转弯,在新的道路上,摸索前进,最终通往成功. 最近,星 ...

  2. JDK源码阅读-FileOutputStream

    本文转载自JDK源码阅读-FileOutputStream 导语 FileOutputStream用户打开文件并获取输出流. 打开文件 public FileOutputStream(File fil ...

  3. java实现压缩文件

    原文链接:https://www.cnblogs.com/zeng1994/p/7862288.html

  4. Java基础语法:变量与常量

    一.命名规范 所有变量.常量.方法.类 都使用英文单词 命名,要见名知意. 所有变量.方法 的命名都使用小驼峰法 :首字母小写的驼峰命名法.例如:sampleText 类 的命名都使用大驼峰法 :首字 ...

  5. Hive实现自增序列及常见的Hive元数据问题处理

    Hive实现自增序列 在利用数据仓库进行数据处理时,通常有这样一个业务场景,为一个Hive表新增一列自增字段(比如事实表和维度表之间的"代理主键").虽然Hive不像RDBMS如m ...

  6. Wireguard 全互联模式(full mesh)配置指南

    上篇文章给大家介绍了如何使用 wg-gen-web 来方便快捷地管理 WireGuard 的配置和秘钥,文末埋了两个坑:一个是 WireGuard 的全互联模式(full mesh),另一个是使用 W ...

  7. 关于 JMeter 5.4.1 的一点记录

    APACHE JMeter table { border: 0; border-collapse: collapse; background-color: rgba(255, 245, 218, 1) ...

  8. CCF(棋局评估)博弈论+对抗搜索+DFS

    201803-4 棋局评估 这题主要使用对抗搜索,也就是每一步寻找可以下棋的位置,通过在这一步下棋看最后会取的什么样的分数. #include<iostream> #include< ...

  9. 如何快速开发Winform应用系统

    在实际的业务中,往往还有很多需要使用Winform来开发应用系统的,如一些HIS.MIS.MES等系统,由于Winform开发出来的系统界面友好,响应快速,开发效率高等各方面原因,还有一些原因是独立的 ...

  10. Hadoop hdfs副本存储和纠删码(Erasure Coding)存储优缺点

    body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...