Skip to content

Instantly share code, notes, and snippets.

@1a57danc3
Last active August 8, 2019 05:46
Show Gist options
  • Save 1a57danc3/62629312bbc1500fc41e476e90ff9f37 to your computer and use it in GitHub Desktop.
Save 1a57danc3/62629312bbc1500fc41e476e90ff9f37 to your computer and use it in GitHub Desktop.
Java Interview Answers

#1

public List<String> fizzBuzz(int n) {
        List<String> list = new ArrayList<>();
        for(int i=1; i<=n; i++) {
            if(i % 15 == 0) list.add("FizzBuzz");
            else if(i % 5 == 0) list.add("Buzz");
            else if(i % 3 == 0) list.add("Fizz");
            else list.add(String.valueOf(i));
        }
        return list;
    }

或者

import java.util.ArrayList;
import java.util.List;

public class t412 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
    }
 
    public static List<String> fizzBuzz(int n) {
        List<String> re = new ArrayList<String>();
        for (int i = 0; i < n; i++) {
            re.add(change(i + 1));
        }
        return re;
    }
 
    public static String change(int n) {
        if (n % 15 == 0) {
            return "FizzBuzz";
        }
        if (n % 5 == 0) {
            return "Buzz";
        }
        if (n % 3 == 0) {
            return "Fizz";
        }
        return n + "";
    }
 
}

#2

class Solution {
    /**
     * @param n: an integer
     * @return an integer f(n)
     */
    public int fibonacci(int n) {
        int a = 0;
        int b = 1;
        for (int i = 0; i < n - 1; i++) {
            int c = a + b;
            a = b;
            b = c;
        }
        return a;
    }
}

//递归版本,会超时
public class Solution {
    /*
     * @param : an integer
     * @return: an ineger f(n)
     */
    public int fibonacci(int n) {
        // write your code here
        if (n == 1) {
            return 0;
        }
        
        if (n == 2) {
            return 1;
        }
        
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
};

#3

public class Solution {
    public Boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }

        ListNode fast, slow;
        fast = head.next;
        slow = head;
        while (fast != slow) {
            if(fast==null || fast.next==null)
                return false;
            fast = fast.next.next;
            slow = slow.next;
        } 
        return true;
    }
}

#4

class Solution {
    /*
     * @param n: An integer
     * @return: True or false
     */
    public boolean checkPowerOf2(int n) {
        if (n <= 0) {
            return false;
        }
        return (n & (n-1)) == 0;
    }
};

#5

public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: The new head of reversed linked list.
     */
    public ListNode reverse(ListNode head) {
        //prev表示前继节点
        ListNode prev = null;
        while (head != null) {
            //temp记录下一个节点,head是当前节点
            ListNode temp = head.next;
            head.next = prev;
            prev = head;
            head = temp;
        }
        return prev;
    }
}

#6

public class JavaTest {

    public  static String array = "abuacdeaudbdfcefhph";

    public static int[] container = new int[128];

    public static void main(String[] args) {

        for (int i = 0; i <array.length() ; i++) {
            int index = Character.valueOf(array.charAt(i)).hashCode();
            container[index] ++;
        }

        for (int i = 0; i <array.length() ; i++) {
            int index = Character.valueOf(array.charAt(i)).hashCode();
            if (container[index] == 1) {
                System.out.println("index: " + i);
                System.out.println("char: " + array.charAt(i));
                break;
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment