package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: MatrixReshape
* @Author: xiaof
* @Description: TODO 566. Reshape the Matrix
*
* In MATLAB, there is a very useful function called 'reshape',
* which can reshape a matrix into a new one with different size but keep its original data.
* You're given a matrix represented by a two-dimensional array,
* and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
* The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
* If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise,
* output the original matrix.
*
* Input:
* nums =
* [[1,2],
* [3,4]]
* r = 1, c = 4
* Output:
* [[1,2,3,4]]
* Explanation:
* The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
*
* @Date: 2019/7/8 9:03
* @Version: 1.0
*/
public class MatrixReshape { public int[][] solution(int[][] nums, int r, int c) {
//吧对应的数组输出为新的矩阵数组,如果不够那么直接输出原来数组
if(nums[0].length * nums.length != (r * c)) {
return nums;
}
int[][] result = new int[r][c];
int indexNum = 0;
//依次遍历数组
for(int i = 0; i < nums.length; ++i) {
for(int j = 0; j < nums[i].length; ++j) {
//遍历所有
int curNum = i * nums[i].length + j;
result[curNum / c][curNum % c] = nums[i][j];
}
} return result;
} public static void main(String args[]) {
int A[][] = {{1,2},{3,4}};
MatrixReshape fuc = new MatrixReshape();
System.out.println(fuc.solution(A, 1, 4));
} }
package y2019.Algorithm.array;

import java.util.Arrays;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: DuplicateZeros
* @Author: xiaof
* @Description: TODO 1089. Duplicate Zeros
* Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.
* Note that elements beyond the length of the original array are not written.
* Do the above modifications to the input array in place, do not return anything from your function.
*
* Input: [1,0,2,3,0,4,5,0]
* Output: null
* Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
*
* @Date: 2019/7/8 9:17
* @Version: 1.0
*/
public class DuplicateZeros { public void solution(int[] arr) {
//每次遇到0就修改为两次0,然后所有其他的数据右移
int[] copyArry = Arrays.copyOf(arr, arr.length);
int index = 0; for(int i = 0; i < copyArry.length && index < arr.length; ++i) {
if(copyArry[i] == 0) {
arr[index++] = 0;
if(index < arr.length)
arr[index++] = 0;
} else {
arr[index++] = copyArry[i];
}
} }
}

【LEETCODE】49、数组分类,简单级别,题目:566,1089的更多相关文章

  1. 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747

    真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...

  2. 【LEETCODE】54、数组分类,简单级别,题目:605、532

    数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...

  3. 【LeetCode】数组-1(643)-返回规定长度k的最大子数组的平均数

    好久没有刷LeetCode了,准备重拾并坚持下去,每天刷个两小时.今天算是开始的第一天,不过出师不利,在一道很简单的题目上墨迹半天.不过还好,现在踩过的坑,应该都不会白踩,这些可能都是以后程序员路上稳 ...

  4. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...

  5. LeetCode 49: 字母异位词分组 Group Anagrams

    LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...

  6. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)

    这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...

  7. LeetCode~移除元素(简单)

    移除元素(简单) 1. 题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使 ...

  8. 面阿里P7,竟问这么简单的题目?

    关于作者:程序猿石头(ID: tangleithu),来自十八县贫困农村(查看我的逆袭之路),BAT某厂P7,是前大疆(无人机)技术主管,曾经也在创业公司待过,有着丰富的经验. 本文首发于微信公众号, ...

  9. LeetCode:数组中的第K个最大元素【215】

    LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...

  10. Java数据结构和算法之数组与简单排序

    一.数组于简单排序 数组 数组(array)是相同类型变量的集合,可以使用共同的名字引用它.数组可被定义为任何类型,可以是一维或多维.数组中的一个特别要素是通过下标来访问它.数组提供了一种将有联系的信 ...

随机推荐

  1. Windows本机调试内部组件

    将详细分析Windows调试的本机接口.希望读者对C和通用NT内核体系结构和语义有一些基本的了解.此外,这并不是介绍什么是调试或如何编写调试器.它可以作为经验丰富的调试器编写人员或好奇的安全专家的参考 ...

  2. P2388 阶乘之乘

    首先感谢wxy学长之前告诉我这道题,结果今天竟然一眼切了,咕咕咕 题目链接: P2388 阶乘之乘 题目思路: 第一眼看到一定想到的是先求一下阶乘然后看最后又几个零,但是这样会TIL啊 想一下0是怎么 ...

  3. CNN中各类卷积总结:残差、shuffle、空洞卷积、变形卷积核、可分离卷积等

    CNN从2012年的AlexNet发展至今,科学家们发明出各种各样的CNN模型,一个比一个深,一个比一个准确,一个比一个轻量.我下面会对近几年一些具有变革性的工作进行简单盘点,从这些充满革新性的工作中 ...

  4. [Beta]第十次 Scrum Meeting

    [Beta]第十次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/20 22:00 20min 大运村公寓6F寝室 附Github仓库:WEDO 例会照片 工作情况 ...

  5. Linux学习笔记(一)

    操作系统(operation system,os) 主要作用是管理好硬件设备,并为用户和应用程序提供一个简单的接口, 以便于使用,作为中间人,连接软件和硬件 操作系统linux发展历程 unix-&g ...

  6. Layui 点击查询分页,页码不刷新解决方法

    Layui 点击查询分页,页码不刷新解决方法 function queryDataGrid() { layui.table.reload(tableName, { where: { //设定异步数据接 ...

  7. C# redis客户端帮助类

    需要在NuGet里面引用ServiceStack.Redis using ServiceStack.Redis; using ServiceStack.Redis.Generic; using Sys ...

  8. spark-submit --files 动态加载外部资源文件

    在做spark时,有些时候需要加载资源文件,需要在driver或者worker端访问.在client模式下可以使用IO流直接读取,但是在cluster模式下却不能直接读取,需要如下代码: val is ...

  9. 认领该应用 apk空白包签名 方法

    起因: apicloud开发的项目,上架应用宝市场,被要求做这个 解决方法: 下载签名工具,并解压缩.解压缩.解压后是两个文件夹,选择keystore签名工具. 下载地址: linux 签名工具(命令 ...

  10. Python - Django - form 组件内置的正则校验器

    app01/models.py: from django.db import models class UserInfo(models.Model): username = models.CharFi ...