LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation
1. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
思路:模拟即可,思路不清楚时最好再纸上画画,确定节点指针的改变规则和顺序。交换顺序是这样的,用 cur 指针指向第一个,如果它不为空并且他的next也不为空则可以进行交换。交换时的步骤:(1)用temp暂存cur所指的第一个节点,(2)将cur指向第二个节点,也就是cur.next,(3)将第一个节点的next指向第二个节点的next,(4)第二个节点指向第一个节点,(5)将cur指向第三个节点,(6)这步容易忽略,要将改变后的第二个节点的next指向第四个节点(如果不为空的情况) 。这是因为一开始改变的时候,第一个节点前面没有节点,但是之后每个节点前面都是有节点的,所以之后不光要串连后面,还要串连前面。
public class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode cur = head;
ListNode newHead = head.next;
while (cur != null && cur.next != null) {
ListNode tmp = cur;
cur = cur.next;
tmp.next = cur.next;
cur.next = tmp;
cur = tmp.next;
// 第6步
if (cur != null && cur.next != null) tmp.next = cur.next;
}
return newHead;
}
}
2. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.
思路:可以使用加法来实现除法,但这题要注意的是一些临界情况,比如正负,溢出,除数被为0等情况。试了下结果Time Limit exceeded,而且这里发现了一个很有意思的问题,java中Math.abs(-2147483648)的返回值应该是什么?。那么改进下这个想法,不是一个一个加除数,而是成倍的增加除数。比如对于10/1,原来的算法是 1+1+1+1+1+1+1+1+1+1 总共循环10次,
LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation的更多相关文章
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode 题解]:Swap Nodes in Pairs
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetcode 题解 || Swap Nodes in Pairs 问题
problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- LeetCode OJ 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
随机推荐
- NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解
肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...
- Android如何在初始化的时候获取加载的布局的宽高
在自定义ListView中,需要将下拉刷新的View在初始化的时候设置padding隐藏起来,这时就要在初始化的时候获得要加载的布局View的高度. private View headView; he ...
- [洛谷P3628] [APIO2010]特别行动队
洛谷题目链接:[APIO2010]特别行动队 题目描述 你有一支由 n 名预备役士兵组成的部队,士兵从 1 到 \(n\) 编号,要将他们拆分 成若干特别行动队调入战场.出于默契的考虑,同一支特别行动 ...
- iOS Button设置
UIButton *kefuBtn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )]; kefuBtn.backgroundColor = SX ...
- 适配器模式 C#
适配器模式 将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. ● Target目标角色:该角色定义把其他类转换为何种接口,也就是我们的期望接 ...
- Item 2---遇到构造器具有多个参数时,要考虑用构建器;Builder模式
问题,面对这种一个构造器具备多个参数的问题,现有的做法是使用重叠构造器的方式,该方式存在的问题: public class NutritionFacts { private final int ser ...
- Vue 使用中的小技巧(山东数漫江湖)
在vue的使用过程中会遇到各种场景,当普通使用时觉得没什么,但是或许优化一下可以更高效更优美的进行开发.下面有一些我在日常开发的时候用到的小技巧,在下将不定期更新~ 1. 多图表resize事件去中心 ...
- floyd骚操作——传递闭包
传递闭包的含义指通过传递性推导出尽量多的元素之间的关系,而传递闭包一般都是采用floyd算法. 下面用两道题来实现传递闭包: Problem 1(POJ3660): 题目链接:http://poj.o ...
- ASP.NET MVC各个版本区别
ASP.NET MVC 1 view接收用户输入,把命令传到controller controller处理命令,更新model model被更新后,会通知view需要update view更新后向用户 ...
- .net XmlHelper xml帮助类
using System.Data; using System.IO; using System.Xml; using System.Xml.Serialization; /// <summar ...