Saturday, October 8, 2011

Trie data structure, reverse an integer in place

1) http://www.technicalypto.com/2010/04/trie-in-java.html

2) Reverse an integer (java)

The time taken is proportional to the number of digits in the integer.


 public int revNum(int num){  
    if(num == 0) return 0;  
    boolean isNeg = false;  
    if(num < 0){  
      isNeg = true;  
      num = num * -1; //make it + ve  
    }  
    int newNum = 0;  
    while(num != 0){  
       newNum = newNum * 10 + num % 10;  
       num = num / 10;  
     }  
     if(isNeg){  
       newNum = newNum * -1;  
     }  
     return newNum;  
   }  

No comments:

Post a Comment