日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

算法练习day8——190326(猫狗队列、转圈打印矩阵、旋转正方形矩阵、反转单向双向链表、数N的加法组合)

發布時間:2024/10/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法练习day8——190326(猫狗队列、转圈打印矩阵、旋转正方形矩阵、反转单向双向链表、数N的加法组合) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.貓狗隊列

【題目】 寵物、 狗和貓的類如下:

public class Pet {private String type;public Pet(String type) {this.type = type;}public String getPetType() {return this.type;} }public class Dog extends Pet {public Dog() {super("dog");} }public class Cat extends Pet {public Cat() {super("cat");} }

實現一種狗貓隊列的結構, 要求如下:

  • 用戶可以調用add方法,將cat類或dog類的實例放入隊列中;
  • 用戶可以調用pollAll方法, 將隊列中所有的實例按照進隊列的先后順序依次彈出;
  • 用戶可以調用pollDog方法, 將隊列中dog類的實例按照進隊列的先后順序依次彈出;
  • 用戶可以調用pollCat方法, 將隊列中cat類的實例按照進隊列的先后順序依次彈出;
  • 用戶可以調用isEmpty方法, 檢查隊列中是否還有dog或cat的實例;
  • 用戶可以調用isDogEmpty方法, 檢查隊列中是否有dog類的實例;
  • 用戶可以調用isCatEmpty方法, 檢查隊列中是否有cat類的實例。

1.1 分析

使用兩個隊列實現:

add():當寵物進入結構時:

  • 若寵物為狗,則進入Dog隊列;
  • 若為貓,則進入Cat隊列。

pollDog():從Dog隊列取頭一個狗;

pollCat():從Cat隊列取頭一個貓;

pollAll():得設置一個計數器count,作為時間戳,進來一個寵物,無論貓狗,count++,賦給這個寵物的count,取出時,由Dog隊列和Cat隊列的頭一個的count來判斷哪個寵物進的早點。

isEmpty():Dog隊列和Cat隊列都為空時返回true;

isDogEmpty():Dog隊列為空時返回true;

isDogEmpty():Cat隊列為空時返回true;

1.2 代碼實現

注:不能更改給定的Pet、Cat、Dog類,需自己寫其他的類實現以上功能。

