隨筆 - 8, 文章 - 0, 評論 - 6, 引用 - 0
          數(shù)據(jù)加載中……

          2007年7月17日

          實現(xiàn)一個棧,使其push,pop,min(取得棧中的最小元素)均為O(1)

          實現(xiàn)一個棧,使其push,pop,min(取得棧中的最小元素)均為O(1)

          我的解
          interface IntStack
          {
              
          int pop();
              
          void push(int i);
              
          int get();
          }


          class MinStack
          {
              
          //store all the element
              private IntStack elemStack = new IntStack();
              
              
          //store current and historical smallest element
              private IntStack minStack = new IntStack();
              
              
          public void push(int i)
              
          {
                  elemStack.push(i);
                  
                  
          int currentMin = minStack.get();
                  
          if(i <= currentMin) minStack.push(i);
              }

              
              
          public int pop()
              
          {
                  
          int result = elemStack.pop();
                  
          if(result == minStack.get()) minStack.pop();
                  
          return result;
              }

              
              
          public int getMinElem()
              
          {
                  
          return minStack.get();
              }

          }

          posted @ 2007-07-18 20:57 Job Hu 閱讀(902) | 評論 (0)編輯 收藏

          二叉排序樹變?yōu)殡p向鏈表

          把一個二叉排序樹(也許不叫這個)變?yōu)檫f增的雙向鏈表,不能夠生成額外的結(jié)點.
          eg 6
                 / \
                4   8
               / \ / \
              3  5 7  9

          3=4=5=6=7=8=9

          我的解:

          class Node
          {
              
          public Node left;
              
          public Node right;
              
              
          private static Node getLinkListTail(Node head)
              
          {
                  Node result 
          = head;
                  
          if(result==nullreturn null;
                  
          while(result.right!=null)
                  
          {
                      result 
          = result.right;
                  }

                  
          return result;
              }

              
              
          public static Node flatten(Node root)
              
          {
                  
          if(root==nullreturn null;
                  
                  Node result 
          = root;
                  
                  
          // A leaf node
                  if(root.left==null&&root.right==nullreturn root;
                  
                  
          //divide-and-conquer
                  Node leftSubTreeLinkListHead = flatten(root.left);
                  Node rightSubTreeLinkListHead 
          = flatten(root.right);
                  
                  
          //merge
                  Node leftSubTreeLinkListTail = getLinkListTail(leftSubTreeLinkListHead);
                  root.left 
          = leftSubTreeLinkListTail;
                  root.right 
          = rightSubTreeLinkListHead;
                  
          if(leftSubTreeLinkListHead!=null
                  
          {
                      result 
          = leftSubTreeLinkListHead;
                      leftSubTreeLinkListTail.right 
          = root;
                  }

                  
          if(rightSubTreeLinkListHead!=null) rightSubTreeLinkListHead.left = root;
                  
                  
          return result;
              }

          }


          posted @ 2007-07-18 20:37 Job Hu 閱讀(648) | 評論 (0)編輯 收藏

          Byte in Java


          public class ByteTest
          {
              
          public static void main(String[] args)
              
          {
                  
          byte b;
                  
          byte c;
                  
          //b = 255; //Cannot convert from int to byte
                  
          //b = 0xFF; //Cannot convert from int to byte
                  b = 127;
                  c 
          = 0x7F;
                  
          if(b == c) System.out.println("b == c");
                  
          if(127 == 0x7F) System.out.println("127 == 0x7F");
                  
                  b 
          = -128;
                  
          //c = 0x80; //Cannot convert from int to byte
                  c = (byte)0x80;
                  
          if(b == c) System.out.println("b == c");
                  
          if(-128 == 0x80) System.out.println("-128 == 0x80");
                  
          if(128 == 0x80) System.out.println("128 == 0x80"); 
                  
                  c 
          = (byte)0x80;
                  
          if(128 == c) System.out.println("128 == c");
                  
          if(-128 == c) System.out.println("-128 == c");
                  
          if(128 == (c&0xFF)) System.out.println("128 == (c&0xFF)");
              }

          }

          輸出:
          b == c
          127 == 0x7F
          b == c
          128 == 0x80
          -128 == c
          128 == (c&0xFF)

          posted @ 2007-07-17 23:22 Job Hu 閱讀(294) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 元江| 宜春市| 五家渠市| 朝阳市| 霍州市| 囊谦县| 盐源县| 台东县| 宁化县| 广昌县| 工布江达县| 昌邑市| 揭西县| 柯坪县| 柳河县| 平陆县| 荆门市| 南通市| 衢州市| 漯河市| 宁陵县| 洞头县| 沛县| 江达县| 湛江市| 漳州市| 安吉县| 都江堰市| 石景山区| 雷州市| 新平| 绩溪县| 双桥区| 大宁县| 商河县| 广南县| 松溪县| 微山县| 博罗县| 安塞县| 东光县|