LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
Example:
Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.
思路:这题感觉有点没有说清楚,如果k大于了链表长度该怎么办呢?这个时候怎么确定旋转位置呢?从右往左数,超出数组长度则取余。所以第一步是确定数组长度,结合k来确定旋转位置,然后再作旋转。
Since n may be a large number compared to the length of list. So we need to know the length of linked list.After that, move the list after the (l-n%l )th node to the front to finish the rotation.
Ex: {1,2,3} k=2 Move the list after the 1st node to the front
Ex: {1,2,3} k=5, In this case Move the list after (3-5%3=1)st node to the front.
public ListNode rotateRight(ListNode head, int n) {
if (head==null||head.next==null) return head;
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode fast=dummy,slow=dummy; int i;
for (i=0;fast.next!=null;i++)//Get the total length
fast=fast.next; for (int j=i-n%i;j>0;j--) //Get the i-n%i th node
slow=slow.next; fast.next=dummy.next; //Do the rotation
dummy.next=slow.next;
slow.next=null; return dummy.next;
}
2. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
思路:这题可以用遍历的方式取求解,但关键是如何在O(1)的空间复杂度内求解这题。一个可行的想法是,先遍历一遍矩阵,将元素为0的那一行和那一列的第一个元素标记为0,然后从第二行第二列开始遍历矩阵。如果这行或者这列的开始元素是0则将这个元素置为0,剩下一个要做的是处理将第一行和第一列上要置为0的元素。
import java.util.*; public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt(); int[][] matrix=new int[m][n];
// 老实说输入一个矩阵真的是超级烦,这里不加这个就会输入异常,大概是因为上面的nextInt()不会读入换行符?
sc.nextLine();
for(int i=0;i<m;i++){
String input=sc.nextLine();
String[] inputs=input.split(",");
for(int j=0;j<inputs.length;j++){
matrix[i][j]=Integer.parseInt(inputs[j]);
}
}
SetZeros(matrix);
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix.length;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
} static void SetZeros(int[][] matrix){
boolean fr=false, fc=false;
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]==0){
if(i==0)fr=true;
if(j==0)fc=true;
matrix[i][0]=0;
matrix[0][j]=0;
}
}
} for(int i=1;i<matrix.length;i++){
for(int j=1;j<matrix.length;j++){
if(matrix[i][0]==0||matrix[0][j]==0)
matrix[i][j]=0;
}
} if(fr==true){
for(int j=0;j<matrix[0].length;j++)
matrix[0][j]=0;
}
if(fc==true){
for(int i=0;i<matrix.length;i++)
matrix[i][0]=0;
}
}
}
3. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
import java.util.*; public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
String[] inputs=input.split(",");
int[] colors=new int[inputs.length];
for(int i=0;i<inputs.length;i++){
colors[i]=Integer.parseInt(inputs[i]);
}
sortColors(colors);
for(int c:colors){
System.out.print(c+" ");
}
} static void sortColors(int[] colors){
if(colors==null||colors.length<2)
return;
int low=0,high=colors.length-1;
for(int i=low;i<=high;){
if(colors[i]==0){
int temp=colors[i];
colors[i]=colors[low];
colors[low]=temp;
i++;low++;
}else if(colors[i]==2){
int temp=colors[i];
colors[i]=colors[high];
colors[high]=temp;
high--;
}else{
i++;
}
}
}
}
LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- LeetCode解题报告—— Group Anagrams & Pow(x, n) & Spiral Matrix
1. Group Anagrams Given an array of strings, group anagrams together. For example, given: ["eat ...
- leetcode解题报告(20):Rotate Array
描述 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...
- leetcode解题报告(16):Move Zeroes
描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
随机推荐
- bzoj4870: [Shoi2017]组合数问题(DP+矩阵乘法优化)
为了1A我居然写了个暴力对拍... 那个式子本质上是求nk个数里选j个数,且j%k==r的方案数. 所以把组合数的递推式写出来f[i][j]=f[i-1][j]+f[i-1][(j-1+k)%k].. ...
- linux添加vim编辑器和一些用法
vim.tar文件在自己的百度云盘里面,linux目录下 上传vim.tar文件,解压 vim编辑文件的一些快捷方式: n+t打开文件所在目录,显示在左侧 ctrl+w+l 切换到右边文件ctrl+w ...
- Educational Codeforces Round 6 B
B. Grandfather Dovlet’s calculator time limit per test 1 second memory limit per test 256 megabytes ...
- vue-transition-animation
<!Doctype> <html> <head> <meta charset="utf-8"> <meta name=&quo ...
- 【Android】完善Android学习(三:API 3.0)
备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...
- http学习 - 缓存
对缓存的理解更加深刻,缓存有一个过期时间,现在用的比较多的是 max-age,以前使用 expirt之类的, 然后就是需要向服务器验证是否是最新的,如果不是最新的则需要更新.
- Jmeter-8-FTP测试
1. 此处要深刻理解FTP的用法. 2. Get的时候填写的Remote File 路径/, 此处是相对路径. 实际为/home/user/ 3. Local file 此处要写到具体的文件. 4. ...
- HDFS不存在绝对路径,无法找到文件所在具体位置
This is set in the dfs.datanode.data.dir property, which defaults to file://${hadoop.tmp.dir}/dfs/da ...
- Item27--优先考虑泛型方法
类型推导:发生在以下三个地方.1.Java编译器根据泛型方法传入的参数,推导出具体的类型.2.Java编译器,根据泛型构造器传入的类型来推导出实际要构造的实例类型.3.Java编译器根据表达式的目标类 ...
- Hadoop和大数据:60款顶级开源工具(山东数漫江湖)
说到处理大数据的工具,普通的开源解决方案(尤其是Apache Hadoop)堪称中流砥柱.弗雷斯特调研公司的分析师Mike Gualtieri最近预测,在接下来几年,“100%的大公司”会采用Hado ...