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

时间:2024-05-04 16:39:01

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);
        }
    }
}