7.8~7.10练习

news2024/9/22 19:31:00

目录

1.扑克牌游戏

2.链表基本功能的实现(单项链表)

 3.移除链表元素力扣

4.反转链表力扣

5.链表的中间结点

5.返回倒数第k个节点​编辑

6.合并两个有序链表

 7.链表基本功能的实现(双向链表)

8.链表分割


1.扑克牌游戏


public class Card {
 private String suit;
 private int rank;

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

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

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


public class CardDemo {
  public static final String[] stuits={"♠","方块","♥","♣"};
  public List<Card> buyCard() {
      List<Card> cardList = new ArrayList<>();
      for (int i = 0; i < 13; i++) {
          for (int j = 0; j < 4; j++) {
              int rank = i;
              String suit = stuits[j];
              Card card = new Card(rank,suit);
              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);
        }
    }
    private 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 List<List<Card>> play(List<Card>cardList){
      List<Card> hand0 = new ArrayList<>();
      List<Card> hand1 = new ArrayList<>();
      List<Card> hand2 = new ArrayList<>();
      List<List<Card>> hand = new ArrayList<>();
      hand.add(hand0);
      hand.add(hand1);
      hand.add(hand2);
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);
                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
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(cardList);
        //洗牌
       cardDemo.shuffle(cardList);
       //轮流发牌
      List <List<Card>> ret = cardDemo.play(cardList);
        for (int i = 0; i < ret.size(); i++) {
            System.out.println("第"+(i+1)+"个人的牌"+ret.get(i));
        }
    }
}

2.链表基本功能的实现(单项链表)

public class Test {
    public static void main(String[] args) {
        MySingleList list = new MySingleList();
        list.CreatList();
        list.clear();
        list.display();
    }
}

public class MySingleList implements IList {

    static  class ListNode{
        private int val;
        private ListNode next;
        public   ListNode(int val){
            this.val = val;
        }

    }
    public ListNode head;
    public void CreatList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(12);
        ListNode node3 = new ListNode(23);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;

    }
    @Override
    public void addFirst(int data) {
       ListNode node = new ListNode(data);
       node.next = head;
       head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
            return;
        }
        while(cur.next != null ){
            cur = cur.next;
        } cur.next = node;


    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if(index < 0 || index > len){
            System.out.println("index位置不合法");
            return;
        }
            if( index == 0){
               addFirst(data);
               return;
            }if(index == len){
                addLast(data);
                return;
        }
            ListNode node = new ListNode(data);
            ListNode cur =head;
            while(index - 1 != 0){
                cur = cur.next;
                index--;
            }
            node.next = cur.next;
              cur.next = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }else
                cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            return;
        }
       if(head.val == key){
           head = head.next;
           return;
       }
       ListNode cur = findNodeOfKey(key);
       if(cur == null){
           return;
       }
       cur.next = cur.next.next;
    }
   private ListNode findNodeOfKey(int key){
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
   }


    @Override
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while (cur != null) {
            if (cur.val == key) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }

        }
        if (head.val == key){
            head = head.next;
      }
    }
    @Override
    public int size() {
        ListNode cur = this.head;
        int len = 0;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        System.out.println();
       return len;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while ((cur != null)){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
}
public interface IList {

     public void addFirst(int data);

     public void addLast(int data);

     public void addIndex(int index,int data);

     public boolean contains (int key);

     public void remove(int key);

     public void removeAllKey(int key);

     public int size();
     public void clear();
     public void display();


}

 3.移除链表元素力扣

public class Solution {

        public ListNode removeElements(ListNode head, int val) {

            if(head == null){
                return head;
            }
            ListNode pre = head;
            ListNode cur = head.next;
            while(cur != null){
                if(cur.val == val){
                    pre.next = cur.next;
                    cur = cur.next;
                }else{
                    pre = cur;
                    cur = cur.next;
                }
            }
            if(head.val == val){
                head = head.next;
            }
            return head;
        }
    }

4.反转链表力扣

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
       return head;
    }
}

5.链表的中间结点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        if(head == null){
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
    return slow;

    }
}

