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

          2007年7月9日

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

          實(shí)現(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é)點(diǎn).
          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 閱讀(293) | 評論 (0)編輯 收藏

          Java Language Keywords

          Here's a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.
          abstract continue for new switch
          assert*** default goto* package synchronized
          boolean do if private this
          break double implements protected throw
          byte else import public throws
          case enum**** instanceof return transient
          catch extends int short try
          char final interface static void
          class finally long strictfp** volatile
          const* float native super while

          posted @ 2007-07-09 22:38 Job Hu 閱讀(201) | 評論 (1)編輯 收藏

          主站蜘蛛池模板: 哈尔滨市| 武隆县| 郯城县| 金沙县| 晋中市| 玉田县| 镇巴县| 洪湖市| 平江县| 禄丰县| 怀仁县| 牡丹江市| 宣威市| 屏东县| 桐庐县| 宁明县| 金阳县| 河源市| 达州市| 潍坊市| 常州市| 元江| 德格县| 昆山市| 盐津县| 崇仁县| 景洪市| 济源市| 金秀| 西充县| 万荣县| 绥宁县| 岚皋县| 湖州市| 鄂州市| 綦江县| 巧家县| 安西县| 司法| 正镶白旗| 晋州市|