三面算法不難,數據庫問了聯合索引和最左匹配,主要問了項目上的細節,項目上遇到的問題和如何解決的,比較難的是長短鏈接服務整個設計流程的實現;
問題列表
- 單鏈表的反轉,鏈接結構、測試全部完成;
- 聯合索引和最左匹配
一個表table有3個字段 A,B,C
select * from table where A=? and B=?
select * from table where C=? and B=?
select * from table where B=?
- 長短鏈接服務整個設計,算法怎麼實現?(難)
代碼實現
單鏈表的反轉就是頭插法了,三個指針就搞定,不過要讓自己完成整個鏈表的過程還是比較麻煩的;
package bytedance.interview;
import java.util.ArrayList;
/**
* Copyright (c) 2020.
* Email: [email protected]
*
* @author lyg 2020/3/22 下午5:18
* description:
**/
class LinkList<T> {
private LinkNode<T> head = null;
static class LinkNode<T> {
private T value;
private LinkNode<T> next;
LinkNode(T val) {
this.value = val;
this.next = null;
}
}
LinkList(T[] array) {
LinkNode<T> p, q = null;
for (T val : array) {
p = new LinkNode<>(val);
if (this.head == null) {
this.head = p;
q = p;
} else {
q.next = p;
q = p;
}
}
}
void print() {
LinkNode<T> p = this.head;
while (p != null) {
System.out.print(p.value + "\t");
p = p.next;
}
System.out.println();
}
void reverse() {
LinkNode<T> p = this.head, q = null, r;
while (p != null) {
r = p.next;
p.next = q;
q = p;
p = r;
}
this.head = q;
}
}
public class Solution3 {
public static void main(String[] args) {
Integer[] array = {1, 2, 3, 4, 5};
LinkList<Integer> linkList = new LinkList<Integer>(array);
linkList.print();
linkList.reverse();
linkList.print();
}
}