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 ...
随机推荐
- sourcemap总结
sourcemap在线上压缩文件调试中很重要,在此总结如下: 1. 开启sourcemap (1). 浏览器要开启source-map支持(2). 压缩文件底部要有source-map的URL,压缩要 ...
- CentOS 6.5 下安装 QT 4
1. 获取下载地址 从我到网盘下载: qt-creator-opensource-linux-x86_64-3.5.1.run qt-everywhere-opensource ...
- [10.18模拟赛] 序列 (DP)
[10.18模拟赛] 序列 题目描述 山山有一个整数序列s1,s2,-,sn,其中1≤si≤k. 求出有多少个准确移除m个元素后不同的序列.答案模(1e9+7) 输入 输入包括几个测试用例,并且由文件 ...
- 集成淘宝sdk
204是安全图片的问题, 请先检测以下几点: .请检测百川控制台是否已经申请初级API. .请检测百川控制台“我的产品后台”是否开通电商SDK应用.(重点检测很多用户疏忽这一点) .debug版本的可 ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- Android 一些系统参数的获取
//获取网络类型 2G/3G/WIFI public String getNetworkType(){ String mNetWorkType = ""; Connectivity ...
- poi-对于word的操作(二)
poi对于word文本的底纹和下划线的样式的展现 package poi.test; import java.io.FileOutputStream; import java.math.BigInte ...
- RDLC - 后台代码直接导出Excel/PDF/Word格式
最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...
- MVP应用在android app上
使用MVP模式来解耦activity中业务代码和界面代码.在activity中,将其中的业务抽象到presenter层:将其中的界面代码抽象到View层. MVP模式: 一个软件被划分成三层,View ...
- Billboard HDU 2795 (线段树)
题目链接 Problem Description At the entrance to the university, there is a huge rectangular billboard of ...