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

283. Move Zeroes (Easy)

Leetcode / 力扣

  1. class Solution {
  2. public void moveZeroes(int[] nums) {
  3. int id=0;
  4. for(int num:nums){
  5. if(num!=0)nums[id++]=num;
  6. }
  7. while(id<nums.length){
  8. nums[id++]=0;
  9. }
  10. }
  11. }

跨计算节点

2. 改变矩阵维度

566. Reshape the Matrix (Easy)

Leetcode / 力扣

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

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

485. Max Consecutive Ones (Easy)

Leetcode / 力扣

  1. class Solution {
  2. public int findMaxConsecutiveOnes(int[] nums) {
  3. int res=0;
  4. int count=0;
  5. for(int i=0;i<nums.length;i++){
  6. if(nums[i]==1){
  7. count++;
  8. res=Math.max(res,count);
  9. }
  10. else count=0;
  11. }
  12. return res;
  13. }
  14. }

4. 有序矩阵查找

240. Search a 2D Matrix II (Medium)

Leetcode / 力扣

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

5. 有序矩阵的 Kth Element

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

Leetcode / 力扣

  1. class Solution {
  2. public int kthSmallest(int[][] matrix, int k) {
  3. int m=matrix.length,n=matrix[0].length;
  4. int lo=matrix[0][0],hi=matrix[m-1][n-1];
  5. while(lo<=hi){
  6. int mid=lo+(hi-lo)/2;
  7. int cnt=0;
  8. for(int i=0;i<m;i++){
  9. for(int j=0;j<n && matrix[i][j]<=mid;j++){
  10. cnt++;
  11. }
  12. }
  13. if(cnt<k)lo=mid+1;
  14. else hi=mid-1;
  15. }
  16. return lo;
  17. }
  18. }

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

645. Set Mismatch (Easy)

Leetcode / 力扣

  1. class Solution {
  2. public int[] findErrorNums(int[] nums) {
  3. HashMap<Integer,Integer>map=new HashMap<>();
  4. int[] arr=new int[2];
  5. for(int n:nums){
  6. map.put(n,map.getOrDefault(n,0)+1);
  7. }
  8. for(int i=1;i<=nums.length;i++){
  9. if(map.containsKey(i)){
  10. if(map.get(i)==2)arr[0]=i;
  11. }
  12. else arr[1]=i;
  13. }
  14. return arr;
  15. }
  16. }

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

287. Find the Duplicate Number (Medium)

Leetcode / 力扣

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

二分查找解法:

  1. class Solution {
  2. public int findDuplicate(int[] nums) {
  3. int l=1,h=nums.length-1;
  4. while(l<=h){
  5. int mid=l+(h-l)/2;
  6. int cnt=0;
  7. for(int i=0;i<nums.length;i++){
  8. if(nums[i]<=mid)cnt++;
  9. }
  10. if(cnt>mid)h=mid-1;
  11. else l=mid+1;
  12. }
  13. return l;
  14. }
  15. }

8. 数组相邻差值的个数

667. Beautiful Arrangement II (Medium)

Leetcode / 力扣

  1. class Solution {
  2. public int[] constructArray(int n, int k) {
  3. int[] res=new int[n];
  4. int numk=k+1,numt=1;
  5. //下标段[0,k]中,偶数填充[1、2、3...]
  6. for(int i=0;i<=k;i+=2){
  7. res[i]=numt++;
  8. }
  9. //下标段[0,k]中,奇数下标填充[k+1,k,k-1...]
  10. for(int i=1;i<=k;i+=2){
  11. res[i]=numk--;
  12. }
  13. //其余的部分顺序填充
  14. for(int i=k+1;i<n;i++){
  15. res[i]=i+1;
  16. }
  17. return res;
  18. }
  19. }

9. 数组的度

697. Degree of an Array (Easy)

Leetcode / 力扣

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

10. 对角元素相等的矩阵

766. Toeplitz Matrix (Easy)

Leetcode / 力扣

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

11. 嵌套数组

565. Array Nesting (Medium)

Leetcode / 力扣

  1. class Solution {
  2. public int arrayNesting(int[] nums) {
  3. int max=0;
  4. for(int i=0;i<nums.length;i++){
  5. int cnt=0;
  6. for(int j=i;nums[j]!=-1;){
  7. cnt++;
  8. int t=nums[j];
  9. nums[j]=-1; //标记该位置已经被访问
  10. j=t;
  11. }
  12. max=Math.max(max,cnt);
  13. }
  14. return max;
  15. }
  16. }

12. 分隔数组

769. Max Chunks To Make Sorted (Medium)

Leetcode / 力扣

  1. class Solution {
  2. public int maxChunksToSorted(int[] arr) {
  3. if(arr==null)return 0;
  4. int ret=0;
  5. int right=arr[0];
  6. for(int i=0;i<arr.length;i++){
  7. right=Math.max(right,arr[i]);
  8. if(right==i)ret++;
  9. }
  10. return ret;
  11. }
  12. }

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. java中synchronized与Lock的异同

    本文转载自java中synchronized与Lock的异同 前言 synchronized和Lock通过互斥保障原子性,能够保护共享数据以实现线程安全,其作用包括保障原子性.可见性.有序性 常见问题 ...

  2. git配置了公钥,在下载项目时为什么还要输入密码

    配置git地址:https://www.cnblogs.com/lz0925/p/10794616.html 原文链接:https://blog.csdn.net/xiaomengzi_16/arti ...

  3. Win32Api -- 关闭当前应用

    本文介绍Windows系统下使用Win32API获取当前应用并关闭的方法. 思路 使用EnumWindows接口枚举当前窗口; 过滤掉不可用.隐藏.最小化的窗口: 过滤掉子窗口: 通过标题.类名过滤掉 ...

  4. CentOS 7.7上配置mysql

    转载:https://www.cnblogs.com/VinsonYang/p/12333570.html 首先登陆到阿里云,进行远程连接,在这里我使用的是Xshell 6进行连接的. 参照https ...

  5. 利用Visual Studio调试JavaScript脚本

    方法1: 方法2: 打开IE,按F12调试. 方法3: JS断电点debugger代替

  6. .NET Core中的Worker Service

    当你想到ASP.NET Core时,可能会想到Web应用程序后端代码,包括MVC和WebAPI.MVC视图和Razor页面还允许使用后端代码生成带有HTML元素的前端UI.全新的Blazor更进一步, ...

  7. hiho一下 第195周 奖券兑换[C solution][Accepted]

    时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi在游乐园中获得了M张奖券,这些奖券可以用来兑换奖品. 可供兑换的奖品一共有N件.第i件奖品需要Wi张奖券才能兑换到, ...

  8. Linux:使用systemd管理进程

    Blog:博客园 个人 概述 systemd是目前Linux系统上主要的系统守护进程管理工具,由于init一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面init也仅仅是执行启动脚本,并不能 ...

  9. jdk8的安装与环境搭建

    jdk8的安装与环境搭建 jdk8下载网址:https://www.oracle.com/cn/java/technologies/javase/javase-jdk8-downloads.html ...

  10. django框架如何解决跨域问题

    跨域问题的由来 由于浏览器具有同源策略的限制. 限制:在发送Ajax请求时,如果当前浏览器的URL是a.com,页面中向b.com发送Ajax请求,请求可以正常访问,但数据回到浏览器时,浏览器会阻止. ...