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