首页 > 编程语言 >算法——字符串(一)

算法——字符串(一)

时间:2023-06-05 18:33:08浏览次数:39  
标签:pre ListNode next 算法 l2 l1 字符串 null

1、两数相加

 1 class Solution {
 2     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 3         ListNode pre = new ListNode();
 4         ListNode cur = pre;
 5         int carry = 0;
 6         while(l1!=null||l2!=null||carry!=0){
 7             int x=0,y=0;
 8             if(l1!=null){
 9                 x=l1.val;
10                 l1=l1.next;
11             }
12             if(l2!=null){
13                 y=l2.val;
14                 l2=l2.next;
15             }
16             int sum=(x+y+carry)%10;
17             ListNode newnode=new ListNode(sum);
18             cur.next=newnode;
19             carry=(x+y+carry)/10;
20             cur=cur.next;
21 
22         }
23         return pre.next;
24     }
25 }
View Code

2、删除链表倒数第k个节点

快慢指针的思想

 1 class Solution {
 2     public ListNode removeNthFromEnd(ListNode head, int n) {
 3         ListNode dummy=new ListNode();
 4         ListNode pre=dummy;
 5         pre.next=head;
 6         ListNode fast=head;
 7         ListNode slow=head;
 8         int i=0;
 9         while(fast!=null&&i<n){
10             fast=fast.next;
11             i++;
12         }
13         while(fast!=null){
14             fast=fast.next;
15             pre=pre.next;
16             slow=slow.next;
17         }
18         pre.next=slow.next;
19         return dummy.next;
20 
21     }
22 }
View Code

3、合并两个有序链表

 1 class Solution {
 2     public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
 3         ListNode dummy=new ListNode();
 4         ListNode pre=dummy;
 5         //dummy.next=pre;
 6         while(list1!=null&&list2!=null){
 7             if(list1.val<=list2.val){
 8                 pre.next=list1;
 9                 list1=list1.next;
10                 pre=pre.next;
11             }else{
12                 pre.next=list2;
13                 list2=list2.next;
14                 pre=pre.next;
15             }
16 
17         }
18         if(list1!=null)pre.next=list1;
19         if(list2!=null)pre.next=list2;
20         return dummy.next;
21     }
22 }
View Code

4、合并K个有序链表

分治的思想

 1 class Solution {
 2    public ListNode mergeKLists(ListNode[] lists) {
 3         if (lists == null || lists.length == 0) return null;
 4         return merge(lists, 0, lists.length - 1);
 5     }
 6 
 7     private ListNode merge(ListNode[] lists, int left, int right) {
 8         if (left == right) return lists[left];
 9         int mid = left + (right - left) / 2;
10         ListNode l1 = merge(lists, left, mid);
11         ListNode l2 = merge(lists, mid + 1, right);
12         return mergeTwoLists(l1, l2);
13     }
14 
15     private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
16         if (l1 == null) return l2;
17         if (l2 == null) return l1;
18         if (l1.val < l2.val) {
19             l1.next = mergeTwoLists(l1.next, l2);
20             return l1;
21         } else {
22             l2.next = mergeTwoLists(l1,l2.next);
23             return l2;
24         }
25     }
26 }
View Code

5、两两交换链表的节点

 1 class Solution {
 2     public ListNode swapPairs(ListNode head) {
 3         ListNode dummy=new ListNode();
 4         dummy.next=head;
 5         ListNode pre=dummy,start=head,end=head;
 6         while(start!=null&&start.next!=null){
 7             end=start.next;
 8             ListNode nex=end.next;
 9             pre.next=end;
10             end.next=start;
11             start.next=nex;
12             pre=start;
13             start=nex;
14             
15         }
16         return dummy.next;
17     }
18 }
View Code

 

标签:pre,ListNode,next,算法,l2,l1,字符串,null
From: https://www.cnblogs.com/coooookie/p/17458693.html

相关文章

  • 获取字符串个数和长度
    SAP中strlen()只能计算字符串的个数,不能计算含有中文字符串的长度。FIELD-SYMBOLS:<FV>TYPESTRING.DATA:LV_SRTTYPEI.DATA:LVTYPEREFTODATA.DATA:LV_SSSSTYPECHAR255.LV_SSSS='我'.START-OF-SELECTION.CREATEDATALVTYPESTRING.ASSIGNLV->*TO<FV&g......
  • 字符串输出的两种调用方法
    classReprStr:def__repr__(self):#命令行交互环境,输入对象名回车,调用此方法。字符串真正的样子return"返回的是__repr__方法"def__str__(self):#用print输出变量时,调用此方法。经过Python优化,更便于人类阅读的样式return"返......
  • c++ 与c#之间的字符串传递
    1.方法中不要直接返回字符串,防止内存崩溃。c++写法:voidnecall(char*str1,char*outdata){strcpy(outdata,str1);}outdata为导出数c#写法:[DllImport("testdemo")]privatestaticexternvoidnecall(stringa,StringBuilderb);StringBuilderb=new......
  • 数据结构--Dijkstra算法最清楚的讲解
    迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径。它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想),直到扩展到终点为止###基本思想通过Dijkstra计算图G中的最短路径时,需要指定起点s(即从顶点s开始计算)。此外,引进两个集合S和U。S的......
  • c# – RichTextBox用表情符号/图像替换字符串
    在RichtTextBox中,我想用表情符号图像自动替换表情符号字符串(例如:D).我到目前为止工作,除了当我在现有的单词/字符串之间写出表情符号字符串时,图像会在行尾插入. 例如:你好(在这里插入:D)这是一条消息结果是:你好,这是一条消息☺<<图片另一个(微小的)问题是插入后的插入位置在插......
  • 树之深度优先遍历算法详解(DFS实现) LeetCode94
           本文以如下树结构为例深度优先(DeepFirstSearch)       树的孩子称作子树,对于一个树进行深度优先遍历,即将其某一子树下所有节点遍历完再去遍历其他子树。遍历的顺序以根为参照可分为先序遍历,中序遍历,后序遍历。遍历方式描述先序遍历根左右中序遍历左根右后......
  • Hash表算法
    第一部分:TopK算法详解问题描述百度面试题:   搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串的长度为1-255字节。   假设目前有一千万个记录(这些查询串的重复度比较高,虽然总数是1千万,但如果除去重复后,不超过3百万个。一个查询串的重复度越高,......
  • K-Means算法--聚类算法
    在数据挖掘中,K-Means算法是一种clusteranalysis的算法,其主要是来计算数据聚集的算法,主要通过不断地取离种子点最近均值的算法。问题K-Means算法主要解决的问题如下图所示。我们可以看到,在图的左边有一些点,我们用肉眼可以看出来有四个点群,但是我们怎么通过计算机程序找出这几个点群......
  • 【算法系列】淘汰算法之FIFO
    FIFO算法FIFO(Fistinfirstout)先进先出。FIFO算法的思想FIFO算法的描述FIFO算法的实现FIFOCacheFIFOCacheFIFOCacheTestFIFOCacheTestOutputOutput总结参考缓存算法(FIFO、LRU、LFU三种算法的区别)......
  • Hash算法系列-应用(负载均衡)
            现在的网站用户量都很大,一台服务器包打天下的时代一去不复返了,多台服务器就存在一个问题,如何将访问用户转向不同的服务器,并且各个服务器接受的请求数大致相当呢?这就是一致性hash算法要解决的问题。一致性hash算法在负载服务器(ngnix、haproxy等)、K/V缓存系统memca......