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?

思路:可以扫描一遍记录0,1,2的个数然后新建个数组即可。但是如何再O(1)的空间复杂度内完成?可以借鉴快速排序的思想,两个指针分别从左右扫面,左边如果遇到0,那么将它与最低(左)位交换,这时确定了最左边一定是0,所以最低位加1,同时i要加1,这里为什么i要加1呢?可以肯定的一点是无论位置i上交换后是几都无关紧要,因为low肯定是小于等于i的,low在一路加过来的过程中就已经把相应的位置全置为0了。如果遇到的是2,那么将其与最大位置(最右边)交换,最大位减1,此时i不加1,因为如果交换后i位置上是2的话,那么i再加1的话这个2就无法挪到最右边。。简而言之这个过程就是,从左往右扫面数组,遇到的0全堆积在左边,遇到的2全堆积到右边,那么中间剩下的部分就全是1了。
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的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode解题报告—— Permutations & Permutations II & Rotate Image

    1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

  6. LeetCode解题报告—— Group Anagrams & Pow(x, n) & Spiral Matrix

    1. Group Anagrams Given an array of strings, group anagrams together. For example, given: ["eat ...

  7. 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 ...

  8. 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 ...

  9. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

随机推荐

  1. UVA.11636 Hello World! (思维题)

    UVA.11636 Hello World! (思维题) 题意分析 这题挺水的,还是错了几发. QWQ. 有一个同学打了一行hello world,现在他想打n行hello world,请问最少复制粘 ...

  2. HashMap多线程并发的问题

    ---恢复内容开始--- 前言:大多数javaer都知道HashMap是线程不安全的,多线程环境下数据可能会发生错乱,一定要谨慎使用.这个结论是没错,可是HashMap的线程不安全远远不是数据脏读这么 ...

  3. Going Home POJ - 2195 费用流板子题

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  4. Android 自定义ListView实现底部分页刷新与顶部下拉刷新,androidlistview

    在项目开发中,由于数据过大时,需要进行分页加载或下拉刷新,来缓解一次性加载的过长等待.本篇博文实例讲解通过自定义的ListView实现底部分页加载和顶部下拉刷新的效果. 其效果图: 一.ListVie ...

  5. Chrome控制台报错个人总结(不定时更新)

    最近开始使用Chrome控制台检测代码错误,对于经常碰到的报错做一个汇总,免得每次遇到都要重新想一遍策略,错误原因,重复劳动,浪费时间. 由于不是每个错误都能碰到,以下仅列出个人写代码时经常碰到的报错 ...

  6. 【Android】Android之Copy and Paste

    Android为复制粘贴提供了一个强大的基于剪切板的框架,它支持简单和复杂的数据类型,包括纯文本,复杂的数据结构,二进制流,甚至app资源文件.简单的文本数据直接存储在剪切板中,而复杂的数据则存储的是 ...

  7. quick-cocos2dx 悬浮节点(NotificationNode)

    cocos2dx 开发游戏时,有时某些节点不需要随着场景的切换而销毁.但cocos2dx的机制只允许同时只有一个运行的场景,如果你的所有节点都是依附于这个场景的,那场景的切换必然带来节点的销毁. 比如 ...

  8. 【转载】Lua脚本语法说明(修订)

    原文:http://www.cnblogs.com/ly4cn/archive/2006/08/04/467550.html 挑出来几个 .逻辑运算 and, or, not 其中,and 和 or ...

  9. 01背包入门 dp

    题目引入: 有n个重量和价值分别为Wi,Vi的物品.从这些物品中挑选出总重量不超过W的物品,求所有挑选方案中的价值总和的最大值. 分析: 首先,我们用最普通的方法,针对每个物品是否放入背包进行搜索. ...

  10. 加overflow-hidden就可以解决高度塌陷问题,overflow-触发BFC

    1.BFC 全称是块级排版上下文,用于对块级元素排版,默认情况下只有根元素(body)一个块级上下文,但是如果一个块级元素 设置了float:left,overflow:hidden或position ...