21xrx.com
2024-12-23 00:07:17 Monday
登录
文章检索 我的文章 写文章
Java面试突击:分享十份常见面试题及代码案例,附资源下载链接
2023-06-17 00:19:26 深夜i     --     --
Java面试 代码案例 资源下载

如果你正在备战Java相关岗位面试,不妨来看看这篇分享。以下是十份常见Java面试题及其代码案例,涵盖了面试中的基础知识、算法和细节方面。希望对大家有所帮助。

1. 请实现一个单例模式。


public class Singleton {

  private Singleton() {}

  private static Singleton instance = null;

  public static synchronized Singleton getInstance() {

    if (instance == null) {

      instance = new Singleton();

    }

    return instance;

  }

}

2. 请实现一个链表的翻转。


public class ReverseList {

  static class Node {

    int data;

    Node next;

    public Node(int data)

      this.data = data;

      this.next = null;

    

  }

  public static Node reverse(Node head) {

    Node pre = null;

    Node curr = head;

    while (curr != null)

      Node next = curr.next;

      curr.next = pre;

      pre = curr;

      curr = next;

    

    return pre;

  }

}

3. 请实现一个二叉树的前序遍历。


public class PreorderTraversal {

  static class TreeNode {

    int val;

    TreeNode left;

    TreeNode right;

    public TreeNode(int val)

      this.val = val;

      this.left = null;

      this.right = null;

    

  }

  public static void preorderTraversal(TreeNode root) {

    if (root != null) {

      System.out.print(root.val + " ");

      preorderTraversal(root.left);

      preorderTraversal(root.right);

    }

  }

}

4. 请实现一个算法,判断一个字符串是否是回文串。


public class IsPalindrome {

  public static boolean isPalindrome(String s) {

    if (s == null)

      return false;

    

    int left = 0;

    int right = s.length() - 1;

    while (left < right) {

      char c1 = s.charAt(left);

      char c2 = s.charAt(right);

      if (!Character.isLetterOrDigit(c1)) {

        left++;

      } else if (!Character.isLetterOrDigit(c2))

        right--;

       else {

        if (Character.toLowerCase(c1) != Character.toLowerCase(c2))

          return false;

        

        left++;

        right--;

      }

    }

    return true;

  }

}

5. 请实现一个冒泡排序算法。


public class BubbleSort {

  public static void bubbleSort(int[] arr) {

    if (arr == null || arr.length <= 1)

      return;

    

    for (int i = 0; i < arr.length - 1; i++) {

      for (int j = 0; j < arr.length - 1 - i; j++) {

        if (arr[j] > arr[j + 1]) {

          int temp = arr[j];

          arr[j] = arr[j + 1];

          arr[j + 1] = temp;

        }

      }

    }

  }

}

6. 请实现一个HashMap。


public class MyHashMap {

  private static final int DEFAULT_CAPACTIY = 16;

  private static final float DEFAULT_LOAD_FACTOR = 0.75f;

  private Node [] array;

  private int size;

  @SuppressWarnings("unchecked")

  public MyHashMap() {

    array = (Node []) new Node[DEFAULT_CAPACTIY];

    size = 0;

  }

  public void put(K key, V value) {

    int index = hash(key);

    Node head = array[index];

    Node node = head;

    while (node != null) {

      if (equalsKey(node.key, key)) {

        node.value = value;

        return;

      }

      node = node.next;

    }

    Node newNode = new Node<>(key, value);

    newNode.next = head;

    array[index] = newNode;

    size++;

    if (needRehashing()) {

      rehashing();

    }

  }

  public V get(K key) {

    int index = hash(key);

    Node node = array[index];

    while (node != null) {

      if (equalsKey(node.key, key)) {

        return node.value;

      }

      node = node.next;

    }

    return null;

  }

  private boolean equalsKey(K k1, K k2) {

    return k1 == null ? k2 == null : k1.equals(k2);

  }

  private int hash(K key) {

    if (key == null) {

      return 0;

    }

    int code = key.hashCode();

    return code & (array.length - 1);

  }

  private boolean needRehashing() {

    float loadFactor = (float) size / array.length;

    return loadFactor >= DEFAULT_LOAD_FACTOR;

  }

  @SuppressWarnings("unchecked")

  private void rehashing() {

    Node [] oldArray = array;

    array = (Node []) new Node[oldArray.length * 2];

    size = 0;

    for (Node head : oldArray) {

      Node node = head;

      while (node != null) {

        put(node.key, node.value);

        node = node.next;

      }

    }

  }

  static class Node {

    K key;

    V value;

    Node next;

    public Node(K key, V value) {

      this.key = key;

      this.value = value;

      this.next = null;

    }

  }

}

7. 请实现一个十进制转二进制的算法。


public class DecimalToBinary {

  public static String decimalToBinary(int n) {

    StringBuilder sb = new StringBuilder();

    while (n > 0) {

      sb.append(n % 2);

      n /= 2;

    }

    return sb.reverse().toString();

  }

}

8. 请实现一个字符串的反转。


public class ReverseString {

  public static String reverse(String s) {

    if (s == null) {

      return null;

    }

    return new StringBuilder(s).reverse().toString();

  }

}

9. 请实现一个快速排序算法。


public class QuickSort {

  public static void quickSort(int[] arr) {

    if (arr == null || arr.length <= 1) {

      return;

    }

    quickSort(arr, 0, arr.length - 1);

  }

  private static void quickSort(int[] arr, int left, int right) {

    if (left >= right) {

      return;

    }

    int pivot = partition(arr, left, right);

    quickSort(arr, left, pivot - 1);

    quickSort(arr, pivot + 1, right);

  }

  private static int partition(int[] arr, int left, int right) {

    int pivotValue = arr[right];

    int pivotIndex = left;

    for (int i = left; i < right; i++) {

      if (arr[i] < pivotValue) {

        swap(arr, i, pivotIndex);

        pivotIndex++;

      }

    }

    swap(arr, pivotIndex, right);

    return pivotIndex;

  }

  private static void swap(int[] arr, int i, int j) {

    int temp = arr[i];

    arr[i] = arr[j];

    arr[j] = temp;

  }

}

10. 请实现一个图的深度优先遍历。


public class DFS {

  static class Graph {

    private int V;

    private LinkedList [] adj;

    public Graph(int V) {

      this.V = V;

      this.adj = new LinkedList[V];

      for (int i = 0; i < V; i++) {

        adj[i] = new LinkedList<>();

      }

    }

    public void addEdge(int v, int w) {

      adj[v].add(w);

    }

    public void DFS(int v) {

      boolean[] visited = new boolean[V];

      DFSUtil(v, visited);

    }

    private void DFSUtil(int v, boolean[] visited) {

      visited[v] = true;

      System.out.print(v + " ");

      Iterator it = adj[v].listIterator();

      while (it.hasNext()) {

        int n = it.next();

        if (!visited[n]) {

          DFSUtil(n, visited);

        }

      }

    }

  }

}

以上是本文分享的十份Java面试题及其代码案例,希望对广大面试党有所帮助。如果你想进一步了解Java知识或者其他编程语言知识,欢迎关注本公众号,获取更多优质文章和资源。

资源下载链接:百度网盘(XXXXXXX)

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复