LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
思路:链表移动的题目,要求遍历一遍完成。可以用这个移动模式来解决,对于要移动的区间m~n,往右移动第一个元素,后面的元素添加到最前面,比如2-3-4,左移一次是3-2-4,2再往右移是4-3-2,这样就完成了链表的逆置。
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null) return null;
ListNode dummy = new ListNode(0); // create a dummy node to mark the head of this list
dummy.next = head;
ListNode pre = dummy; // make a pointer pre as a marker for the node before reversing
for(int i = 0; i<m-1; i++) pre = pre.next;
ListNode start = pre.next; // a pointer to the beginning of a sub-list that will be reversed
ListNode then = start.next; // a pointer to a node that will be reversed
// 1 - 2 -3 - 4 - 5 ; m=2; n =4 ---> pre = 1, start = 2, then = 3
// dummy-> 1 -> 2 -> 3 -> 4 -> 5
// 难点在于具体怎么实现那个移动模式
for(int i=0; i<n-m; i++)
{
start.next = then.next;
then.next = pre.next;
pre.next = then;
then = start.next;
}
// first reversing : dummy->1 - 3 - 2 - 4 - 5; pre = 1, start = 2, then = 4
// second reversing: dummy->1 - 4 - 3 - 2 - 5; pre = 1, start = 2, then = 5 (finish)
return dummy.next;
}
2. Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given "25525511135",
return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
思路:首先要明白的是什么是一个valid ip地址,即四位中的每一个少于3位数,值小于255,并且不出现01(应该只有一个1),00这种情形。那么可以用穷举,针对所有可能的情况来遍历判断即可。
public class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<String>();
int len = s.length();
for(int i = 1; i<4 && i<len-2; i++){
for(int j = i+1; j<i+4 && j<len-1; j++){
for(int k = j+1; k<j+4 && k<len; k++){
String s1 = s.substring(0,i), s2 = s.substring(i,j), s3 = s.substring(j,k), s4 = s.substring(k,len);
if(isValid(s1) && isValid(s2) && isValid(s3) && isValid(s4)){
res.add(s1+"."+s2+"."+s3+"."+s4);
}
}
}
}
return res;
}
public boolean isValid(String s){
if(s.length()>3 || s.length()==0 || (s.charAt(0)=='0' && s.length()>1) || Integer.parseInt(s)>255)
return false;
return true;
}
}
3. Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n.
For example,
Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
思路:想到了用递归来解这题,但是具体下手时却遇到了各种问题,现在感觉与树相关的题目,一大部分都可以用递归来解决。
public class Solution {
public List<TreeNode> generateTrees(int n) {
return genTrees(1,n);
}
// 首先要明确的是这个方法返回的是一个集合,即所有可能的BST的根节点,对于它的子递归,返回的是所有可能的左子树和右子树的根节点,比如对于题目中给出的例子
第一层返回的集合中的元素是1,3,3,2,1,第二层的话,对于3来说,返回的集合中的元素应该是3的左子树的根节点2,1和右子树的根节点 null,null
public List<TreeNode> genTrees (int start, int end)
{
List<TreeNode> list = new ArrayList<TreeNode>();
// 递归的返回条件,当给子排序二叉树的start>end时,够造不了,所以将null加入到集合中
if(start>end)
{
list.add(null);
return list;
}
// 递归的返回条件,一层层递归下来会走到这里。如果相等,则子排序二叉树就是start,也就是叶节点
if(start == end){
list.add(new TreeNode(start));
return list;
}
// 这里的构造思路是这样的,先去找当前根节点的所有可能的左子树根节点集合和右子树根节点集合,再构造这个当前根节点,再将这个二叉树形串起来
List<TreeNode> left,right;
for(int i=start;i<=end;i++)
{
// 从start到end遍历所有可能的当前根节点,并对每一根节点寻找所有可能的左子树根节点集合和右子树根节点集合
left = genTrees(start, i-1); // 对左子树递归,返回所有可能的根节点集合
right = genTrees(i+1,end); // 对右子树递归,返回所有可能的根节点集合
for(TreeNode lnode: left) // 连接处于递归同一层的左子树根节点和右子树根节点
{
for(TreeNode rnode: right)
{
TreeNode root = new TreeNode(i); //从低向上,一层层将这个排序二叉树构造起来
root.left = lnode;
root.right = rnode;
// 对于递归底层返回的是所有可能的左,右子树根节点集合(以构造好,就是左右子树指针都赋好了),对于最上一层返回的就是所有合法的二叉排序树的根节点集合了
list.add(root);
}
}
}
return list;
}
}
LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II的更多相关文章
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 41. Unique Binary Search Trees && Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- Java第一次实验报告——Java开发环境的熟悉
北京电子科技学院(BESTI) 实 验 报 告 课程名称:java程序设计实验 班级:1352 姓名:洪韶武 学号:20135219 成绩: ...
- webpack优化总结
1. 分包 将不需要变动的第三方包分离出去, 主要方法有: (1). externals(2). DllPlugin(3). expose-loader(4). ProviderPlugin 2. 拆 ...
- Codeforces 854C Planning(贪心+堆)
贪心:让代价大的尽量移到靠前的位置. 做法:先让前k个数加进堆里,枚举k+1~n+k,每次把新元素加进堆后找到最大代价放在当前位置即可. #include<bits/stdc++.h> # ...
- bzoj3694: 最短路(树链剖分/并查集)
bzoj1576的帮我们跑好最短路版本23333(双倍经验!嘿嘿嘿 这题可以用树链剖分或并查集写.树链剖分非常显然,并查集的写法比较妙,涨了个姿势,原来并查集的路径压缩还能这么用... 首先对于不在最 ...
- Flex 布局教程:语法篇 【转】
Flex 布局教程:语法篇 作者: 阮一峰 日期: 2015年7月10日 原文:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 网 ...
- [zz]【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie
http://www.crifan.com/python_auto_handle_cookie_and_save_to_from_cookie_file/ #!/usr/bin/python # -* ...
- python2.6升级2.7导致yum无法使用 No module named yum
这里有解决方法:https://teddysun.com/473.html 记住旧版本 Python 2.6.6 的重要路径如下所示,在运行 yum 命令的时候,会提示你哪个 module 不存在,不 ...
- Java IO------------------BIO(同步阻塞)、NIO1.0(多路复用)、NIO2.0(AIO,非阻塞)
1. BIO JDK5之前, JDK的IO模式只有BIO(同步阻塞)问题: 因为阻塞的存在, 需对每个请求开启一个线程. 过多的线程切换影响操作系统性能解决: 使用线程池, 处理不过来的放入队列, 再 ...
- mpvue开发小记
1.组件嵌套组件时,子组件作用域bug 组件A内的slot包含子组件B的话,无法正常使用变量(这种情况下,B组件的template错误地使用了A的作用域). 我的解决方案:减少一层组件提炼,即这种情况 ...
- 【洛谷 P4180】【模板】严格次小生成树[BJWC2010](倍增)
题目链接 题意如题. 这题作为我们KS图论的T4,我直接打了个很暴力的暴力,骗了20分.. 当然,我们KS里的数据范围远不及这题. 这题我debug了整整一个晚上还没debug出来,第二天早上眼前一亮 ...