当前位置:网站首页>Day34 LeetCode
Day34 LeetCode
2022-08-02 03:07:00 【太阳在坠落】
1. 回文链表
给定一个链表的 头节点 head ,请判断其是否为回文链表。如果一个链表是回文,那么链表节点序列从前往后看和从后往前看是相同的。
分析:
使用数组List把链表中节点的值复制,再用双指针遍历判断是否为回文。
class Solution {
public boolean isPalindrome(ListNode head) {
List<Integer> vals = new ArrayList<Integer>();
// 将链表的值复制到数组中
ListNode currentNode = head;
while (currentNode != null) {
vals.add(currentNode.val);
currentNode = currentNode.next;
}
// 使用双指针判断是否回文
int front = 0;
int back = vals.size() - 1;
while (front < back) {
if (!vals.get(front).equals(vals.get(back))) {
return false;
}
front++;
back--;
}
return true;
}
}
2. 重排链表
给定一个单链表 L 的头节点 head ,单链表 L 表示为:
L0 → L1 → … → Ln-1 → Ln
请将其重新排列后变为:
L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …
不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
分析:
先将链表转化为数组,然后再利用双指针进行重排。
class Solution {
public void reorderList(ListNode head) {
List<ListNode> list = new ArrayList<>();
while (head != null){
list.add(head);
head = head.next;
}
int i = 0, j = list.size()-1;
while (i < j){
list.get(i).next = list.get(j);
i++;
if (i == j) break;
list.get(j).next = list.get(i);
j--;
}
list.get(i).next = null;
}
}
3. 链表中的两数相加
给定两个非空链表 l1和 l2 来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
可以假设除了数字 0 之外,这两个数字都不会以零开头。
分析:
借助栈来实现,建两个栈然后把遍历两个链表并把节点分别存入栈中,然后就是加法运算,与数组的加法运算类似。
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
while (l1 != null) {
stack1.push(l1.val);
l1 = l1.next;
}
while (l2 != null){
stack2.push(l2.val);
l2 = l2.next;
}
int add = 0;
ListNode head = null;
while (!stack1.isEmpty() || !stack2.isEmpty() || add != 0){
int a = stack1.isEmpty()?0:stack1.pop();
int b = stack2.isEmpty()?0:stack2.pop();
ListNode node = new ListNode((a+b+add)%10);
add = (a+b+add)/10;
node.next = head;
head = node;
}
return head;
}
}
4. 反转链表
给定单链表的头节点 head ,请反转链表,并返回反转后的链表的头节点。
分析:
双指针+迭代。
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null) return null;
ListNode pre = null, cur = head;
while (cur !=null){
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
5. 生成每种字符都是奇数个的字符串
给你一个整数 n,请你返回一个含 n 个字符的字符串,其中每种字符在该字符串中都恰好出现 奇数次 。返回的字符串必须只含小写英文字母。如果存在多个满足题目要求的字符串,则返回其中任意一个即可。
分析:
判断一下n的奇偶性,然后生成字符串。
class Solution {
public String generateTheString(int n) {
StringBuffer stringBuffer = new StringBuffer();
if (n%2 == 0){
for (int i = 0; i < n-1; i++) {
stringBuffer.append("a");
}
stringBuffer.append("b");
return stringBuffer.toString();
}
for (int i = 0; i < n; i++) {
stringBuffer.append("a");
}
return stringBuffer.toString();
}
}
边栏推荐
- 【LeetCode】102. Level order traversal of binary tree
- ROS2自学笔记:launch文件完整编写流程
- Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
- mysql8.0.28 download and installation detailed tutorial, suitable for win11
- 深度学习:目标检测入门知识
- 有人知道HTML怎么到MYSQL数据库吗? (NODEJS)
- JSP Webshell free kill
- Nacos source code analysis topic (2) - service registration
- Common SQL interview questions: 50 classic examples
- 一种基于行为空间的回声状态网络参数优化方法
猜你喜欢
随机推荐
【无标题】【Koltin Flow(三)】Flow操作符之中间操作符(二)
svm.SVC application practice 1--Breast cancer detection
7-40 奥运排行榜 (25 分)多项排序
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL Web框架Gin(九)
运维理想和现实,你是?
请教各位大佬,如果我代码里面设置了,这个id我在什么地方可以查到呢?连接到mysql cluste
dropout
基于时延估计的动力型下肢假肢分段控制策略研究
8万字带你入门Rust
OperatingSystemMXBean to get system performance metrics
EF Core:基于关系的复杂查询 区分IEnumerable和IQueryable
什么是轮式里程计
JSP WebSehll 后门脚本
ASP WebShell 后门脚本与免杀
mysql8.0.28下载和安装详细教程,适配win11
嵌入式分享合集25
2W字!详解20道Redis经典面试题!(珍藏版)
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
MySQL中根据日期进行范围查询
MySQL八股文背诵版