Timo66

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Reverse num/word/bits

最近回国了 有点懒

Question 1

Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Be careful about some interesting cases. i.e.negative number, overflow case.

 1     public int reverse(int x) {
 2         if (x >= -9 && x <= 9){
 3             return x;
 4         }
 5         
 6         boolean isNeg = false;
 7         if (x < 0){
 8             isNeg = true;
 9             x = 0 - x;
10         }
11         
12         double result = 0;
13         
14         while(x > 0){
15             result = result * 10 + x % 10;
16             x = x / 10;
17         }
18         
19         if (isNeg){
20             return -result < Integer.MIN_VALUE ? 0 : -(int)result;
21         }
22         else{
23             return result > Integer.MAX_VALUE ? 0 : (int)result;
24         }
25

Question 2

Reverse Words in a String

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

I use split to change it an word array, or you can use toCharArray() to change it to char array. 

 1     public String reverseWords(String s) {
 2         StringBuffer result = "";
 3         String[] array = s.split(" ");
 4         for (int i = array.length - 1; i >= 0; i --){
 5             String temp = array[i].trim();
 6             if (temp.equals("") == false){
 7                 if (i != array.length - 1){
 8                     result += " " + temp;
 9                 }
10                 else{
11                     result += temp;
12                 }
13             }
14         }
15         return result.trim();
16     }

 

Question 3

Reverse Words in a String II
这里要求in-place,也就是说不需要开辟额外空间

Reverse all and then deal with space.

 1 public class Solution {
 2     public void reverseWords(char[] s) {
 3         reverse(s, 0, s.length);//reverse the whole char array
 4         //i is the start reverse point, j is the end reverse pont, i's change is
 5         //controlled by j, when j is the space, set i to the character taht next to space, i.e. i+1
 6         for (int i=0, j=0; j<=s.length; j++) {
 7             if (j==s.length || s[j]==' ') {
 8                 reverse(s, i, j);
 9                 i =  j + 1; //j is the space, specify i to j+1, i.e. the character next to space
10             }
11         }
12     }
13     //begin: indicate the start reversing location
14     //end-1:last character, end-1-i: i is the distance to end
15     private void reverse(char [] s, int begin, int end) {
16         //[!]i<(end-begin)/2
17         for (int i=0; i<(end-begin)/2; i++) {
18             char temp = s[begin+i];
19             s[begin+i] = s[end-i-1];
20             s[end-i-1] = temp;
21         }
22     }
23 }

Question 4

Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).

n & 1 means always get the first digit of n. And put it to reverse's first digit (reversed << 1 menas move all digits to left by 1 bit). 

1     public int reverseBits(int n) {
2         int reversed = 0;
3         for (int i = 0; i < 32; i ++){
4            reversed = (reversed << 1) | (n & 1);
5             n = n >> 1; 
6         }
7         return reversed;
8     }

 

 

 

 

posted on 2015-08-17 10:21  Timo66  阅读(163)  评论(0编辑  收藏  举报