面试笔试杂项积累-leetcode 141-145

来源:互联网 发布:arduino摄像头编程 编辑:程序博客网 时间:2024/05/29 09:27

141.141-Linked List Cycle-Difficulty: Medium

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

思路

判断是否为循环链表

一个快指针跳两次,一个慢指针走一次,如果是循环链表快指针绝对会赶上慢指针,如果不是循环链表快指针会先达到null


/** * Definition for singly-linked list. * public class ListNode { *     public int val; *     public ListNode next; *     public ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public bool HasCycle(ListNode head) {        ListNode slow = head;        ListNode fast = head;        while (fast != null)        {            fast = fast.next;            if (fast == null)                return false;            if (slow == fast)                return true;            fast = fast.next;            slow = slow.next;        }        return false;    }}

142.142-Linked List Cycle II-Difficulty: Medium

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.

Note: Do not modify the linked list.

Follow up:
Can you solve it without using extra space?

思路

和上题相同,是否为循环链表,并返回循环起点

本题是上一道题目的扩展。可以沿用上一题目的方式。使用一块一慢两个指针从起点开始行走。快指针每次走2步,慢指针每次走1步。如果链表中有环,2者必然会在环中某点相遇。

假设链表恰好是尾巴指向了开头,即假设链表恰好从头道尾都是一个环。那么相遇点必然在两个指针开始走动的起点。

假设链表的组成为先有一段单链表后有一个环,那么相遇点显然不一定是起点了,相遇点显然也和之前那段单链表的长度是相关的。

具体的数学关系可以通过推导得知,参考了这篇博客,如下。

1 设链表长度为len(链表中非空next指针的个数,下面所说的长度均为非空next指针的个数),链表head到环的起点长度为a,环起点到快慢指针相遇点的长度为b,环的长度为r。
2 假设到快慢指针相遇时,慢指针移动的长度为s,则快指针移动长度为2s,而快指针移动的长度还等于s加上在环上绕的k圈(k>=1),所以2s=s+kr ,即s = kr。
3 由s = a + b 和 s = kr 可知 a + b = kr = (k-1)r + r; 而r = len - a,所以a + b = (k-1)r + len - a, 即 a = (k-1)r + len - a - b,len - a - b是相遇点到环的起点的长度,由此可知,从链表头到环起点长度 = (k-1)环长度+从相遇点到环起点长度,于是我们从链表头、与相遇点分别设一个指针,每次各走一步,两个指针必定相遇,且相遇点为环起点。

参考:

http://blog.csdn.net/feliciafay/article/details/18070735


/** * Definition for singly-linked list. * public class ListNode { *     public int val; *     public ListNode next; *     public ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode DetectCycle(ListNode head) {   ListNode slow = head;        ListNode fast = head;                if (head == null || head.next == null)            //注意分析临界情况 (1)成环单节点; (2)不成环单节点; (3)成环2节点; (4)不成环2节点              return null;          while (slow != null&&fast != null&&fast.next != null)           {               fast = fast.next.next;               slow = slow.next;               if (slow == fast)                   break;           }        if (fast == slow)        {            slow = head;            while (slow != fast && fast != null && fast.next != null)            {                slow = slow.next;                fast = fast.next;                if (slow == fast)                    break;            }            return slow;        }        else        return null;    }}

143.143-Reorder List-Difficulty: Medium

Given a singly linked list L: L0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

方法一

思路

像上例一样重造链表,

博主投机了,遍历一遍存到List里,然后当Array操作了= =。。。。

/** * Definition for singly-linked list. * public class ListNode { *     public int val; *     public ListNode next; *     public ListNode(int x) { val = x; } * } */public class Solution {    public void ReorderList(ListNode head) {                IList<ListNode> list = new List<ListNode>();        ListNode temp = head;        while (temp != null)        {            list.Add(temp);            temp = temp.next;        }        temp = head;        for (int i = 0; i < list.Count / 2; i++)        {            list[i].next = list[list.Count - 1 - i];            list[list.Count - 1 - i].next = list[i + 1];        }                if(list.Count>0)                list[list.Count / 2].next = null;    }}

方法二

思路

discuss上发现的用java操作链表实现

This question is a combination of Reverse a linked list I & II. It should be pretty straight forward to do it in 3 steps :)

参考:

https://leetcode.com/discuss/35599/java-solution-with-3-steps

