Lintcode algorithm--parity split array, 1 numbers in binary, inverse integer, plus one, sorted array converted to the smallest two-fork search tree, binary sum

Source: Internet
Author: User
Tags array length lintcode
odd-even segmented arrays

Splits an array of integers so that the odd number is behind the first even number. Sample Example

Given [1, 2, 3, 4], return [1, 3, 2, 4]. algorithm

This problem is still relatively simple, the method is many, we will loop through the array, the traversal of the number of even and odd exchanges between the OK.

public class Solution {
 /* * @param nums:an array of integers
 * @return: Nothing */public
    void parti Tionarray (int[] nums) {
        //write your code here
        int length = nums.length;
        int temp = 0;
        int index = 0;
        if (length==0)
            return;
        for (int i=0;i<length;i++) {
            if (nums[i]%2==0) {
                index = i;
                while (nums[index]%2==0) {
                    if (index!=length-1)
                        index=index+1;
                    else break
                        ;
                }
                temp = nums[i];
                Nums[i] = Nums[index];
                Nums[index] = temp;}}}}

1 numbers in a binary

Calculates how many 1 are in the binary representation of a 32-bit integer. Sample Example

Given 32 (100000), return 1

Given 5 (101), return 2

Given 1023 (111111111), return 9 algorithm

Press and calculate
1&1==1,1&0==0,0&&0==0; The following is a very ingenious method.

public class Solution {
 /* * @param num:an integer
 * @return: An integer *
 /public
    int Countones (int num) {
        //write your code here
        int count = 0;
        while (num!=0) {
            num = num & (num-1);
            count++;
        }
        return count;       
    }
}
Inverse integer

Reverses the number in an integer and returns 0 (marked as a 32-bit integer) when the inverted integer overflows. Sample Example

Given x = 123, return 321

given x =-123, returns the -321 algorithm

Reversal is very simple, the difficulty is reversed after the certificate overflow judgment, Int32 bit range is -2^31~2^31-1, here use TEMP/10 and reversed_n to judge, when the temp overflow,/10 not equal to reversed_n, so can judge overflow set up.

public class Solution {
     /* * @param n:the integer to be reversed
     * @return: The reversed integer */
    P ublic int Reverseinteger (int n) {
        //Write your code here
        int reversed_n = 0;

        while (n! = 0) {
            int temp = reversed_n * + n%;
            n = N/10;
            if (TEMP/10! = reversed_n) {
                reversed_n = 0;
                break;
            }
            Reversed_n = temp;
        }
        return reversed_n;
    }
}
Add a

Given a non-negative number, represents a numeric array, on the basis of that number +1, returns a new array.

The number is arranged by size, and the largest number is at the front of the list. Sample Example

Given [1,2,4] means 123, return [to].

Given [9,9,9] represents 999, returns [1,0,0,0]. algorithm

Google's question, which is not a trap, the first time the pit.
Be careful not to convert the array to an int integer, because an array size is not determined to cause an overflow.
The idea is that if the array value is 9, the array length is incremented, the first is set to 1, the other bit is 0; otherwise, the end is added 1, and the return is ended.

public class Solution {
     /* * @param digits:a number represented as an array of digits
     * @return: The result
  */public
    Static int[] PlusOne (int[] digits) {
        //write your code here
        Boolean index = true;
        for (int n=0; n<digits.length;n++) {
            if (digits[n]!=9)
                index = false;
        }
        if (index==true) {
            int[] num = new int[digits.length+1];
            Num[0] = 1;
            for (int m=1;m<digits.length+1;m++)
                num[m] = 0;
            return num;
        }
        for (int i=digits.length-1;i>=0;i--) {
            if (digits[i]!=9) {
                digits[i] = digits[i]+1;
                break;
            } else{
                Digits[i] = 0;
            }
        }
        return digits;
    }
}
converts a sorted array into a two-prong search tree with the smallest height

Give a sorted array (from small to large) and convert it to a binary tree with the smallest height. Sample Example

Give the array [1,2,3,4,5,6,7], return

         4
       /   \
      2     6
     /\    /\
    1   3  5   7
algorithm
/**
 * Definition of TreeNode:
 * public class TreeNode {
 * public     int val;
 * Public     TreeNode left and right;
 * Public     TreeNode (int val) {
 *         this.val = val;
 *         this.left = This.right = null;
 *     }
 *} * * Public 
Class solution () {
    /**
     * @param nums:an integer array
     * @return: a T REE Node
     *
    /Public TreeNode Sortarraytobst (int[] A) {
        if (a==null)
            return null;
        Return Bulidtree (A, 0, a.length-1); 
    }

    public void Buildtree (int[] A, int start, int end) {
        if (start>end)
            return null;
        int middle = (start+end)/2;
        TreeNode root = new TreeNode (A[middle]);
        A.left = Buildtree (A, start, middle-1);
        A.right = Buildtree (A, middle+1, end);  
    }
}
Binary summation

Given two binary strings, return their and (in binary notation). Sample Example

A = 11

b = 1

Returns the algorithm

Do not convert the binary to an integer, so the complexity of the conversion will increase the geometric multiples.
Note If the corresponding bit addition result is 2 (+), then the result is set to 1, when a Sentinel is set, the corresponding bit overflow, Sentinel set to 1; The result is 3 (1+1+ Sentinel), the same reason.

public class Solution {/* * @param a:a number * @param b:a number * @return: the result */ public string Addbinary (string A, string b) {//write your code here if (B.length () ==0&&a.length ()
        ==0) return null;
        String str = "";
        int index = 0;
        int i = 0;
        int middle = 0;
        string[] A = A.split ("");
        string[] B = B.split (""); while (i<a.length| | I<b.length) {if (I >= a.length && i < b.length) middle = integer.valueof (b[b
            . Length-1-i]) + index;
            else if (i >= b.length && i < a.length) middle = integer.valueof (A[a.length-1-i]) + index;  else if (i < a.length && i < b.length) middle = integer.valueof (A[a.length-1-i])
            + integer.valueof (B[b.length-1-i]) + index;
                if (middle = = 3) {str = 1 + str;
    index = 1;            i=i+1;
                } else if (middle = = 2) {str = 0 + str;
            index = 1;
                } else if (middle = = 0 | | middle = = 1) {str = middle + str;
            index = 0;
        } i = i + 1;
        } if (index==1) str = INDEX+STR;
    return str;   
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.