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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

思路:链表移动的题目,要求遍历一遍完成。可以用这个移动模式来解决,对于要移动的区间m~n,往右移动第一个元素,后面的元素添加到最前面,比如2-3-4,左移一次是3-2-4,2再往右移是4-3-2,这样就完成了链表的逆置。

  1. public ListNode reverseBetween(ListNode head, int m, int n) {
  2. if(head == null) return null;
  3. ListNode dummy = new ListNode(0); // create a dummy node to mark the head of this list
  4. dummy.next = head;
  5. ListNode pre = dummy; // make a pointer pre as a marker for the node before reversing
  6. for(int i = 0; i<m-1; i++) pre = pre.next;
  7.  
  8. ListNode start = pre.next; // a pointer to the beginning of a sub-list that will be reversed
  9. ListNode then = start.next; // a pointer to a node that will be reversed
  10.  
  11. // 1 - 2 -3 - 4 - 5 ; m=2; n =4 ---> pre = 1, start = 2, then = 3
  12. // dummy-> 1 -> 2 -> 3 -> 4 -> 5

  13.   // 难点在于具体怎么实现那个移动模式
  14. for(int i=0; i<n-m; i++)
  15. {
  16. start.next = then.next;
  17. then.next = pre.next;
  18. pre.next = then;
  19. then = start.next;
  20. }
  21.  
  22. // first reversing : dummy->1 - 3 - 2 - 4 - 5; pre = 1, start = 2, then = 4
  23. // second reversing: dummy->1 - 4 - 3 - 2 - 5; pre = 1, start = 2, then = 5 (finish)
  24.  
  25. return dummy.next;
  26.  
  27. }

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这种情形。那么可以用穷举,针对所有可能的情况来遍历判断即可。

  1. public class Solution {
  2. public List<String> restoreIpAddresses(String s) {
  3. List<String> res = new ArrayList<String>();
  4. int len = s.length();
  5. for(int i = 1; i<4 && i<len-2; i++){
  6. for(int j = i+1; j<i+4 && j<len-1; j++){
  7. for(int k = j+1; k<j+4 && k<len; k++){
  8. String s1 = s.substring(0,i), s2 = s.substring(i,j), s3 = s.substring(j,k), s4 = s.substring(k,len);
  9. if(isValid(s1) && isValid(s2) && isValid(s3) && isValid(s4)){
  10. res.add(s1+"."+s2+"."+s3+"."+s4);
  11. }
  12. }
  13. }
  14. }
  15. return res;
  16. }
  17. public boolean isValid(String s){
  18. if(s.length()>3 || s.length()==0 || (s.charAt(0)=='0' && s.length()>1) || Integer.parseInt(s)>255)
  19. return false;
  20. return true;
  21. }
  22. }

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. 1 3 3 2 1
  2. \ / / / \ \
  3. 3 2 1 1 3 2
  4. / / \ \
  5. 2 1 2 3

思路:想到了用递归来解这题,但是具体下手时却遇到了各种问题,现在感觉与树相关的题目,一大部分都可以用递归来解决。

  1. public class Solution {
  2. public List<TreeNode> generateTrees(int n) {
  3. return genTrees(1,n);
  4. }

  5. // 首先要明确的是这个方法返回的是一个集合,即所有可能的BST的根节点,对于它的子递归,返回的是所有可能的左子树和右子树的根节点,比如对于题目中给出的例子
    第一层返回的集合中的元素是13321,第二层的话,对于3来说,返回的集合中的元素应该是3的左子树的根节点21和右子树的根节点 nullnull
  6. public List<TreeNode> genTrees (int start, int end)
  7. {
  8.  
  9. List<TreeNode> list = new ArrayList<TreeNode>();

  10.    // 递归的返回条件,当给子排序二叉树的start>end时,够造不了,所以将null加入到集合中
  11. if(start>end)
  12. {
  13. list.add(null);
  14. return list;
  15. }
  16. // 递归的返回条件,一层层递归下来会走到这里。如果相等,则子排序二叉树就是start,也就是叶节点
  17. if(start == end){
  18. list.add(new TreeNode(start));
  19. return list;
  20. }
  21. // 这里的构造思路是这样的,先去找当前根节点的所有可能的左子树根节点集合和右子树根节点集合,再构造这个当前根节点,再将这个二叉树形串起来
  22. List<TreeNode> left,right;
  23. for(int i=start;i<=end;i++)
  24. {
  25. // 从start到end遍历所有可能的当前根节点,并对每一根节点寻找所有可能的左子树根节点集合和右子树根节点集合
  26. left = genTrees(start, i-1); // 对左子树递归,返回所有可能的根节点集合
  27. right = genTrees(i+1,end); // 对右子树递归,返回所有可能的根节点集合
  28.  
  29. for(TreeNode lnode: left)  // 连接处于递归同一层的左子树根节点和右子树根节点
  30. {
  31. for(TreeNode rnode: right)
  32. {
  33. TreeNode root = new TreeNode(i); //从低向上,一层层将这个排序二叉树构造起来
  34. root.left = lnode;
  35. root.right = rnode;
              // 对于递归底层返回的是所有可能的左,右子树根节点集合(以构造好,就是左右子树指针都赋好了),对于最上一层返回的就是所有合法的二叉排序树的根节点集合了
  36. list.add(root);
  37. }
  38. }
  39.  
  40. }
  41.  
  42. return list;
  43. }
  44. }

LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II的更多相关文章

  1. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  2. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  3. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  4. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  5. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  6. 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]* ...

  7. 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 ...

  8. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

  9. 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 ...

随机推荐

  1. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  2. 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化

    神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...

  3. ios轮播

    // // ViewController.m // Ocproject // // Created by wenzhe yi on 2018/2/28. // Copyright © 2018年 we ...

  4. Java日期时间实用工具类

    Java日期时间实用工具类 1.Date (java.util.Date)    Date();        以当前时间构造一个Date对象    Date(long);        构造函数   ...

  5. 【题解】我也不是B ifrog 1112 二分 倍增

    题目传送门:http://ifrog.cc/acm/problem/1112 神奇的倍增二分,长见识了,在此做个记录,分享给大家. 懒得写题解了,直接转YJQ的:http://ifrog.cc/acm ...

  6. kafka 命令笔记

    以下命令都是在kafka根目录下 启动自带的zookeeper bin/zookeeper-server-start.sh config/zookeeper.properties 启动kafka(启动 ...

  7. javascript实现购物车思路

    /* 思路: 第一步:获取所要操作的节点对象 第二步:当页面加载完后,需要计算本地cookie存了多少[个]商品,把个数赋值给count 第三步:为每一个商品对应的添加购物车按钮绑定一个点击事件onc ...

  8. Mabatis(2) 全局配置文件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...

  9. java项目环境搭建

    开发java项目时,由于涉及到版权问题,最好使用开源.免费的软件.比如eclipse. 此外,一个web的java项目涉及到jdk.tomcat等,插件还可能用到svn插件.maven插件. 建议进入 ...

  10. python基础--结构篇

    在C/C++/Java中,main是程序执行的起点,Python中,也有类似的运行机制,但方式却截然不同: Python使用缩进对齐组织代码的执行,所有没有缩进的代码(非函数定义和类定义),都会在载入 ...