public void reorderList(ListNode head) {            if(head==null||head.next==null) return;            //Find the middle of the list            ListNode p1=head;            ListNode p2=head;            while(p2.next!=null&&p2.next.next!=null){                 p1=p1.next;                p2=p2.next.next;            }            //Reverse the half after middle  1->2->3->4->5->6 to 1->2->3->6->5->4            ListNode preMiddle=p1;            ListNode preCurrent=p1.next;            while(preCurrent.next!=null){                ListNode current=preCurrent.next;                preCurrent.next=current.next;                current.next=preMiddle.next;                preMiddle.next=current;            }            //Start reorder one by one  1->2->3->6->5->4 to 1->6->2->5->3->4            p1=head;            p2=preMiddle.next;            while(p1!=preMiddle){                preMiddle.next=p2.next;                p2.next=p1.next;                p1.next=p2;                p1=p2.next;                p2=preMiddle.next;            }        }

144&145

144- Binary Tree Preorder Traversal-Difficulty: Medium&145- Binary Tree Postorder Traversal-Difficulty: Hard


144要求先序遍历二叉树,145要求后序遍历二叉树,看似很简单的问题难度却是一个Medium一个Hard,原因是OJ要求使用迭代,尽量不使用递归。迭代只是需要使用一个外部数据结构,栈,明白了就很简单了,不比递归难

不知道递归和迭代是什么的看下面的链接就清楚了

http://zhidao.baidu.com/link?url=kzoFhXm2HFUWa0koAurm0nUYahu2jU2MQ0Cl4o4VA80O2pOxtygcbzSeQb5V_u6PCp8pyMB8mGcjSiyMoVoBjF-tVX5yAGEyUOzsrXy9Era

方法一-递归

递归很简单了,大家看代码吧

先序

/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int x) { val = x; } * } */public class Solution {    IList<int> fin = new List<int>();    public IList<int> PreorderTraversal(TreeNode root) {        if(root != null)        recursion(root);        return fin;    }void    recursion(TreeNode root){    fin.Add(root.val);    if(root.left!=null)    recursion(root.left);    if(root.right!=null)    recursion(root.right);}}

后序

/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int x) { val = x; } * } */public class Solution {     IList<int> fin = new List<int>();    public IList<int> PostorderTraversal(TreeNode root) {        if(root != null)        recursion(root);        return fin;    }void    recursion(TreeNode root){    if(root.left!=null)    recursion(root.left);    if(root.right!=null)    recursion(root.right);        fin.Add(root.val);}}

方法二-迭代

方法2-1

来自

https://leetcode.com/discuss/72221/ac-java-iterative-solution-with-explanation

https://leetcode.com/discuss/56142/java-simple-and-clean

我个人比较喜欢这种,易懂,

本质上适合递归意思是相同的,但是借助了栈

先序

public class Solution {//  the key idea is stack is first in last out. so need push right node first.List<Integer> res= new ArrayList<Integer>();public List<Integer> preorderTraversal(TreeNode root) {    Stack<TreeNode> stack = new Stack<TreeNode>();    if(root==null)        return res;    stack.push(root);    TreeNode temp;    while(!stack.empty()){        temp=stack.pop();        res.add(temp.val);        if(temp.right!=null)            stack.push(temp.right);        if(temp.left!=null)            stack.push(temp.left);    }     return res;}

后序

public List<Integer> postorderTraversal(TreeNode root) {<pre name="code" class="csharp"> Stack<TreeNode> stack = new Stack<TreeNode>();    if(root==null)        return res;    stack.push(root);    TreeNode temp;    while(!stack.empty()){        temp=stack.pop();        res.add(temp.val);        if(temp.right!=null)            stack.push(temp.left);        if(temp.left!=null)            stack.push(temp.right);    }     return res;}

方法2-2

来自:

https://leetcode.com/discuss/71943/preorder-inorder-and-postorder-iteratively-summarization

先序

public List<Integer> preorderTraversal(TreeNode root) {    List<Integer> result = new ArrayList<>();    Deque<TreeNode> stack = new ArrayDeque<>();    TreeNode p = root;    while(!stack.isEmpty() || p != null) {        if(p != null) {            stack.push(p);            result.add(p.val);  // Add before going to children            p = p.left;        } else {            TreeNode node = stack.pop();            p = node.right;           }    }    return result;}


中序

public List<Integer> inorderTraversal(TreeNode root) {    List<Integer> result = new ArrayList<>();    Deque<TreeNode> stack = new ArrayDeque<>();    TreeNode p = root;    while(!stack.isEmpty() || p != null) {        if(p != null) {            stack.push(p);            p = p.left;        } else {            TreeNode node = stack.pop();            result.add(node.val);  // Add after all left children            p = node.right;           }    }    return result;}


后序

public List<Integer> postorderTraversal(TreeNode root) {    LinkedList<Integer> result = new LinkedList<>();    Deque<TreeNode> stack = new ArrayDeque<>();    TreeNode p = root;    while(!stack.isEmpty() || p != null) {        if(p != null) {            stack.push(p);            result.addFirst(p.val);  // Reverse the process of preorder            p = p.right;             // Reverse the process of preorder        } else {            TreeNode node = stack.pop();            p = node.left;           // Reverse the process of preorder        }    }    return result;}


















1 0
原创粉丝点击