Algorithmic Fibonacci Number-Reverse String

Keywords: Programming ascii Java

00509 Fibonacci Number

Title Description

The number of Fibonacci, usually expressed in F(n), forms a sequence called the Fibonacci series.The column starts with 0 and 1, and each subsequent number is the sum of the first two numbers.That is:

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), among N > 1.

Given N, calculate F(N).

Example 1:

Input: 2
 Output: 1

Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: 3
 Output: 2

Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: 4
 Output: 3

Interpretation: F(4) = F(3) + F(2) = 2 + 1 = 3.  

Tips:

  • 0 ≤ N ≤ 30

Force Button Address

shopping spree

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

Recursive implementation based

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public int fib(int N) {
    if(N == 0){
        return 0;
    }
    if(N == 1){
        return 1;
    }
    return fib(N-2) + fib(N-1);
}

Array-based implementation

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public int fib(int N) {
    if(N == 0){
        return 0;
    }
    if(N == 1){
        return 1;
    }
    int [] arr = new int[N+1];
    arr[0] = 0;
    arr[1] = 1;
    for(int i = 2; i <= N; i++){
        arr[i] = arr[i-2] + arr[i-1];
    }
    return arr[N];
}

Exchange-based implementation

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public int fib(int N) {
    if(N == 0){
        return 0;
    }
    if(N == 1){
        return 1;
    }
    int a = 0;
    int b = 1;
    for(int i = 2; i <= N; i++){
        int sum = a + b;
        a = b;
        b = sum;
    }
    return b;
}

00344 Reverse String

Title Description

Write a function that reverses the input string.The input string is given as the character array char[].

Instead of allocating extra space to other arrays, you must modify the input arrays in place and use the extra space of O(1) to solve this problem.

You can assume that all the characters in the array are printable characters in the ASCII code table.

Example 1:

Input: ['h','e','l','l','o']
Output: ['o','l','l','e','h']

Example 2:

Input: ['H','a','n','n','a','h']
Output: ['h','a','n','n','a','H']

Force Button Address

shopping spree

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

By XOR

High and low bits of a string by XOR exchange

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public static String reverse(String source){
   char[] chars = source.toCharArray();
   int low = 0;
   int top = chars.length - 1;
   while (low < top){
       chars[low] ^= chars[top];
       chars[top] ^= chars[low];
       chars[low] ^= chars[top];
       low++;
       top--;
   }
   return new String(chars);
}

Using charAt s to implement

Reverse appending target string from first to last character

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public static String reverse(String source) {
   int length = source.length();
   String reverse = "";
   for (int i = 0; i < length; i++) {
       reverse = source.charAt(i) + reverse;
   }
   return reverse;
}

<!-- more -->

Using StringBuffer or StringBuilder implementation

Flip using built-in algorithms in java collections

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public static String reverse(String source) {
   return new StringBuffer(source).reverse().toString();
}

perhaps

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public static String reverse(String source) {
   return new StringBuilder(source).reverse().toString();
}

Implemented by stack

Flip using stack FIFO feature

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public static String reverse(String source) {
   char[] chars = source.toCharArray();
   Stack<Character> stack = new Stack<Character>();
   for (char ch : chars) {
       stack.push(ch);
   }
   String target = "";
   while (!stack.isEmpty()) {
       target += stack.pop();
   }
   return target;
}

Using recursive implementation

Exchange left and right using recursion

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public static String reverse(String source) {
   int length = source.length();
   if (length <= 1) {
       return source;
   }
   String left = source.substring(0, length / 2);
   String right = source.substring(length / 2, length);
   return reverse(right) + reverse(left);
}

Use dichotomy

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public static String reverse(String source) {
   char[] s = source.toCharArray();
   int n = s.length - 1;
   int half = n / 2;
   for (int i = 0; i <= half; i++) {
       char temp = s[i];
       s[i] = s[n - i];
       s[n - i] = temp;
   }
   return new String(s);
}

Use a for loop

/**
 *  WeChat Public Number "Ape Brush Title"
 */
public static String reverse(String source) {
   char[] array = source.toCharArray();
   String reverse = "";
   for (int i = array.length - 1; i >= 0; i--)
       reverse += array[i];

   return reverse;
}

Posted by Arnold_26 on Thu, 09 Jan 2020 16:00:56 -0800