顺序表的使用例题(杨辉三角、扑克牌)



1.杨辉三角

给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。
在这里插入图片描述

示例 1:

输入: numRows = 5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

示例 2:

输入: numRows = 1
输出: [[1]]

提示:

1 <= numRows <= 30

1.思路

我们可以把这个杨辉三角想成一个直角三角形,先方便我们解答,根据题目要求public List<List> generate(int numRows)我们可以想到利用二维数组的思路来解答这个题,每一行的第一个元素和最后一个元素都是1,这一行的中间值=上一行这一列的值+上一行上一列的值

在这里插入图片描述

2.完整代码

代码如下:

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<>();
        List<Integer> one = new ArrayList<>();
        one.add(1);
        ret.add(one);
        for(int i=1;i<numRows;i++) {
            List<Integer> curRows =new ArrayList<>();
            curRows.add(1);
            List<Integer> preRows = ret.get(i-1);
            for(int j=1;j<i;j++) {
                int value = preRows.get(j-1) + preRows.get(j);
                curRows.add(value);
            }
            curRows.add(1);
            ret.add(curRows);
        }
        return ret;
    }
}

在这里插入图片描述

2.扑克牌

一个简单扑克牌代码,可以实现初始化一副牌,洗牌,发牌操作。

1.定义一张牌这个类

一张牌有俩个属性,花色和数值

class Card {
    private String color;
    private int value;

    public Card(String color, int value) {
        this.color = color;
        this.value = value;
    }

    public String getColor() {
        return color;
    }

    public int getValue() {
        return value;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "[ "+ color+value+" ]";
    }
}

2.初始化一副牌

public  static  final  String[] colors = {"♥","♠","♣","♦"};

    public static List<Card> buycard() {
        List<Card> desk = new ArrayList<>();//一幅牌
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13 ; j++) {
                String color = colors[i];
                Card card = new Card(color,j);//一张牌具有花色和数值的牌
                desk.add(card);
            }
        }
        return desk;
    }

3.洗牌

洗牌的逻辑就是把一副崭新的牌的里每一张牌与其他的牌进行随机交换

   public static void shuffle(List<Card> cardList) {
        for (int i = cardList.size()-1; i > 0; i--) {
            Random random = new Random();
            int index = random.nextInt(i);
            swap(cardList,i,index);
        }
    }
    public static void swap(List<Card> cardList,int i,int j) {
        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

4.发牌

List<List<Card>> hands = new ArrayList<>();//一共的牌
        List<Card> hand1 = new ArrayList<>();//每个人手里的牌
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);
        //规则:每个人轮流揭够5张牌,一次揭1张牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                hands.get(j).add(cardList.remove(0));
                //将牌堆中的牌依次放在每个人手中,并且将这张牌从牌堆中移除
            }
        }

5.完整代码

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card {
    private String color;
    private int value;

    public Card(String color, int value) {
        this.color = color;
        this.value = value;
    }

    public String getColor() {
        return color;
    }

    public int getValue() {
        return value;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "[ "+ color+value+" ]";
    }
}

public class 扑克牌 {
    public  static  final  String[] colors = {"♥","♠","♣","♦"};

    public static List<Card> buycard() {
        List<Card> desk = new ArrayList<>();//一幅牌
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13 ; j++) {
                String color = colors[i];
                Card card = new Card(color,j);//一张牌
                desk.add(card);
            }
        }
        return desk;
    }
    public static void shuffle(List<Card> cardList) {
        for (int i = cardList.size()-1; i > 0; i--) {
            Random random = new Random();
            int index = random.nextInt(i);
            swap(cardList,i,index);
        }
    }
    public static void swap(List<Card> cardList,int i,int j) {
        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

    public static void main(String[] args) {
        List<Card> cardList = buycard();
        System.out.println("买牌后:"+cardList);
        shuffle(cardList);
        System.out.println("洗牌后:"+cardList);
        //发牌:每个人抓三张牌
        List<List<Card>> hands = new ArrayList<>();//一共的牌
        List<Card> hand1 = new ArrayList<>();//每个人手里的牌
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);
        //规则:每个人轮流揭够5张牌,一次揭1张牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                hands.get(j).add(cardList.remove(0));
                //将牌堆中的牌依次放在每个人中,并且将这张牌从牌堆中移除
            }
        }
        System.out.println("第一个人的牌:"+hand1);
        System.out.println("第二个人的牌:"+hand2);
        System.out.println("第三个人的牌:"+hand3);
        System.out.println("牌堆中剩余的牌:"+cardList);
    }

}

