【LEETCODE】49、数组分类,简单级别,题目:566,1089
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的更多相关文章
- 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747
真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...
- 【LEETCODE】54、数组分类,简单级别,题目:605、532
数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...
- 【LeetCode】数组-1(643)-返回规定长度k的最大子数组的平均数
好久没有刷LeetCode了,准备重拾并坚持下去,每天刷个两小时.今天算是开始的第一天,不过出师不利,在一道很简单的题目上墨迹半天.不过还好,现在踩过的坑,应该都不会白踩,这些可能都是以后程序员路上稳 ...
- LeetCode:颜色分类【75】
LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...
- LeetCode 49: 字母异位词分组 Group Anagrams
LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...
- LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)
这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...
- LeetCode~移除元素(简单)
移除元素(简单) 1. 题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使 ...
- 面阿里P7,竟问这么简单的题目?
关于作者:程序猿石头(ID: tangleithu),来自十八县贫困农村(查看我的逆袭之路),BAT某厂P7,是前大疆(无人机)技术主管,曾经也在创业公司待过,有着丰富的经验. 本文首发于微信公众号, ...
- LeetCode:数组中的第K个最大元素【215】
LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...
- Java数据结构和算法之数组与简单排序
一.数组于简单排序 数组 数组(array)是相同类型变量的集合,可以使用共同的名字引用它.数组可被定义为任何类型,可以是一维或多维.数组中的一个特别要素是通过下标来访问它.数组提供了一种将有联系的信 ...
随机推荐
- Flutter 简介(事件、路由、异步请求)
1. 前言 Flutter是一个由谷歌开发的开源移动应用软件开发工具包,用于为Android和iOS开发应用,同时也将是Google Fuchsia下开发应用的主要工具.其官方编程语言为Dart. 同 ...
- 前端微信小程序资讯类仿今日头条微信小程序
需求描述及交互分析设计思路和相关知识点新闻频道滑动效果设计首页新闻内容设计首页新闻详情页设计我的界面列表式导航设计系统设置二级界面设计 设计思路(1)设计底部标签导航,准备好底部标签导航的图标和建立相 ...
- PHP全栈学习笔记33
javascript能做什么? 语句与注解,标识符,字面量,变量命名规范 JavaScript中的数据类型,其它类型如何转为数据型,操作符 字符方法,数组方法,函数与对象,函数表达式,变量作用域,对象 ...
- manjaro AwesomeWM 上使用双显示器
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/dual_monitor_manjaro_awesome 安装ma ...
- python 判断一个字符串组合后,是否在另一个字符串中
code #coding=utf- def getdic(s): dic = {} for i in s: if (i not in dic): dic[i] = else: dic[i] += re ...
- 「HNOI2016」序列
传送门 Description 有 \(q\) 个询问,每个询问给定两个数\(l\) 和\(r\),求 \(a[l:r]\) 的不同子序列的最小值之和 Solution 校内模拟赛用了这道题,但是莫 ...
- TCP Keepalive笔记
TCP是无感知的虚拟连接,中间断开两端不会立刻得到通知.一般在使用长连接的环境下,需要心跳保活机制可以勉强感知其存活.业务层面有心跳机制,TCP协议也提供了心跳保活机制. 长连接的环境下,人们一般使用 ...
- 隐藏一个button的方法(2种) 写出一个button的按钮(2种)
display:none;visibility:hidden: <input type = button><button>这是一个按钮 </button>
- Python分词、情感分析工具——SnowNLP
本文内容主要参考GitHub:https://github.com/isnowfy/snownlp what's the SnowNLP SnowNLP是一个python写的类库,可以方便的处理中文文 ...
- RequestUtil 获取网址页面信息
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...