牛客網演算法——名企高頻面試題143題(5)
阿新 • • 發佈:2020-12-13
package 名企高頻面試題143; import org.junit.Test; /** * @Classname 劃分連結串列 * @Description TODO * @Date 2020/12/12 15:33 * @Created by xjl */ public class 劃分連結串列 { public class ListNode { int val; ListNode next; public ListNode(int val) { this.val = val; } } public ListNode partition(ListNode head, int x) { if (head == null) return head; ListNode h1 = new ListNode(0); ListNode h2 = new ListNode(0); ListNode n1 = h1; ListNode n2 = h2; ListNode pre = head; while (pre != null) { if (pre.val < x) { n1.next = pre; n1 = pre; } else { n2.next = pre; n2 = pre; } pre = pre.next; } n2.next = null; n1.next = h2.next; return h1.next; } @Test public void test() { ListNode s1 = new ListNode(1); ListNode s2 = new ListNode(4); ListNode s3 = new ListNode(3); ListNode s4 = new ListNode(2); ListNode s5 = new ListNode(5); ListNode s6 = new ListNode(2); s1.next = s2; s2.next = s3; s3.next = s4; s4.next = s5; s5.next = s6; ListNode res = partition(s1, 3); while (res != null) { System.out.print(res.val+" "); res = res.next; } } } package 復現程式碼; import org.junit.Test; /** * @Classname 分割連結串列 * @Description TODO * @Date 2020/12/12 16:24 * @Created by xjl */ public class 分割連結串列 { public class ListNode { int val; ListNode next; public ListNode(int val) { this.val = val; } } public ListNode per(ListNode head, int x) { //檢查安全 if (head == null) return null; ListNode dumpy1 = new ListNode(0); ListNode dumpy2 = new ListNode(0); ListNode samll = dumpy1; ListNode large = dumpy2; ListNode pre = head; while (pre!=null){ if (pre.val>=x){ large.next=pre; large=large.next; }else { samll.next=pre; samll=samll.next; } pre=pre.next; } large.next=null; samll.next=dumpy2.next; return dumpy1.next; } @Test public void test() { ListNode s1 = new ListNode(1); ListNode s2 = new ListNode(4); ListNode s3 = new ListNode(3); ListNode s4 = new ListNode(2); ListNode s5 = new ListNode(5); ListNode s6 = new ListNode(2); s1.next = s2; s2.next = s3; s3.next = s4; s4.next = s5; s5.next = s6; ListNode res = per(s1, 3); while (res != null) { System.out.print(res.val + " "); res = res.next; } } }
題目描述
給定一個非負整數N,返回N!結果的末尾為0的數量
package 名企高頻面試題143; import org.junit.Test; /** * @Classname 階乘問題 * @Description TODO * @Date 2020/12/12 16:39 * @Created by xjl */ public class 階乘問題 { /** * @description TODO 階乘結果的0和乘數的2和5有關,而2的個數遠多於5,所以只要數5。而5,25,125的倍數是自相似的,所以可以用遞迴。時間複雜度O(logN)。 * @param: n * @date: 2020/12/12 16:41 * @return: long * @author: xjl */ public long thenumberof0(long n) { long res = 0; while(n>0){ n /= 5; res += n; } return res; } /** * @description TODO 陣列中的1的個數 * @param: n * @date: 2020/12/12 16:44 * @return: long * @author: xjl */ public long thenumberof1(int n) { int res = n; int count = 0; while (res != 0) { count++; res = res & (res - 1); } return count; } @Test public void test(){ long l = thenumberof0(5); System.out.println(l); } }
題目描述
給定彼此獨立的兩棵二叉樹,判斷 t1 樹是否有與 t2 樹拓撲結構完全相同的子樹。
設 t1 樹的邊集為E1,t2 樹的邊集為E2,若 E2等於E1 ,則表示 t1 樹和t2樹的拓撲結構完全相同。
package 名企高頻面試題143; /** * @Classname 是否包含子樹 * @Description TODO * @Date 2020/12/12 16:51 * @Created by xjl */ public class 是否包含子樹 { public class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode(int val) { this.val = val; } } /** * @description TODO 給定彼此獨立的兩棵二叉樹,判斷 t1 樹是否有與 t2 樹拓撲結構完全相同的子樹。 * @param: root1 * @param: root2 * @date: 2020/12/12 16:53 * @return: boolean * @author: xjl */ public boolean isContains(TreeNode root1, TreeNode root2) { if (root2 == null) return true; // t 為 null 一定都是 true if (root1 == null) return false; return isContains(root1.left, root2) || isContains(root1.right, root2) || isSameTree(root1,root2); } public boolean isSameTree(TreeNode s, TreeNode t){ if (s == null && t == null) return true; if (s == null || t == null) return false; if (s.val != t.val) return false; return isSameTree(s.left, t.left) && isSameTree(s.right, t.right); } }
題目描述
給定一個十進位制數M,以及需要轉換的進位制數N。將十進位制數M轉化為N進位制數
package 名企高頻面試題143;
import org.junit.Test;
/**
* @Classname 轉化為的Nj進位制數字
* @Description TODO
* @Date 2020/12/12 17:59
* @Created by xjl
*/
public class 轉化為的N進位制數字 {
public String solve(int M, int N) {
if (M == 0) return "0";
String s = "0123456789ABCDEF";
String res = "";
boolean f = false;
if (M < 0) {
f = true;
M = -M;
}
while (M != 0) {
res += s.charAt(M % N);
M /= N;
}
if (f) res += "-";
StringBuffer sb = new StringBuffer(res);
return sb.reverse().toString();
}
@Test
public void test() {
String solve = solve(7, 2);
System.out.println(solve);
}
}