入门基础5
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类的实例。
实现Demo:
public class Code4_CatDogQueue {
public static class Pet {
private String type;
public Pet(String type) {
this.type = type;
}
public String getPetType() {
return this.type;
}
}
public static class Dog extends Pet {
public Dog() {
super("dog");
}
}
public static class Cat extends Pet {
public Cat() {
super("cat");
}
}
public static class PetEnterQueue{
private Pet pet;
private long count;
public PetEnterQueue(Pet pet,long count){
this.pet = pet;
this.count = count;
}
public Pet getPet(){
return this.pet;
}
public long getCount(){
return this.count;
}
public String getEnterType(){
return this.pet.getPetType();
}
}
public static class CatDogQueue{
private Queue<PetEnterQueue> dogQ;
private Queue<PetEnterQueue> catQ;
private long count;
public CatDogQueue(){
this.dogQ = new LinkedList<PetEnterQueue>();
this.catQ = new LinkedList<PetEnterQueue>();
this.count = 0;
}
public void add(Pet pet){
if(pet.getPetType().equals("cat")){
catQ.add(new PetEnterQueue(pet,count++));
} else if(pet.getPetType().equals("dog")){
dogQ.add(new PetEnterQueue(pet,count++));
}else {
throw new RuntimeException("error,no such type");
}
}
public Pet pollAll(){
//这道题的精华所在,到底是谁先进来队列中?在这里先弹出的应该是谁?
if(!dogQ.isEmpty() && !catQ.isEmpty()){
if(dogQ.peek().getCount() < catQ.peek().getCount()){
return dogQ.poll().getPet();
} else {
return catQ.poll().getPet();
}
}else if (!dogQ.isEmpty()){
return dogQ.poll().getPet();
}else if (!catQ.isEmpty()){
return catQ.poll().getPet();
}else {
throw new RuntimeException("error!the queue is empty");
}
}
public Cat pollCat(){
if(!catQ.isEmpty()){
return (Cat) catQ.poll().getPet();
}else {
throw new RuntimeException("error!the cat queue is empty");
}
}
public Dog pollDog(){
if(!dogQ.isEmpty()){
return (Dog) dogQ.poll().getPet();
}else {
throw new RuntimeException("error!the cat queue is empty");
}
}
public boolean isDogEmpty(){
if(!dogQ.isEmpty()){
return false;
}else {
return true;
}
}
public boolean isCatEmpty(){
if(catQ.isEmpty()){
return true;
}else {
return false;
}
}
public boolean isEmpty(){
if(catQ.isEmpty() && dogQ.isEmpty()){
return true;
}else {
return false;
}
}
}
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);
test.add(dog1);
test.add(cat1);
test.add(dog2);
test.add(cat2);
test.add(dog3);
test.add(cat3);
test.add(dog1);
test.add(cat1);
test.add(dog2);
test.add(cat2);
test.add(dog3);
test.add(cat3);
while (!test.isDogEmpty()) {
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)。
题目图解:
实现思路:
实现代码:
public class Code5_RotatePrintMatrix {
public static void rotatePrint(int[][] matrix){
int tR = 0;//记录左上角的行号
int tC = 0;//记录左上角的列号
int dR = matrix.length - 1;//记录右下角的行号
int dC = matrix[0].length - 1;//记录右下角的列号
while (tR <= dR && tC <= dC){
//一层一层地打印
printEdge(matrix,tR++,tC++,dR--,dC--);
}
}
public static void printEdge(int[][] matrix,int tR,int tC,int dR,int dC){
//打印每一层的边界
int curR = tR;//记录下当前行
int curC = tC;//记录下当前列
if(tR == dR){
//如果矩阵是横条状的执行
printMatrix(matrix);//直接打印
}else if(tC == dC){
//如果矩阵是竖条状的执行
printMatrix(matrix);
}else {
//一般矩阵执行
while(curC < dC){
//按图顺序打印
System.out.print(matrix[tR][curC++] + " ");
}
while(curR < dR){
System.out.print(matrix[curR++][dC] + " ");
}
while(tC < curC){
System.out.print(matrix[dR][curC--] + " ");
}
while(tR < curR){
System.out.print(matrix[curR--][tC] + " ");
}
}
}
public static void printMatrix(int[][] matrix){
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 main(String[] args) {
int[][] matrix = {
{
1