运行结果如下:
在这里插入图片描述

买牌后:[[ ♥1 ], [ ♥2 ], [ ♥3 ], [ ♥4 ], [ ♥5 ], [ ♥6 ], [ ♥7 ], [ ♥8 ], [ ♥9 ], [ ♥10 ], [ ♥11 ], [ ♥12 ], [ ♥13 ], [ ♠1 ], [ ♠2 ], [ ♠3 ], [ ♠4 ], [ ♠5 ], [ ♠6 ], [ ♠7 ], [ ♠8 ], [ ♠9 ], [ ♠10 ], [ ♠11 ], [ ♠12 ], [ ♠13 ], [ ♣1 ], [ ♣2 ], [ ♣3 ], [ ♣4 ], [ ♣5 ], [ ♣6 ], [ ♣7 ], [ ♣8 ], [ ♣9 ], [ ♣10 ], [ ♣11 ], [ ♣12 ], [ ♣13 ], [ ♦1 ], [ ♦2 ], [ ♦3 ], [ ♦4 ], [ ♦5 ], [ ♦6 ], [ ♦7 ], [ ♦8 ], [ ♦9 ], [ ♦10 ], [ ♦11 ], [ ♦12 ], [ ♦13 ]]
洗牌后:[[ ♥8 ], [ ♦10 ], [ ♥10 ], [ ♣3 ], [ ♠6 ], [ ♦6 ], [ ♥1 ], [ ♠1 ], [ ♣1 ], [ ♠4 ], [ ♣13 ], [ ♥11 ], [ ♦1 ], [ ♥12 ], [ ♦2 ], [ ♠11 ], [ ♣6 ], [ ♣10 ], [ ♣2 ], [ ♠10 ], [ ♥4 ], [ ♠8 ], [ ♠3 ], [ ♠9 ], [ ♦5 ], [ ♦4 ], [ ♣12 ], [ ♠13 ], [ ♥2 ], [ ♥13 ], [ ♥5 ], [ ♣8 ], [ ♣11 ], [ ♦11 ], [ ♠12 ], [ ♥9 ], [ ♠5 ], [ ♦8 ], [ ♣4 ], [ ♣5 ], [ ♦9 ], [ ♥3 ], [ ♠7 ], [ ♦3 ], [ ♣7 ], [ ♥7 ], [ ♦13 ], [ ♦7 ], [ ♦12 ], [ ♥6 ], [ ♣9 ], [ ♠2 ]]
第一个人的牌:[[ ♥8 ], [ ♣3 ], [ ♥1 ], [ ♠4 ], [ ♦1 ]]
第二个人的牌:[[ ♦10 ], [ ♠6 ], [ ♠1 ], [ ♣13 ], [ ♥12 ]]
第三个人的牌:[[ ♥10 ], [ ♦6 ], [ ♣1 ], [ ♥11 ], [ ♦2 ]]
牌堆中剩余的牌:[[ ♠11 ], [ ♣6 ], [ ♣10 ], [ ♣2 ], [ ♠10 ], [ ♥4 ], [ ♠8 ], [ ♠3 ], [ ♠9 ], [ ♦5 ], [ ♦4 ], [ ♣12 ], [ ♠13 ], [ ♥2 ], [ ♥13 ], [ ♥5 ], [ ♣8 ], [ ♣11 ], [ ♦11 ], [ ♠12 ], [ ♥9 ], [ ♠5 ], [ ♦8 ], [ ♣4 ], [ ♣5 ], [ ♦9 ], [ ♥3 ], [ ♠7 ], [ ♦3 ], [ ♣7 ], [ ♥7 ], [ ♦13 ], [ ♦7 ], [ ♦12 ], [ ♥6 ], [ ♣9 ], [ ♠2 ]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值