当前位置:   article > 正文

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

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

CardDemo类创建

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4. import java.util.concurrent.CopyOnWriteArrayList;
  5. public class CardDemo {
  6. public final String[] suits = {"♥","♠","♦","♣"};
  7. public List<Card> buyCard(){
  8. List<Card> cardList = new ArrayList<>();
  9. for (int i = 0; i < 4; i++) {
  10. for (int j = 1; j <= 13; j++) {
  11. Card card = new Card(suits[i],j);
  12. cardList.add(card);
  13. }
  14. }
  15. return cardList;
  16. }
  17. public void shuffle(List<Card> cardList){
  18. Random random = new Random();
  19. for (int i = cardList.size()-1; i > 0 ; i--) {
  20. int index = random.nextInt(i);
  21. swap(cardList,i,index);
  22. }
  23. }
  24. public void swap(List<Card> cardList,int a, int b){
  25. Card tmp = cardList.get(a);
  26. cardList.set(a,cardList.get(b));
  27. cardList.set(b,tmp);
  28. }
  29. public void getCard(List<Card> cardList){
  30. List<Card> hand1 = new ArrayList<>();
  31. List<Card> hand2 = new ArrayList<>();
  32. List<Card> hand3 = new ArrayList<>();
  33. List<List<Card>> hands = new ArrayList<>();
  34. hands.add(hand1);
  35. hands.add(hand2);
  36. hands.add(hand3);
  37. for (int i = 0; i < 5; i++){
  38. for (int j = 0; j < 3; j++){
  39. Card card = cardList.remove(i);
  40. hands.get(j).add(card);
  41. }
  42. }
  43. System.out.println("第一个揭牌如下");
  44. System.out.println(hand1);
  45. System.out.println("第二个揭牌如下");
  46. System.out.println(hand2);
  47. System.out.println("第三个揭牌如下");
  48. System.out.println(hand3);
  49. System.out.println("还剩的牌如下");
  50. System.out.println(cardList);
  51. }
  52. }

Card类创建

  1. public class Card {
  2. private String suit;//花色
  3. private int rank;//数字
  4. public Card(String suit, int rank) {
  5. this.suit = suit;
  6. this.rank = rank;
  7. }
  8. public String getSuit() {
  9. return suit;
  10. }
  11. public void setSuit(String suit) {
  12. this.suit = suit;
  13. }
  14. public int getRank() {
  15. return rank;
  16. }
  17. public void setRank(int rank) {
  18. this.rank = rank;
  19. }
  20. @Override
  21. public String toString() {
  22. return suit + ":" + rank + " ";
  23. }
  24. }

Test

  1. import java.util.List;
  2. public class Test {
  3. public static void main(String[] args) {
  4. CardDemo cardDemo = new CardDemo();
  5. List<Card> cardList = cardDemo.buyCard();
  6. System.out.println("买的牌如下");
  7. System.out.println(cardList);
  8. System.out.println("洗牌");
  9. cardDemo.shuffle(cardList);
  10. System.out.println(cardList);
  11. System.out.println("揭牌");
  12. cardDemo.getCard(cardList);
  13. }
  14. }

LeetCode

27. 移除元素

  1. class Solution {
  2. public int removeElement(int[] nums, int val) {
  3. List<Integer> List = new ArrayList<>();
  4. for (int i = 0; i < nums.length; i++){
  5. if(nums[i] == val){
  6. continue;
  7. }else {
  8. List.add(nums[i]);
  9. }
  10. }
  11. for (int i = 0; i < List.size(); i++){
  12. nums[i] = List.get(i);
  13. }
  14. int len = List.size();
  15. return len;
  16. }
  17. }

88. 合并两个有序数组

  1. class Solution {
  2. public static void merge(int[] nums1, int m, int[] nums2, int n) {
  3. List<Integer> list = new ArrayList<>();
  4. for (int i = 0; i < m; i++) {
  5. list.add(nums1[i]);
  6. }
  7. for (int i = 0; i < n; i++) {
  8. list.add(nums2[i]);
  9. }
  10. int gap = list.size();
  11. while(gap >= 1){
  12. shellSort(list,gap);
  13. gap /= 2;
  14. }
  15. for (int i = 0; i < m+n; i++){
  16. nums1[i] = list.get(i);
  17. }
  18. }
  19. public static void shellSort(List<Integer> list, int gap){
  20. for (int i = gap; i < list.size() ; i++) {
  21. int tmp = list.get(i);
  22. int j = i - gap;
  23. for (; j >= 0; j -= gap) {
  24. if(list.get(j) > tmp){
  25. list.set(j+gap, list.get(j));
  26. }else {
  27. break;
  28. }
  29. }
  30. list.set(j+gap, tmp);
  31. }
  32. }
  33. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/545128
推荐阅读
相关标签
  

闽ICP备14008679号