package Solution;import java.util.LinkedList; import java.util.Queue;class Pet {private String type;public Pet(String type) {this.type = type;}public String getPetType() {return this.type;} }class Dog extends Pet {public Dog() {super("dog");} } class Cat extends Pet {public Cat() {super("cat");} }class PetQueue{private Pet pet;private long count;public PetQueue(Pet pet, long count) {this.pet=pet;this.count=count;}public Pet getPet() {return this.pet;}public long getCount() {return this.count;}public String getPetType() {return this.pet.getPetType();} } public class CatDogQueue {private Queue<PetQueue> DogQ;private Queue<PetQueue> CatQ;long count;//計數器public CatDogQueue() {this.DogQ=new LinkedList<PetQueue>();this.CatQ=new LinkedList<PetQueue>();this.count=0;}public void add(Pet pet) {//根據類型入相應的隊列if(pet.getPetType().equals("cat"))CatQ.add(new PetQueue(pet,count++));else if(pet.getPetType().equals("dog"))DogQ.add(new PetQueue(pet,count++));elsethrow new RuntimeException("err, not dog or cat");}public Pet pollAll() {if(!DogQ.isEmpty()&&!CatQ.isEmpty())if(DogQ.peek().getCount()<CatQ.peek().getCount())//越小越早return this.DogQ.poll().getPet();elsereturn this.CatQ.poll().getPet();else if(!DogQ.isEmpty())return this.DogQ.poll().getPet();else if(!CatQ.isEmpty())return this.CatQ.poll().getPet();elsethrow new RuntimeException("err, queue is empty!");}public Dog pollDog() {if(!DogQ.isEmpty())return (Dog)this.DogQ.poll().getPet();elsethrow new RuntimeException("err, Dog queue is empty!");}public Cat pollCat() {if(!CatQ.isEmpty())return (Cat)this.CatQ.poll().getPet();elsethrow new RuntimeException("err, Cat queue is empty!");}public boolean isEmpty() {return this.DogQ.isEmpty()&&this.CatQ.isEmpty();}public boolean isDogQueueEmpty() {return this.DogQ.isEmpty();}public boolean isCatQueueEmpty() {return this.CatQ.isEmpty();}public static void main(String[] args) {CatDogQueue test = new CatDogQueue();Pet dog1 = new Dog();Pet cat1 = new Cat();Pet dog2 = new Dog();Pet cat2 = new Cat();Pet dog3 = new Dog();Pet cat3 = new Cat();test.add(dog1);test.add(cat1);test.add(dog2);test.add(cat2);test.add(dog3);test.add(cat3);System.out.println(test.pollAll().getPetType());System.out.println(test.pollDog().getPetType());System.out.println(test.pollCat().getPetType());while (!test.isDogQueueEmpty()) {System.out.println(test.pollDog().getPetType());}while (!test.isEmpty()) {System.out.println(test.pollAll().getPetType());}} }

運行結果:

2.轉圈打印矩陣

【題目】 給定一個整型矩陣matrix, 請按照轉圈的方式打印它。例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14? 15 16 打印結果為: 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9,5, 6, 7, 11, 10
【要求】 額外空間復雜度為O(1)

另一篇已寫過

2.1 分析

2.1.1 子過程

給定左上角和右下角兩個點,可以確定一個矩形。

打印上邊的邊:

打印右邊的邊:

打印下邊的邊:

打印左邊的邊:

2.1.2 整體分析

一層一層打印,第一層打完,由左上角和右下角移動后,分別確定下一層的范圍:

2.2 代碼實現

package Solution;public class PrintMatrix {public static void main(String[] args) {int[][] matrix= {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};int[][] ma1= {{1,2,3,4}};int[][] ma2= {{1},{2},{3},{4}};printMatrix(matrix);System.out.println();printMatrix(ma1);System.out.println();printMatrix(ma2);}public static void printMatrix(int[][] matrix) {int hstart=0;int hend=0;int lstart=matrix.length-1;//行數int lend=matrix[0].length-1;//列數while(hstart<=lstart&&hend<=lend) {printEdge(matrix,hstart++,hend++,lstart--,lend--);}}public static void printEdge(int[][] ma,int hs,int he,int ls,int le) {if(hs==ls)while(he<=le)System.out.print(ma[hs][he++]+" ");else if(he==le)while(hs<=ls)System.out.print(ma[hs++][he]+" ");else {int curC=hs;//當前行標int curR=he;//當前列標while(curR<le) System.out.print(ma[curC][curR++]+" ");while(curC<ls)System.out.print(ma[curC++][curR]+" ");while(curR>he) System.out.print(ma[curC][curR--]+" ");while(curC>hs)System.out.print(ma[curC--][curR]+" ");}} }

運行結果:

3.旋轉正方形矩陣

【題目】 給定一個整型正方形矩陣matrix, 請把該矩陣調整成順時針旋轉90度的樣子。
【要求】 額外空間復雜度為O(1)。

3.1 分析

分層轉。如下,分成兩層:

第一層:

拿出1放到4的位置,4放到16的位置,16放到13的位置,13放到1的位置;

接著:

第一層中的2放到8的位置,8放到15的位置,15放到9的位置,9放到2的位置;

3,5,12,14類似。

第二層也一樣。

3.2 代碼實現

package Solution;public class RotateMatrix {public static void main(String[] args) {int[][] matrix= {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};int hstart=0;int hend=0;int lstart=matrix.length-1;int lend=matrix[0].length-1;while(hstart<=lstart)rotateMatrix(matrix,hstart++,hend++,lstart--,lend--);for(int i=0;i<matrix.length;i++) {for(int j=0;j<matrix[0].length;j++)System.out.print(matrix[i][j]+" ");System.out.println();}}public static void rotateMatrix(int[][] ma,int hs,int he,int ls,int le) {int times=le-he;for(int i=0;i<times;i++){int temp=ma[hs][he+i];ma[hs][he+i]=ma[ls-i][he];ma[ls-i][he]=ma[ls][le-i];ma[ls][le-i]=ma[hs+i][le];ma[hs+i][le]=temp; }} }

運行結果:

注意:代碼中times的使用。

int times=le-he;

  • 即每行元素的個數-1
  • 每一列第一個元素加到最后一個元素所需加的次數
  • 但是不能到最后一個元素,所以循環的i應小于times。到達最后一個元素的前一個元素就可停止。

4.反轉單向和雙向鏈表

【題目】 分別實現反轉單向鏈表和反轉雙向鏈表的函數。

【要求】 如果鏈表長度為N, 時間復雜度要求為O(N), 額外空間復雜度要求為O(1)。

4.1 反轉單鏈表

4.1.1 分析

反轉之前:

需要設置兩個指針pre、next,next指向當前節點的后繼(以確定當前節點的下一個節點的位置),pre指向當前節點的前驅(以確定當前節店反轉之后的后繼)。head指向的是當前節點。

Node pre=null; Node next=head.next;

首先,令當前節點的后繼指向pre所指的節點(此處為null,反轉之后1為末節點,所以他的后繼本應也就為null):

head.next=pre;

然后,令pre指向head,以保存2節點反轉之后的后繼節點位置:

pre=head;

接著,head移到它的后繼位置,繼續處理下一個節點(節點2):

head=next;

一輪結束!

第二輪:

  • next指向head的后繼節點(節點3);
  • head的后繼指向pre(節點1);
  • pre指向當前節點(節點2);
  • head指向next節點(節點3);
  • 代碼:

    next=head.next; head.next=pre; pre=head; head=next;

    圖示:

    4.1.2 代碼實現

    package Solution;class Node {public int value;public Node next;public Node(int data) {this.value = data;} }public class ReverseList {public static void main(String[] args) {Node head=new Node(1);head.next=new Node(2);head.next.next=new Node(3);printList(head);Node revhead=reverseList(head);printList(revhead);}public static Node reverseList(Node head) {//head不為空Node pre=null;Node next=null;while(head!=null) {next=head.next;head.next=pre;pre=head;head=next;}return pre;}public static void printList(Node head) {while(head!=null) {System.out.print(head.value+" ");head=head.next;}System.out.println();}}

    運行結果:

    4.2 反轉雙向鏈表

    4.2.1 分析

    同樣準備兩個指針pre、next:

    步驟和反轉單向鏈表一樣:

    代碼:

    next=head.next; head.next=pre; head.last=next; pre=head; head=next;

    圖示:

    4.2.2 代碼實現

    package Solution;class DoubleNode {public int value;public DoubleNode last;public DoubleNode next;public DoubleNode(int data) {this.value = data;} } public class ReverseDoubleList {public static void main(String[] args) {DoubleNode head2 = new DoubleNode(1);head2.next = new DoubleNode(2);head2.next.last = head2;head2.next.next = new DoubleNode(3);head2.next.next.last = head2.next;head2.next.next.next = new DoubleNode(4);head2.next.next.next.last = head2.next.next;printDoubleList(head2);printDoubleList(reverseDoubleList(head2));}public static DoubleNode reverseDoubleList(DoubleNode head) {DoubleNode next=null;DoubleNode pre=null;while(head!=null) {next=head.next;head.next=pre;head.last=next;pre=head;head=next;}return pre;}public static void printDoubleList(DoubleNode head) {System.out.print("Double Linked List: ");DoubleNode end = null;while (head != null) {System.out.print(head.value + " ");end = head;head = head.next;}System.out.print("| ");while (end != null) {System.out.print(end.value + " ");end = end.last;}System.out.println();} }

    運行結果:

    5.求n的加法組合,將一個整數拆分成多個整數相加的形式, O(N)時間,O(N)空間

    原博

    ?

    總結

    以上是生活随笔為你收集整理的算法练习day8——190326(猫狗队列、转圈打印矩阵、旋转正方形矩阵、反转单向双向链表、数N的加法组合)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 黄色99| 91九色蝌蚪 | 一起艹在线观看 | 九九热免费 | 日本国产在线播放 | 免费成人深夜夜国外 | 欧美激情亚洲激情 | 国产精品热久久 | 九九爱精品 | 国精产品一区 | 国产精品免费一区二区三区都可以 | h片在线免费观看 | 999精品在线观看 | 日韩av看片| 日韩电影一区二区在线观看 | 强行挺进皇后紧窄湿润小说 | 成年人的免费视频 | 国产一区在线不卡 | 精品蜜桃一区二区三区 | 日韩三级大片 | 毛利兰被扒开腿做同人漫画 | 日韩3p | 99re这里只有精品首页 | 日韩综合第一页 | 久久久国际精品 | 凸凹人妻人人澡人人添 | 天天干天天做 | 禁漫天堂黄漫画无遮挡观看 | 国产极品久久 | 在线视频观看一区 | 日韩深夜在线 | 精久久久久 | 久久日本精品字幕区二区 | 日本在线www | 久久久久久久久一区 | 久久久久国产一区 | 国产露脸150部国语对白 | 日本人妻一区 | 亚洲一区电影网 | 丰满大乳露双乳呻吟 | 国产精品美女久久久久久久久 | 精品久久久久久久久久 | 日本不卡不卡 | 国产床上视频 | jiizzyou欧美2| 51av在线 | 成人手机在线视频 | 色综合视频在线 | 兔费看少妇性l交大片免费 日韩高清不卡 | 精品一区二区三区四 | 91色在线播放 | 婷婷五综合 | 亚洲精品手机在线 | 美女扒开粉嫩的尿囗给男生桶 | 日韩第一页在线 | 欧美一区二区三区粗大 | 性色欲情网站iwww九文堂 | 特黄一区二区三区 | 国产精品一区在线免费观看 | 91theporn国产在线观看 | 亚洲九九色| 日本在线看片 | 日韩无 | 日韩成人在线视频观看 | 中文字幕亚洲欧美日韩 | 91丝袜一区在线观看 | 国产精品免费av一区二区 | 一级黄色性片 | 国产精品久久久久三级无码 | 年下总裁被打光屁股sp | 免费无码毛片一区二区app | 色老大影院 | 久色国产 | 国产精品国语 | 日日操夜夜操天天操 | 好吊妞视频这里只有精品 | 777欧美| 国产精品伦一区二区三区免费看 | 四虎免费av | 伊人99在线 | 亚洲精品一区二三区 | 性色浪潮av| 久久久久成人精品无码 | 午夜久 | 在哪看毛片| 欧美午夜视频 | 欧美巨鞭大战丰满少妇 | 欧美精品一区二区三区四区五区 | 日韩av线上 | 欧美一级全黄 | 天堂99| 欧美成人吸奶水做爰 | 国产aⅴ精品一区二区果冻 台湾性生生活1 | 国产老头户外野战xxxxx | 成人午夜影片 | 天天插天天搞 | 蜜臀av性久久久久蜜臀aⅴ涩爱 | 秋霞av一区二区三区 | 毛片在哪看 |