11 2013 档案
摘要:NAME pipe, pipe2 - create pipeSYNOPSIS #include int pipe(int pipefd[2]); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include /* Obtain O_* constant definitions */ #include int pipe2(int pipefd[2], int flags);DESCRIPTION ...
阅读全文
摘要:NAME fork - create a child processSYNOPSIS #include pid_t fork(void);RETURN VALUE On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errn...
阅读全文
摘要:Sort a linked list using insertion sort. 1 public ListNode insertionSortList(ListNode head) { 2 // IMPORTANT: Please reset any member data you declared, as 3 // the same Solution instance will be reused for each test case. 4 if(head == null){ 5 return head; 6 ...
阅读全文
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not
阅读全文
摘要:Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is trivial, could you do it iteratively?[解题思路]后序遍历的非递归相对来说比较难,根节点需要在其左右孩子都访问结束后才能被访问,因此对于任一节点,先将其入栈,如果p不存在左孩子和右孩子,则可以直接访问它;或者p存在...
阅读全文
摘要:Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-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}.[解题思路]将队列从中间断开,分成两个队列p1,p2,p2反转,然后将p1,p2进行合并 1 public class Solution { 2 public void reorderList(ListNo
阅读全文
摘要:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?[解题思路]递归解法 1 public ArrayList preorderTraversal(TreeNode root) { 2 // IMPORTANT: ...
阅读全文
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?[解题思路]双指针问题 详见:链表 相交结点与环问题 1 public class Solution { 2 public ListNode detectCycle(ListNode head) { 3 // IMPORTANT: Please reset any member d...
阅读全文
摘要:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?[解题思路]双指针问题,fast指针每次走两步,slow指针每次走一步,如果fast指针可以"追上"slow指针,则说明存在环 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 // IMPORTANT: Please reset any member data yo..
阅读全文
摘要:1 public class VolitileTest { 2 volatile static int count=0; 3 public static void main(String args[]){ 4 5 for(int i=0;i<1000;i++){ 6 new Thread(new Runnable() { 7 public void run() { 8 count++; 9 }10 }11 ...
阅读全文