简单的洗牌算法Java加LeetCode刷题

文章介绍了如何在Java中创建CardDemo类,包括买卡、洗牌和发牌的方法,以及与之相关的Card类。还展示了如何使用ArrayList和随机数生成器进行操作,并引用了LeetCode中的两个算法问题作为扩展。
摘要由CSDN通过智能技术生成

CardDemo类创建

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

public class CardDemo {
    public final String[] suits = {"♥","♠","♦","♣"};
    public List<Card> buyCard(){
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(suits[i],j);
                cardList.add(card);
            }
        }
        return cardList;
    }
    public void shuffle(List<Card> cardList){
        Random random = new Random();
        for (int i = cardList.size()-1; i > 0 ; i--) {
            int index = random.nextInt(i);
            swap(cardList,i,index);
        }
    }
    public void swap(List<Card> cardList,int a, int b){
        Card tmp = cardList.get(a);
        cardList.set(a,cardList.get(b));
        cardList.set(b,tmp);
    }
    public void getCard(List<Card> cardList){
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        List<List<Card>> hands = new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);
        for (int i = 0; i < 5; i++){
            for (int j = 0; j < 3; j++){
                Card card = cardList.remove(i);
                hands.get(j).add(card);
            }
        }
        System.out.println("第一个揭牌如下");
        System.out.println(hand1);
        System.out.println("第二个揭牌如下");
        System.out.println(hand2);
        System.out.println("第三个揭牌如下");
        System.out.println(hand3);
        System.out.println("还剩的牌如下");
        System.out.println(cardList);
    }
}

Card类创建

public class Card {
    private String suit;//花色
    private int rank;//数字

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return suit + ":" + rank + " ";
    }
}

Test

import java.util.List;

public class Test {
    public static void main(String[] args) {
        CardDemo cardDemo = new CardDemo();
        List<Card> cardList = cardDemo.buyCard();
        System.out.println("买的牌如下");
        System.out.println(cardList);
        System.out.println("洗牌");
        cardDemo.shuffle(cardList);
        System.out.println(cardList);
        System.out.println("揭牌");
        cardDemo.getCard(cardList);
    }
}

LeetCode

27. 移除元素

class Solution {
   public int removeElement(int[] nums, int val) {
          List<Integer> List = new ArrayList<>();
        for (int i = 0; i < nums.length; i++){
            if(nums[i] == val){
                continue;
            }else {
                List.add(nums[i]);
            }
            
        }
        for (int i = 0; i < List.size(); i++){
            nums[i] = List.get(i);
        }
        int len = List.size();
        return len;
    }
}

88. 合并两个有序数组

class Solution {
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            list.add(nums1[i]);
        }
        for (int i = 0; i < n; i++) {
            list.add(nums2[i]);
        }
        int gap = list.size();
        while(gap >= 1){
            shellSort(list,gap);
            gap /= 2;
        }

        for (int i = 0; i < m+n; i++){
            nums1[i] = list.get(i);
        }
    }
    public static void shellSort(List<Integer> list, int gap){
        for (int i = gap; i < list.size() ; i++) {
            int tmp = list.get(i);
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if(list.get(j) > tmp){
                    list.set(j+gap, list.get(j));
                }else {
                    break;
                }
            }
            list.set(j+gap, tmp);
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值