1,二叉树多个节点的最近公共祖先,时间复杂度
public static Node solve(Node node, int[] arr){
if(node == null){
return null;
}
for(int i = 0; i < arr.length; i++){
if(arr[i] == node.val){
return node;
}
}
Node left = solve(node.left,arr);
Node right = solve(node.right,arr);
if(left != null && right != null){
return node;
}else if (left != null){
return left;
}else {
return right;
}
}
2,rand5()转rand7()
public static int rand7(){
int t = 100;
while (t>21){
t = (rand5() - 1) * 5 + rand5();
}
return t % 7 + 1;
}
public static int rand5(){
return (int)(Math.random() * 5) + 1;
}
3,最大栈、最小栈
4,最小生成树算法
5,根据前序和中序遍历数组求后序遍历数组
6,集合的所有子集
7,删除字符串中连续相同的字符
8,一个无序数组除了一个数都出现偶数次,且相邻出现,比如112334444400,找出单独出现的数字
9,二分判断波峰
10,大数相加(字符串)
11,给定数组,每个元素代表一个木头的长度,木头可以任意截断,从这堆木头中截出至少k个相同长度为m的木块,已知k,求max(m)
12,给定数组[1,2,3]和一个值x,数字可以重复选取,找出所有和等于x的结果
13,最长上升子序列,[1,5,8,2,6,7,4,1,3] 1,2,6,7,8
14,已知二叉树的先序遍历序列,输出它的中序遍历序列,如ABC##D#E###,输出中序CBDEA
15,实现一个减法器 12345.789-3333675.32455
16,拓扑排序
17,32GB的文件,1GB的内存,怎么实现排序
18,栈实现队列:正确性证明,时间复杂度优化,时间复杂度计算,多线程环境下顺序保证
19,LRU leetcode 146
20,二叉排序树第k大节点
class Main {
int res, k;
public int kthLargest(TreeNode root, int k) {
this.k = k;
dfs(root);
return res;
}
public void dfs(TreeNode root) {
if(root == null) return;
dfs(root.right);
if(k == 0) return;
if(--k == 0) res = root.val;
dfs(root.left);
}
}
21,排序数组求指定数的个数
22,链表反转指定下标之间的节点
23,棋子只能向右和向下走问有多少条路径
24,最长公共前缀子串
25,0-9的环,从0出发,N步之后能否走回0 https://blog.csdn.net/chaoyue1216/article/details/8096223
26,leetcode 82,删除链表的重复元素
27,leetcode 1,不用额外空间(预排序、双指针)
28,leetcode 15三数之和,,k数之和
29,leetcode 39 组合总数
30,leetcode 兑换零钱
31,给定一个数组代表股票每天的价格,请问只能买卖一次的最大利润,买卖多次的最大化利润
32,两个数组,任意长度,两个线程分别读取数组a和数组B,交叉打印
33,青蛙跳台阶,共有n阶,每次最多跳k阶,有多少种跳法
public static int jump(int target, int d){
int[] dp = new int[target+1];
dp[0] = dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= target; i++) {
for (int j = 1; j <=d; j++) {
if (i - j >= 0){
dp[i] += dp[i-j];
}
}
}
return dp[target];
}
34,最少硬币
35,铺瓷砖
public class MyQueue {
Deque<Integer> stack1;
Deque<Integer> stack2;
/**
* Initialize your data structure here.
*/
public MyQueue() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
/**
* Push element x to the back of queue.
*/
public void push(int x) {
stack1.push(x);
}
/**
* Removes the element from in front of queue and returns that element.
*/
public synchronized int pop() {
if(!stack2.isEmpty())
return stack2.pop();
while (!stack1.isEmpty())
stack2.push(stack1.pop());
return stack2.pop();
}
/**
* Get the front element.
*/
public synchronized int peek() {
if(!stack2.isEmpty())
return stack2.peek();
while (!stack1.isEmpty())
stack2.push(stack1.pop());
return stack2.peek();
}
/**
* Returns whether the queue is empty.
*/
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}
leetcode
1,15,25,39,53,238,