8.16
This commit is contained in:
parent
b82cf7eb92
commit
a1b5eeb70d
@ -54,7 +54,7 @@ public class Test {
|
||||
FileOutputStream fos;
|
||||
InputStreamReader isr;
|
||||
OutputStreamWriter osw;
|
||||
int b = 'z';
|
||||
float b = Math.round(11.5);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,50 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
/**
|
||||
* 机器人运动范围.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-19:37
|
||||
*/
|
||||
public class Android {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
public int movingCount(int threshold, int rows, int cols) {
|
||||
boolean[] visited = new boolean[rows * cols];
|
||||
for (int i = 0; i < rows * cols; i++) {
|
||||
visited[i] = false;
|
||||
}
|
||||
return helper(threshold, rows, cols, 0, 0, visited);
|
||||
}
|
||||
|
||||
private int helper(int threshold, int rows, int cols, int row, int col, boolean[] visited) {
|
||||
int count = 0;
|
||||
if (check(threshold, rows, cols, row, col, visited)) {
|
||||
count = 1 + helper(threshold, rows, cols, row - 1, col, visited) +
|
||||
helper(threshold, rows, cols, row + 1, col, visited)
|
||||
+ helper(threshold, rows, cols, row, col - 1, visited)
|
||||
+ helper(threshold, rows, cols, row, col + 1, visited);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private boolean check(int threshold, int rows, int cols, int row, int col, boolean[] visited) {
|
||||
if (getSum(row) + getSum(col) <= threshold && !visited[row * cols + col] && row < rows
|
||||
&& col < cols && row >= 0 && col >= 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private int getSum(int a) {
|
||||
int sum = 0;
|
||||
while (a > 0) {
|
||||
sum += a % 10;
|
||||
a /= 10;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
/**
|
||||
* t2: 二维数组的查找.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-14:05
|
||||
*/
|
||||
public class ArrayFind {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[][] matrix = new int[][]{{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
|
||||
System.out.println(find(matrix, 1));
|
||||
}
|
||||
|
||||
public static boolean find(int[][] arr, int target) {
|
||||
if (arr == null || arr.length == 0 || arr[0].length == 0) {
|
||||
return false;
|
||||
}
|
||||
int row = arr.length;
|
||||
int col = arr[0].length;
|
||||
for (int i = row - 1; i >= 0; i--) {
|
||||
for (int j = 0; j < col; j++) {
|
||||
if (arr[i][j] > target) {
|
||||
break;
|
||||
} else if (arr[i][j] == target) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
/**
|
||||
* 求最小的k个数.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-17:31
|
||||
*/
|
||||
public class MinK {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] test = new int[]{4,5,1,6,2,7,3,8};
|
||||
minK(test, 8);
|
||||
}
|
||||
|
||||
private static ArrayList<Integer> minK(int[] input, int k) {
|
||||
ArrayList<Integer> ret = new ArrayList<>();
|
||||
if (k == 0 || input == null || input.length == 0 || k > input.length) {
|
||||
return ret;
|
||||
}
|
||||
PriorityQueue<Integer> queue = new PriorityQueue<>(k, Comparator.reverseOrder());
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
if( i < k){
|
||||
queue.add(input[i]);
|
||||
}else {
|
||||
if (input[i] < queue.peek()) {
|
||||
queue.poll();
|
||||
queue.add(input[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
while (!queue.isEmpty()){
|
||||
ret.add(queue.poll());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
/**
|
||||
* 二叉树的镜像.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-16:52
|
||||
*/
|
||||
public class MirrorTree {
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
public void Mirror(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.left == null && root.right == null) {
|
||||
return;
|
||||
}
|
||||
TreeNode temp = root.left;
|
||||
root.left = root.right;
|
||||
root.right = temp;
|
||||
Mirror(root.left);
|
||||
Mirror(root.right);
|
||||
}
|
||||
|
||||
static class TreeNode {
|
||||
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
|
||||
TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* 从尾到头打印链表.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-14:39
|
||||
*/
|
||||
public class PrintListEnd2Head {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
ArrayList<Integer> res = new ArrayList<>();
|
||||
while (listNode != null) {
|
||||
stack.push(listNode.val);
|
||||
listNode = listNode.next;
|
||||
}
|
||||
while (!stack.isEmpty()) {
|
||||
res.add(stack.pop());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static class ListNode {
|
||||
|
||||
int val;
|
||||
ListNode next = null;
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
|
||||
/**
|
||||
* 重建二叉树.
|
||||
*
|
||||
* 输入二叉树的前序、中序遍历的结果,重建二叉树
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-14:44
|
||||
*/
|
||||
public class ReconstructBinTree {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] pre = new int[] {1, 2, 4, 7, 3, 5, 6, 8};
|
||||
int[] in = new int[] {4, 7, 2, 1, 5, 3, 8, 6};
|
||||
TreeNode node = reConstructBinaryTree(pre, in);
|
||||
}
|
||||
|
||||
public static TreeNode reConstructBinaryTree(int[] pre, int[] in) {
|
||||
if (pre == null || in == null || pre.length != in.length) {
|
||||
return null;
|
||||
}
|
||||
return construct(pre, 0, pre.length - 1, in, 0, in.length - 1);
|
||||
}
|
||||
|
||||
private static TreeNode construct(int[] pre, int preStart, int preEnd, int[] in, int inStart,
|
||||
int inEnd) {
|
||||
if (preStart > preEnd || inStart > inEnd) {
|
||||
return null;
|
||||
}
|
||||
int rootVal = pre[preStart];
|
||||
TreeNode root = new TreeNode(rootVal);
|
||||
for (int i = 0; i < in.length; i++) {
|
||||
if (in[i] == rootVal) {
|
||||
int leftLength = i - inStart;
|
||||
root.left = construct(pre, preStart + 1, preStart + leftLength, in, inStart, i - 1);
|
||||
root.right = construct(pre, preStart + leftLength + 1, preEnd, in, i + 1, inEnd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
static class TreeNode {
|
||||
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
|
||||
TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
/**
|
||||
* 替换空格.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-14:22
|
||||
*/
|
||||
public class ReplaceSpace {
|
||||
|
||||
public static void main(String[] args) {
|
||||
StringBuffer sb = new StringBuffer("Hello world");
|
||||
String t = replaceSpace(sb);
|
||||
System.out.println(t);
|
||||
}
|
||||
|
||||
public static String replaceSpace(StringBuffer sb) {
|
||||
if (sb == null) {
|
||||
return null;
|
||||
}
|
||||
int oldLength = sb.length();
|
||||
for (int i = 0; i < oldLength; i++) {
|
||||
if (sb.charAt(i) == ' ') {
|
||||
sb.append(" ");
|
||||
}
|
||||
}
|
||||
int newLength = sb.length();
|
||||
int oldIndex = oldLength - 1;
|
||||
int newIndex = newLength - 1;
|
||||
for (; (oldIndex >= 0 && newIndex > oldIndex); oldIndex--) {
|
||||
if (sb.charAt(oldIndex) == ' ') {
|
||||
sb.setCharAt(newIndex--, '0');
|
||||
sb.setCharAt(newIndex--, '2');
|
||||
sb.setCharAt(newIndex--, '%');
|
||||
} else {
|
||||
sb.setCharAt(newIndex--, sb.charAt(oldIndex));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
/**
|
||||
* 单例模式.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-11:59
|
||||
*/
|
||||
public class Singleton {
|
||||
|
||||
private static volatile Singleton instance;
|
||||
|
||||
private Singleton() {
|
||||
}
|
||||
|
||||
public static Singleton newInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (Singleton.class) {
|
||||
if (instance == null) {
|
||||
instance = new Singleton();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.raorao.java.althorithm.offer;
|
||||
|
||||
/**
|
||||
* 判断树的子结构.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-16-16:33
|
||||
*/
|
||||
public class SubTree {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
public boolean HasSubtree(TreeNode root1, TreeNode root2) {
|
||||
boolean result = false;
|
||||
if (root1 != null && root2 != null) {
|
||||
if (root1.val == root2.val) {
|
||||
result = doesTree1HaveTree2(root1, root2);
|
||||
}
|
||||
if (!result) {
|
||||
result = HasSubtree(root1.left, root2);
|
||||
}
|
||||
if (!result) {
|
||||
result = HasSubtree(root1.right, root2);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 当两个树的根节点相同的时候,两个树
|
||||
private boolean doesTree1HaveTree2(TreeNode root1, TreeNode root2) {
|
||||
if (root1 == null && root2 == null) {
|
||||
return true;
|
||||
}
|
||||
if (root1 == null) {
|
||||
return false;// 空节点不会包含其他子节点
|
||||
}
|
||||
if (root2 == null) {
|
||||
return true; // 空节点是任何节点的子节点
|
||||
}
|
||||
if (root1.val != root2.val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return doesTree1HaveTree2(root1.left, root2.left) && doesTree1HaveTree2(root1.right,
|
||||
root2.right);
|
||||
}
|
||||
|
||||
static class TreeNode {
|
||||
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
|
||||
TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,15 +2,70 @@
|
||||
# 牛客网拼多多面试题
|
||||
|
||||
1. 一亿个数找出top 100(手写)
|
||||
|
||||
先对数据进行分分割,然后构建一个长度为100的小顶堆。
|
||||
|
||||
2. 寻找结点的共同双亲
|
||||
|
||||
|
||||
|
||||
3. 一个数组 找出所有和为n的种类数目
|
||||
|
||||
> 先对数组进行排序,然后使用一前一后两个指针,慢慢找到和为n的种类数目
|
||||
|
||||
4. 给定两个稀疏矩阵 求乘法 要优化后的
|
||||
|
||||
|
||||
作者:团子s1
|
||||
链接:https://www.nowcoder.com/discuss/94066?type=2&order=0&pos=13&page=1
|
||||
来源:牛客网
|
||||
|
||||
1. 在纸上写一个一个链表排序,并拍照发过去(实现了一个冒泡的链表排序,被鄙视之,嫌弃空间复杂度和时间复杂度)
|
||||
|
||||
链表排序,先转数组。。。
|
||||
|
||||
2. 在纸上写一个Binary Search Tree的建立函数
|
||||
|
||||
二叉搜索,二分法
|
||||
|
||||
3. 说一说事务的ACID,其中把事务的隔离性让我单独详细解释了一遍
|
||||
|
||||
数据库隔离:
|
||||
|
||||
事务隔离级别 | 脏读 | 不可重复读 | 幻读
|
||||
---|---|---|---
|
||||
读未提交(read-uncommitted) | 是 | 是 | 是
|
||||
不可重复读(read-committed) | 否 | 是 | 是
|
||||
可重复读(repeatable-read) | 否 | 否 | 是
|
||||
串行化(serializable) | 否 | 否 | 否
|
||||
|
||||
4. 说一说幻影读
|
||||
|
||||
线程A读取表的内容,线程B刚好在线程A读取的内容作了新增或者修改的操作。
|
||||
|
||||
5. 说一说HashMap和concurrentHashMap
|
||||
|
||||
常见问题
|
||||
|
||||
6. 说一说如何解决hash冲突的,以及如果冲突了,怎么在hash表中找到目标值
|
||||
|
||||
链表法,再哈希法
|
||||
|
||||
7. 说一说jdk1.8中,对hashMap的优化
|
||||
|
||||
hash 链表长度大于8就改为红黑树
|
||||
|
||||
8. 说一说jdk1.8中对concurrentHashMap的优化
|
||||
|
||||
使用synchronized + cas,1.7 是用的Segment锁
|
||||
|
||||
1、手撕小顶堆
|
||||
|
||||
参考堆排序的内容
|
||||
|
||||
2、剑指offer题目
|
||||
|
||||
8.16 重新刷了一遍
|
||||
|
||||
拼多多:
|
||||
逻辑思维,编程,项目经验,科研能力
|
||||
|
Loading…
x
Reference in New Issue
Block a user