1.“之” 字形打印矩阵

【题目】 给定一个矩阵matrix, 按照“之” 字形的方式打印这个矩阵, 例如: 1 2 3 4 5 6 7 8 9 10 11 12“之” 字形打印的结果为: 1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12

【要求】 额外空间复杂度为O(1)。

1.1 分析

1.设置两个点A,B,起始位置如图:

  • A每次向右走,走到最右边的时候往下走;
  • B每次往下走,走到最下边的时候往右走;

A、B的运动方式可确定对角线的两个端点,如图:

接着,只要根据每次A、B的坐标,打印这条对角线即可。

同时需要设置一个boolean值,表示打印对角线时是否是根据从右上往左下打印。

1.2 代码实现

package Solution;public class ZhiPrintMatrix {public static void main(String[] args) {int[][] matrix= {{1,2,3,4},{5,6,7,8},{9,10,11,12}};PointCom(matrix);}public static void PointCom(int[][] m) {int ax=0;//Axint ay=0;//Ayint bx=0;//Bxint by=0;//Byint x=m.length-1;//行int y=m[0].length-1;//列boolean fromUp=false;while(ax<=x) {//ay=y说明A到了最后一行,即走完了横也走完了竖EdgePrint(m,ax,ay,bx,by,fromUp);//行号的变化:当a的列数来到最后一列,它才往下走,即+1;否则不变ax=ay==y?ax+1:ax;//列号的变化:当a的列号来到最后一列之前,一直+1,到最后一列之后就不变ay=ay==y?ay:ay+1;by=bx==x?by+1:by;bx=bx==x?bx:bx+1;fromUp=!fromUp;}}public static void EdgePrint(int[][] m,int ax,int ay,int bx,int by,boolean flag) {if(flag) {//从上到下while(ax<=bx) {System.out.print(m[ax++][ay--]+" ");}}else {while(bx>=ax) {System.out.print(m[bx--][by++]+" ");}}}
}

运行结果:

2.在行列都排好序的矩阵中找数

【题目】 给定一个有N*M的整型矩阵matrix和一个整数K,matrix的每一行和每一列都是排好序的。 实现一个函数, 判断K是否在matrix中。 例如: 0 1 2 5 2 3 4 7 4 4 4 8 5 7 7 9 如果K为7, 返回true; 如果K为6, 返回false。

【要求】 时间复杂度为O(N+M), 额外空间复杂度为O(1)。

2.1 分析

2.1.1 方法一:从右上角点开始

若K>当前节点,往下;

若K<当前节点,往左;

若越界了都没找到,则返回false。

比如找4:

2.1.2 方法二:从左下角开始

若K>当前节点,则往右;

若K<当前节点,则往上;

2.2 代码实现

package Solution;public class FindValue {public static void main(String[] args) {int[][] matrix= {{0, 1, 2, 5},{ 2, 3, 4, 7}, {4, 4, 4, 8}, {5, 7, 7, 9}};System.out.println("6:"+findValue(matrix,6));System.out.println("7:"+findValue(matrix,7));}public static boolean findValue(int[][] m,int k) {int x=m.length-1;int y=m[0].length-1;//从右上角的点开始int i=0;int j=y;while(i<=x&&j>=0) {if(k==m[i][j])return true;else if(k>m[i][j])//k>当前值,往下i++;elsej--;}return false;}
}

运行结果:

3.打印两个有序链表的公共部分

【题目】 给定两个有序链表的头指针head1和head2, 打印两个链表的公共部分。

3.1 分析

Merge的过程中相等的打印即可。

3.2 代码实现

package Solution;public class PrintSameValue {public static void main(String[] args) {Node node1 = new Node(2);node1.next = new Node(3);node1.next.next = new Node(5);node1.next.next.next = new Node(6);Node node2 = new Node(1);node2.next = new Node(2);node2.next.next = new Node(5);node2.next.next.next = new Node(7);node2.next.next.next.next = new Node(8);printLinkedList(node1);printLinkedList(node2);printValue(node1, node2);}public static void printValue(Node head1,Node head2) {while(head1!=null&&head2!=null) {if(head1.value==head2.value) {System.out.print(head1.value+" ");head1=head1.next;head2=head2.next;}else if(head1.value>head2.value)head2=head2.next;elsehead1=head1.next;}}public static void printLinkedList(Node node) {System.out.print("Linked List: ");while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}
}

运行结果:

4. 判断一个链表是否为回文结构

【题目】 给定一个链表的头节点head, 请判断该链表是否为回文结构。

例如: 1->2->1, 返回true。 1->2->2->1, 返回true。15->6->15, 返回true。 1->2->3, 返回false。

进阶: 如果链表长度为N, 时间复杂度达到O(N), 额外空间复杂度达到O(1)。

链表问题的最优解:往往在乎的是最小的空间复杂度(和其他题目有所不同)。

4.1 方法一

将链表遍历一遍,值放入栈中。

进行第二次遍历,同时将遍历到的节点值与弹出的栈顶元素相比较,若每一次都相等,则返回true;否则返回false。

4.1.1 代码实现

package Solution;import java.util.List;
import java.util.Stack;public class IsPalindrome {public static void main(String[] args) {Node head = null;printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");head = new Node(1);printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(1);printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(1);printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);head.next.next.next = new Node(1);printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(2);head.next.next.next = new Node(1);printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);head.next.next.next = new Node(2);head.next.next.next.next = new Node(1);printLinkedList(head);System.out.println(isPalindrome(head) );printLinkedList(head);System.out.println("=========================");}public static boolean isPalindrome(Node head) {if (head == null || head.next == null) {return true;}Stack<Integer> help=new Stack<Integer>();Node newhead=head;while(head!=null) {help.push(head.value);head=head.next;}      while(newhead!=null) {if(newhead.value==help.pop())newhead=newhead.next;elsereturn false;}return true;}public static void printLinkedList(Node node) {System.out.print("Linked List: ");while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}
}

运行结果:

4.2 方法二

两个指针:

  • 快指针每次走两步;
  • 慢指针每次走一步。

当快指针走到末尾的时候,慢指针基本走到了中间。

然后慢指针继续往后走,并将走过的节点值压栈。后续过程同方法一。

4.2.1 代码实现

package Solution;import java.util.List;
import java.util.Stack;public class IsPalindrome {public static boolean isPalindrome(Node head) {//方法二if (head == null || head.next == null) {return true;}Node quick=head;Node slow=head;Stack<Integer> help=new Stack<Integer>();while(quick.next!=null&&quick.next.next!=null) {//可做到元素个数为奇数的时候来到重点,偶数个数的时候来到中间两个的前者quick=quick.next.next;slow=slow.next;}while(slow!=null) {help.push(slow.value);slow=slow.next;}while(!help.isEmpty()) {if(head.value==help.pop())head=head.next;elsereturn false;}return true;}public static void printLinkedList(Node node) {System.out.print("Linked List: ");while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}
}

4.3 方法三:不用辅助空间

两个指针:

  • 快指针每次走两步;
  • 慢指针每次走一步。

当快指针走到末尾的时候,慢指针基本走到了中间。

然后将后半部分进行逆序。

然后head和slow从各自的头开始往后比较。

最后返回之前再将后半部分逆序回来。

4.3.1 代码实现

package Solution;import java.util.List;
import java.util.Stack;public class IsPalindrome {public static boolean isPalindrome(Node head) {//方法三if (head == null || head.next == null) {return true;}Node quick=head;Node slow=head;while(quick.next!=null&&quick.next.next!=null) {quick=quick.next.next;slow=slow.next;}//此时quick指向的是末尾节点;slow指向的是中点quick=slow.next;//quick变为右半部分的第一个节点,实例中的节点3后面的节点2slow.next=null;//中间节点的next为空Node newhead=null;while(quick!=null) {newhead=quick.next;quick.next=slow;slow=quick;quick=newhead;}newhead=slow;quick=head;//quick是左半边的头结点boolean result=true;while(quick!=null&&slow!=null) {if(quick.value==slow.value) {quick=quick.next;slow=slow.next;}else {//不能返回,右半部分还未还原result=false;break;}             }slow=newhead.next;//slow指向倒数第二个节点newhead.next=null;while (slow != null) { // recover list//slow=head;quick=next;newhead=prequick = slow.next;slow.next = newhead;newhead = slow;slow = quick;}return result;}public static void printLinkedList(Node node) {System.out.print("Linked List: ");while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}
}

算法练习day9——190327(“之” 字形打印矩阵、在行列都排好序的矩阵中找数、打印两个有序链表的公共部分、判断一个链表是否为回文结构)相关推荐

  1. 《程序员代码面试指南》第八章 数组和矩阵问题 在行列都排好序的矩阵中找数...

    题目 在行列都排好序的矩阵中找数 java代码 package com.lizhouwei;/*** @Description: 在行列都排好序的矩阵中找数* @Author: lizhouwei* ...

  2. 在行列都排好序的矩阵中找数

    题目] 给定一个有N*M的整型矩阵matrix和一个整数K, matrix的每一行和每一 列都是排好序的.实现一个函数,判断K 是否在matrix中. 例如: 0 1 2 5 2 3 4 7 4 4 ...

  3. 数据结构与算法之打印两个有序链表公共部分和判断一个链表是否具有回文结构

    数据结构与算法之打印两个有序链表公共部分和判断一个链表是否具有回文结构 目录 打印两个有序链表公共部分 判断一个链表是否具有回文结构 1. 打印两个有序链表公共部分 1.问题描述 思路:Node1和N ...

  4. c语言数据结构判断回文数,C++数据结构与算法之判断一个链表是否为回文结构的方法...

    本文实例讲述了C++判断一个链表是否为回文结构的方法.分享给大家供大家参考,具体如下: 题目: 给定一个链表头节点head,请判断是否为回文结构 例如: 1->2->1 true 1-&g ...

  5. 打印设置自定义表尾_教大家Excel2013表格中如何打印固定的表头和表尾

    近日有关于Excel2013表格中如何打印固定的表头和表尾的问题受到了很多网友们的关注,大多数网友都想要知道Excel2013表格中如何打印固定的表头和表尾的具体情况,那么关于到Excel2013表格 ...

  6. 常考数据结构与算法:判断一个链表是否为回文结构

    题目描述 给定一个链表,请判断该链表是否为回文结构. 示例1 输入 [1,2,2,1] 返回值 true 思路: 双指针,快指针一次走两步,慢指针一次走一步,快指针走完,慢指针走到中点.然后将中点开始 ...

  7. 算法练习:判断一个链表是否为回文结构

    [题目]给定一个单链表的头节点head,请判断该链表是否为回文结构. [例子]1->2->1, 返回true; 1->2- ->2->1,返回true; 15->6 ...

  8. 数据结构与算法之“之”字型打印矩阵和矩阵中找数

    数据结构与算法之"之"字型打印矩阵和矩阵中找数 目录 "之"字型打印矩阵 在行列都排好序的矩阵中找数 1. "之"字型打印矩阵 题目描述 思 ...

  9. python输出矩阵的行数_python查看矩阵的行列号以及维数方式

    print(X.shape):查看矩阵的行列号 print(len(X)):查看矩阵的行数 print(X.ndim):查看矩阵的维数 1 查看矩阵的行列号 2 查看矩阵的行数 3 查看矩阵的维数 补 ...

最新文章

  1. Nginx配置免费SSL证书
  2. 判断true的正确做法
  3. Java StringBuffer类
  4. 直接内存访问 (Direct Memory Access, DMA)
  5. 蓝桥杯 PREV-3 历届试题 带分数 Java版
  6. event.keyCode 事件属性
  7. 11 Steps Attackers Took to Crack Target
  8. mysql按逗号拼接起来_MySql逗号拼接字符串查询的两种方法
  9. pdf合并成一个文件,pdf合并方法
  10. 美国专利复审程序中Claim Construction标准变更带来的可能影响
  11. 微信小程序快速达到1000UV流量主开通标准
  12. glibc 知:手册08:消息翻译
  13. 工业机器人应用编程考核设备
  14. Navicat Premium 数据库开发工具
  15. 蓝牙防水耳机排行榜前十名,防水音质表现好的蓝牙耳机推荐
  16. 敏捷实践之回顾会议Retro
  17. 在网页中使用iframe嵌入B站视频(腾讯视频同理)
  18. 4.1 随机变量的数学期望
  19. QGIS 1. qgis的下载和安装(Windows和macOS)
  20. Word2003文档中字数统计在哪?

热门文章

  1. 《Ext JS权威指南》——2.1节获取Ext JS 4
  2. 关于Apache虚拟主机的设置
  3. HTML5 audio 标签-在html中定义声音的标签
  4. 代码艺术~优雅的编程字体及对字体的感觉
  5. 跟互联力量学Silverlight之十_如何完整安装Silverlight 4中文版
  6. FLV Extract 1.2.1
  7. HashMap之三问为什么及性能问题
  8. linux fedora35让GRUB 2记住上一次启动的操作系统
  9. reactjs组件通讯:父组件传递数据给子组件
  10. 【收藏】spark 连接到yarn resourcemanager失败解决方案