5.返回倒数第k个节点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int kthToLast(ListNode head, int k) {
        if(head == null){
            return head.val;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = k - 1;
        while(count != 0){
            fast = fast.next;
            count--;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }
}

6.合并两个有序链表

 /**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
         ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
       while(list1 != null && list2 != null){
          if(list1.val <= list2.val){
              tmp.next = list1;
              list1 = list1.next;
              tmp = tmp.next;
          }else{
              tmp.next = list2;
              tmp = tmp.next;
              list2 = list2.next;
          }
      }
       if(list1 != null){
           tmp.next = list1;
       }
       if(list2 != null){
           tmp.next = list2;
       }
       return newHead.next;
    }
}

 7.链表基本功能的实现(双向链表)

public class test {
       public static MyLinkedList.ListNode  mergeTwoLists(MyLinkedList.ListNode list1,MyLinkedList. ListNode list2) {
             MyLinkedList. ListNode newHead = new MyLinkedList.ListNode(-1);
             MyLinkedList. ListNode tmp = newHead;
              while(list1 != null && list2 != null){
                     if(list1.val < list2.val){
                            tmp.next = list1;
                            list1 = list1.next;
                            tmp = tmp.next;
                     }
                     else{
                            tmp.next = list2;
                            tmp = tmp.next;
                            list2 = list2.next;
                     }
              }
              if(list1 != null){
                     tmp.next = list1;
              }
              if(list2 != null){
                     tmp.next = list2;
              }
              return newHead.next;
       }
    public static void main(String[] args) {

           MyLinkedList myLinkedList = new MyLinkedList();
           myLinkedList.addLast(1);
           myLinkedList.addLast(5);
           myLinkedList.addLast(9);
           myLinkedList.addLast(21);
           myLinkedList.addLast(7);
           myLinkedList.addLast(21);
           MyLinkedList myLinkedList1 =new MyLinkedList();
           myLinkedList1.addLast(1);
           myLinkedList1.addLast(5);
           myLinkedList1.addLast(9);
           myLinkedList1.addLast(21);
           myLinkedList1.addLast(7);
           myLinkedList1.addLast(21);

           MyLinkedList.ListNode newHead =  mergeTwoLists( myLinkedList.head,myLinkedList1.head);
           myLinkedList.display2(newHead);
    }
}
public class MyLinkedList implements IList{
    static class ListNode {
        public int val;
        public ListNode preV;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

    }
    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
       ListNode cur = head;
       ListNode node = new ListNode(data);
       if(head.next == null){
           head = last = node;
       }else{
           node.next = cur;
           cur.preV = node;
           head = node;
       }
    }

    @Override
    public void addLast(int data) {
        ListNode cur = head;
        ListNode node = new ListNode(data);
        if(head == null) {
            head = last = node;
        }else{
            last.next = node;
            node.preV = last;
            last = node;
        }

        }

    @Override
    //在指点位置增删元素
    public void addIndex(int index, int data) {
        int len = size();
        if(index < 0 || index > len ){
            System.out.println("不符合条件");
            return;
        }if(index == 0){
            addFirst(index);
            return;
        }if(index == len){
            addLast(data);
            return;
        }else{
            ListNode cur = findOfKey(index);
            ListNode node = new ListNode(data);
            node.next = cur;
           node.preV = cur.preV;
            cur.preV.next = node;
           cur.preV = node;
        }

    }
    private ListNode findOfKey(int index){
        ListNode cur = head;
        while(index != 0){
           cur = cur.next;
           index--;
        }
        return cur;
    }


    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key)
            {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
       ListNode cur = head;
       while(cur != null) {
           if (cur.val == key) {
               if (cur == head) {
                   head = head.next;
                   if (head != null) {
                       head.preV = null;
                   }
               } else {
                   cur.preV.next = cur.next;
                   if ( cur.next == null) {
                       last = last.preV;
                   }else {
                       cur.next.preV = cur.preV;
                   }
               }
           }
           cur =cur.next;
       }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.preV = null;
                    }
                }
            else {
                    cur.preV.next = cur.next;
                    if (cur.next == null) {
                        last = last.preV;
                    }
                    cur.next.preV = cur.preV;
                }
            }
            cur = cur.next;
        }

    }



    @Override
    public int size() {
        ListNode cur = head;
        int len = 0;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        if(cur != null){
            ListNode curN = cur.next;
            cur.preV = null;
            cur.next = null;
            cur = curN;
        }
        head = null;
        last = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //在指定位置进行打印
    public void display2(ListNode newHead) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

        public ListNode reverseList() {
            if(head == null){
                return head;
            }
            ListNode cur = head.next;
            head.next = null;
            while(cur != null){
                ListNode curN = cur.next;
                cur.next = head;
                head = cur;
                cur = curN;
            }
            return head;
        }

    
    }
public interface IList {

     public void addFirst(int data);

     public void addLast(int data);

     public void addIndex(int index,int data);

     public boolean contains (int key);

     public void remove(int key);

     public void removeAllKey(int key);

     public int size();
     public void clear();
     public void display();


}

8.链表分割

import java.util.*;

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Partition {
    public ListNode partition(ListNode pHead, int x) {
         ListNode as = null;
        ListNode ae = null;
        ListNode bs = null;
        ListNode be = null;
        ListNode cur = pHead;
        while(cur != null){
            if(cur.val < x){
                if(as == null){
                    as = ae = cur;
                }else{
                      ae.next = cur;
                      ae = ae.next;
                }
            }else{
                if(bs == null){
                     bs = be =cur;
                }else{
                     be.next = cur;
                     be = be.next;
                }
            }
            cur = cur.next;
        }
        if(as == null){
            return bs;
        }
         ae.next = bs;
        if(bs != null){
            be.next = null;
        }
            return as;
    }
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1922776.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

新手教学系列——高效管理MongoDB数据:批量插入与更新的实战技巧

前言 在日常开发中,MongoDB作为一种灵活高效的NoSQL数据库,深受开发者喜爱。然而,如何高效地进行数据的批量插入和更新,却常常让人头疼。今天,我们将一起探讨如何使用MongoDB的bulk_write方法,简化我们的数据管理流程,让代码更加简洁高效。 常规做法:find、insertone…

LabVIEW扬尘控制系统

设计了一套基于LabVIEW的扬尘控制系统&#xff0c;通过监测TsP&#xff08;总悬浮颗粒物&#xff09;浓度、风向和摄像头视频&#xff0c;实现对环境的综合监控和扬尘控制。系统可以自动判断扬尘位置&#xff0c;并驱动抑尘设备进行抑尘。硬件选用NI cDAQ-9178数据采集模块、Om…

9.5 栅格图层符号化多波段彩色渲染

文章目录 前言多波段彩色渲染QGis设置为多波段彩色二次开发代码实现多波段彩色 总结 前言 介绍栅格图层数据渲染之多波段彩色渲染说明&#xff1a;文章中的示例代码均来自开源项目qgis_cpp_api_apps 多波段彩色渲染 以“3420C_2010_327_RGB_LATLNG.tif”数据为例&#xff0c…

26.7 Django单表操作

1. 模型管理器 1.1 Manager管理器 Django ORM中, 每个Django模型(Model)至少有一个管理器, 默认的管理器名称为objects. objects是一个非常重要的管理器(Manager)实例, 它提供了与数据库进行交互的接口.通过管理器, 可以执行数据库查询, 保存对象到数据库等操作.objects管理器…

MT6825磁编码IC在智能双旋机器人中的应用

MT6825磁编码IC在智能双旋机器人中的应用&#xff0c;无疑为这一领域的创新和发展注入了新的活力。作为一款高性能的磁性位置传感器&#xff0c;MT6825以其独特的优势&#xff0c;在智能双旋机器人的运动控制、定位精度以及系统稳定性等方面发挥了关键作用。 www.abitions.com …

【Web开发手礼】探索Web开发的魅力(三)-html基础标签(3)

上述主要是对html标签的介绍和一些基本练习可以当作日常笔记收藏一下&#xff01;&#xff01;&#xff01; 目录 前言 html基础标签 前言 上述主要是对html标签的介绍和一些基本练习可以当作日常笔记收藏一下&#xff01;&#xff01;&#xff01; 提示&#xff1a;以下是本…

Kithara与OpenCV (二)

Kithara使用OpenCV QT 进行特征检测 目录 Kithara使用OpenCV QT 进行特征检测OpenCV 特征检测简介Qt应用框架简介项目说明关键代码抖动测试测试平台&#xff1a;测试结果&#xff1a;结论 OpenCV 特征检测简介 OpenCV是一个开源的计算机视觉库&#xff0c;提供了各种图像处理…

WordPress 主题技巧:给文章页增加“谁来过”模块。

模块功能&#xff1a; 我个人目前在做一个电影类的网站&#xff0c;在开发文章页的模版时候&#xff0c;突然觉得给文章页增加一个“谁对本电影感兴趣”的功能模块可能会比较有趣&#xff0c;这个功能有点类似于‘足迹’的感觉&#xff0c;用户可以通过这个功能&#xff0c;发…

昇思25天学习打卡营第14天|K近邻算法实现红酒聚类

红酒Wine数据集 类别(13类属性)&#xff1a;Alcohol&#xff0c;酒精&#xff1b;Malic acid&#xff0c;苹果酸 Ash&#xff0c;灰&#xff1b;Alcalinity of ash&#xff0c;灰的碱度&#xff1b; Magnesium&#xff0c;镁&#xff1b;Total phenols&#xff0c;总酚&#xf…

tkinter-TinUI-xml实战(12)pip可视化管理器

引言 pip命令行工具在平常使用方面确实足够简单&#xff0c;本项目只是作为TinUI多界面开发的示例。 当然&#xff0c;总有人想用GUI版pip&#xff0c;实际上也有。不过现在&#xff0c;我们就来手搓一个基于python和TinUI&#xff08;tkinter&#xff09;的pip可视化管理器。…

102.qt qml-最全Table交互之多列固定、行列拖拽、自定义委托、标题交互使用教程

自定义实现的Table控件&#xff0c;支持跨qt版本&#xff0c;兼容qt5,qt6&#xff01; 截图如下所示: 黑色风格如下所示&#xff1a; 视频演示入口&#xff1a;Qt QML QianWindowV2.5(新增曲线综合示例、QML最全Table交互示例、支持qt5/qt6)_哔哩哔哩_bilibili 1.示例页面入口…

Python爬虫技术从去哪儿网获取旅游数据,对攻略进行可视化分析,提供全面的旅游攻略和个性化的出行建议

背景 随着信息技术的快速发展和互联网的普及&#xff0c;旅游行业也迎来了数字化和智能化的变革。去哪儿网作为中国领先的在线旅游平台之一&#xff0c;提供了丰富的旅游产品和服务&#xff0c;涵盖了机票、酒店、旅游度假等各个方面。用户通过去哪儿网可以方便地查询、预订和…

羧基聚乙二醇生物素的制备方法;COOH-PEG-Biotin

羧基聚乙二醇生物素&#xff08;COOH-PEG-Biotin&#xff09;是一种常见的生物分子聚合物&#xff0c;具有多种应用&#xff0c;特别是在生物实验、药物研发和生物技术等领域。以下是对该化合物的详细解析&#xff1a; 一、基本信息 名称&#xff1a;羧基聚乙二醇生物素&#x…

C/C++ 进阶(7)模拟实现map/set

个人主页&#xff1a;仍有未知等待探索-CSDN博客 专题分栏&#xff1a;C 一、简介 map和set都是关联性容器&#xff0c;底层都是用红黑树写的。 特点&#xff1a;存的Key值都是唯一的&#xff0c;不重复。 map存的是键值对&#xff08;Key—Value&#xff09;。 set存的是键…

一图展示免费开源的分布式版本控制系统​Git

文章目录 前言一、安装Git二、Git配置三、git命令 前言 Git是一个开源的分布式版本控制系统&#xff0c;可以有效、高速地处理从很小到非常大的项目版本管理。也是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件。 一、安装Git Windows操作系统…

端到端拥塞控制的本质

昨天整理了一篇 bbr 的微分方程组建模(参见 bbr 建模)&#xff0c;算是 bbr 算法终极意义上的一个总结&#xff0c;最后也顺带了对 aimd 的描述&#xff0c;算是我最近比较满意的一篇分享了。那么接下来的问题&#xff0c;脱离出具体算法&#xff0c;上升到宏观层面&#xff0c…

【Redis】复制(Replica)

文章目录 一、复制是什么&#xff1f;二、 基本命令三、 配置&#xff08;分为配置文件和命令配置&#xff09;3.1 配置文件3.2 命令配置3.3 嵌套连接3.4 关闭从属关系 四、 复制原理五、 缺点 以下是本篇文章正文内容 一、复制是什么&#xff1f; 主从复制 master&#xff…

【UE5.1 角色练习】15-枪械射击——子弹发射物

目录 效果 步骤 一、创建并发射子弹 二、优化子弹 效果 步骤 一、创建并发射子弹 1. 在前面的文章中&#xff08;【UE5.1 角色练习】06-角色发射火球-part1&#xff09;我们创建了蓝图“BP_Skill_FireBall” 这里我们复制一份命名为“BP_Ammo_5mm”&#xff0c;用于表示…

OpenAI终止对中国提供API服务,对国内AI市场产生重大冲击?

6月25日&#xff0c;OpenAI突然宣布终止向包括中国在内的国家地区提供API服务&#xff0c;本月9日这一政策已经正式生效了&#xff01; 有人说&#xff0c;这个事件给中国AI行业带来很大冲击&#xff01;是这样吗&#xff1f;在展开讨论前&#xff0c;我们先来看看什么是API服务…

详解如何通过稀疏向量优化信息检索

在信息检索方法的发展历程中&#xff0c;我们见证了从传统的统计关键词匹配到如 BERT 这样的深度学习模型的转变。虽然传统方法提供了坚实的基础&#xff0c;但往往难以精准捕捉文本的语义关系。如 BERT 这样的稠密检索方法通过利用高维向量捕获文本的上下文语义&#xff0c;为…