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. go的接口内部实现

    1 前言 1.1 Go汇编 Go语言被定义为一门系统编程语言,与C语言一样通过编译器生成可直接运行的二进制文件.这一点与Java,PHP,Python等编程语言存在很大的不同,这些语言都是运行在基于C ...

  2. 如何打开 win 10 内置应用

    gpedit.msc windows设置 安全设置 本地策略 安全选项 >> 用户账户控制:用于内置管理员账户的管理员批准模式:已启用

  3. presto集成kerberos以及访问集成了kerberos的hive集群

    1.创建主体 注: 192.168.0.230 为单节点集群 192.168.4.50为kdc服务器 192.168.0.9为客户端 1.1.Kdc服务器创建主体 # kadmin.local -q ...

  4. 使用pwn_deploy_chroot部署国赛pwn比赛题目

    目录 使用pwn_deploy_chroot部署国赛pwn比赛题目 一.前言 二.Docker 三.部署镜像 四.pwn_deploy_chroot 五.check && exp 六. ...

  5. 【大数据应用技术】作业十二|Hadoop综合大作业

    本次作业的要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3339 前言 本次作业是在<爬虫大作业>的基础上进行的 ...

  6. flask上传文件到指定路径

    flask上传文件到指定路径 项目结构如下: 首先是:视图函数uload_file.py,代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- fro ...

  7. 通过自定义EasyNVR的Logo、标题、版权等相关信息构建属于自己的摄像机网页视频直播服务

    随着互联网基础设施建设的发展,4G/5G/NB-IoT各种网络技术.物联网技术的大规模商用,视频随时随地可看.可控.可回溯的诉求越来越多,互联网思维.架构和技术引入进传统监控行业里,成为新形势下全终端 ...

  8. [LeetCode] 52. N-Queens II N皇后问题 II

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  9. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  10. [LeetCode] 388. Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...