pull/3/head
Leo 5 years ago
parent b9a7b31e21
commit d2c4fc9d60

@ -0,0 +1,44 @@
package leetcode;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Leo
* @ClassName lc290_wordPattern
* @DATE 2020/12/16 11:34
* @Description
* pattern str str
*/
public class lc290_wordPattern {
public static boolean wordPattern(String pattern, String s) {
char[] p = pattern.toCharArray();
String[] sArray = s.split(" ");
if (p.length != sArray.length) {
return false;
}
Map<Character, String> map = new HashMap<>();
Set<String> set = new HashSet<>();
for (int i = 0;i < sArray.length; i++) {
if(!map.containsKey(p[i])) {
if (set.contains(sArray[i])){
return false;
}
map.put(p[i],sArray[i]);
set.add(sArray[i]);
} else {
if(!map.get(p[i]).equals(sArray[i])){
return false;
}
}
}
return true;
}
public static void main(String[] args){
boolean b = wordPattern("abba","dog dog dog dog");
System.out.println(b);
}
}

@ -127,9 +127,33 @@ public class LevelTraversalBT {
System.out.println();
}
public static void level5(Node head) {
if (head == null) {
return;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(head);
Node cur = null;
Node l = null;
Node r = null;
while (!queue.isEmpty()) {
cur = queue.poll();
l = cur.left;
r = cur.right;
if (l != null) {
queue.offer(l);
}
if (r != null) {
queue.offer(r);
}
System.out.print(cur.value+" ");
}
System.out.println();
}
public static void main(String[] args){
Node node = randomTreeNode(10);
level4(node);
level5(node);
}

@ -44,10 +44,26 @@ public class PaperFolding {
processPrint1(i + 1, n, false);
}
public static void printAllFolds2(int n) {
processPrint2(1, n, true);
System.out.println();
}
static void processPrint2(int i,int n,boolean v) {
if (i > n) {
return;
}
processPrint(i + 1, n, true);
System.out.print(v ? "凹 " : "凸 ");
processPrint(i + 1, n, false);
}
public static void main(String[] args){
//凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸
//凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸
//凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸
printAllFolds1(4);
//凹 凹 凸 凹 凹 凸 凸 凹 凹 凹 凸 凸 凹 凸 凸
printAllFolds2(4);
}
}

@ -64,6 +64,25 @@ public class SerializeAndReconstructTree {
}
}
public static Queue<String> preSerial2(Node head) {
if (head == null) {
return null;
}
Queue<String> queue = new LinkedList<>();
preSerial2(head, queue);
return queue;
}
static void preSerial2(Node node, Queue<String> queue) {
if (node == null) {
queue.offer(null);
return;
}
queue.offer(String.valueOf(node.value));
preSerial2(node.left, queue);
preSerial2(node.right, queue);
}
/**
*
@ -106,6 +125,25 @@ public class SerializeAndReconstructTree {
return head;
}
static Node preDeSerial2(Queue<String> queue) {
if (queue == null) {
return null;
}
return preDeSerialProcess2(queue);
}
static Node preDeSerialProcess2(Queue<String> queue) {
String cur = queue.poll();
if (cur == null) {
return null;
}
Node head = new Node(Integer.valueOf(cur));
head.left = preDeSerialProcess2(queue);
head.right = preDeSerialProcess2(queue);
return head;
}
/**
*
@ -146,8 +184,28 @@ public class SerializeAndReconstructTree {
}
static Queue<String> posSerial2(Node head) {
if (head == null) {
return null;
}
Queue<String> queue = new LinkedList<>();
posSerial2(head, queue);
return queue;
}
static void posSerial2(Node node, Queue<String> queue) {
if (node == null) {
queue.offer(null);
}else {
posSerial2(node.left, queue);
posSerial2(node.right, queue);
queue.offer(String.valueOf(node.value));
}
}
/**
*
*
*/
public static Node posDeSerial(Queue<String> queue) {
if (queue.isEmpty() || queue == null) {
@ -196,6 +254,29 @@ public class SerializeAndReconstructTree {
}
static Node posDeSerial2(Queue<String> queue) {
if (queue == null) {
return null;
}
Stack<String> stack = new Stack<>();
while (!queue.isEmpty()) {
stack.push(queue.poll());
}
return posDeSerialProcess2(stack);
}
static Node posDeSerialProcess2(Stack<String> stack) {
String cur = stack.pop();
if (cur == null) {
return null;
}
Node head = new Node(Integer.valueOf(cur));
head.right = posDeSerialProcess2(stack);
head.left = posDeSerialProcess2(stack);
return head;
}
/**
*
*/
@ -340,13 +421,13 @@ public class SerializeAndReconstructTree {
System.out.println("start");
for (int i = 0; i < testTime; i++) {
Node head = generateRandomNode(maxLevel, range);
Queue<String> preSerial = preSerial1(head);
Queue<String> posSerial = posSerial1(head);
Node posDeSerial = posDeSerial1(posSerial);
Queue<String> preSerial = preSerial2(head);
Queue<String> posSerial = posSerial2(head);
Node posDeSerial = posDeSerial2(posSerial);
Queue<String> levelSerial = levelSerial1(head);
Node levelDeSerial = levelDeSerial1(levelSerial);
Node preDeSerial = preDeSerial1(preSerial);
Node preDeSerial = preDeSerial2(preSerial);
if (!isEqualsNode(preDeSerial, head)) {
System.out.println("preDeSerial ==> fuck");
break;

@ -157,6 +157,36 @@ public class TreeMaxWidth {
return max;
}
public static int maxWidthNoMap2(Node head) {
if (head == null) {
return 0;
}
Node curEnd = head;
Node nextEnd = null;
int curLevel = 0;
int maxLevel = 0;
Queue<Node> queue = new LinkedList<>();
queue.offer(head);
Node cur = null;
while (!queue.isEmpty()) {
cur = queue.poll();
if (cur.left != null) {
queue.offer(cur.left);
nextEnd = cur.left;
}
if (cur.right != null) {
queue.offer(cur.right);
nextEnd = cur.right;
}
curLevel++;
if (cur == curEnd) {
curEnd = nextEnd;
maxLevel = Math.max(maxLevel, curLevel);
curLevel = 0;
}
}
return maxLevel;
}
public static void main(String[] args){
@ -168,7 +198,7 @@ public class TreeMaxWidth {
for (int i = 0; i < test; i++) {
Node head = generateRandomNode(maxSize, range);
int i1 = maxWidthUseMap1(head);
int i2 = maxWidthNoMap1(head);
int i2 = maxWidthNoMap2(head);
if (i1 != i2) {
System.out.println("i1: " + i1 + " i2: " + i2);
break;

@ -15,6 +15,199 @@ import java.util.Queue;
*/
public class IsCBT {
static class ByQueue{
public static boolean isCBT(Node head) {
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(head);
boolean leaf = false;
Node cur = null;
Node l = null;
Node r = null;
while (!queue.isEmpty()) {
cur = queue.poll();
l = cur.left;
r = cur.right;
if ( //是叶节点并且有子孩子
(leaf && (l != null || r != null))
||
(l == null && r != null)//当没有左节点时
) {
return false;
}
if (l != null) {
queue.offer(l);
}
if (r != null) {
queue.offer(r);
}
if (l == null || r == null) {
leaf = true;
}
}
return true;
}
public static boolean isCBT1(Node head){
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(head);
Node cur;
Node l = null;
Node r = null;
boolean leaf = false;
while (!queue.isEmpty()) {
cur = queue.poll();
l = cur.left;
r = cur.right;
if (l!= null) {
queue.offer(l);
}
if (r != null) {
queue.offer(r);
}
if ((leaf &&
(l != null || r != null))
|| (l == null && r != null)
) {
return false;
}
if (l == null || r == null) {
leaf = true;
}
}
return true;
}
}
static class Recursion{
public static boolean isCBT(Node head) {
return process(head).isCBT;
}
public static Info process(Node node){
if (node == null) {
return new Info(true, true, 0);
}
Info left = process(node.left);
Info right = process(node.right);
int height = Math.max(left.height, right.height) + 1;
boolean isFull = left.isFull && right.isFull && right.height == left.height;
boolean isCBT = false;
if (isFull) {
isCBT = true;
}else{
//如果左右都不是满二叉树的情况下
//左树是满二叉树并且右树是满二叉树
if (left.isCBT && right.isCBT) {
//左树是二叉树,右树是满二叉树,左树的高度比右树高度大1
if (left.isCBT && right.isFull && left.height == right.height + 1) {
isCBT = true;
}
//左树是满二叉树,右树是满二叉树,左树的高度比右树高度大1
if (left.isFull && right.isFull && left.height == right.height + 1) {
isCBT = true;
}
//左树是满二叉树,右树是二叉树,左树高度与右树高度一样
if (left.isFull && right.isCBT && right.height == left.height) {
isCBT = true;
}
}
}
return new Info(isFull, isCBT, height);
}
static class Info{
boolean isFull;
boolean isCBT;
int height;
public Info(boolean f,boolean c,int h){
this.isFull = f;
this.isCBT = c;
this.height = h;
}
}
}
static class Recursion1{
public static boolean isCBT(Node head){
return process(head).isCBT;
}
public static class Info{
public boolean isFull;
public boolean isCBT;
public int height;
public Info(boolean f,boolean c,int h){
this.isFull = f;
this.isCBT = c;
this.height = h;
}
}
public static Info process(Node node){
if (node == null){
return new Info(true, true, 0);
}
Info left = process(node.left);
Info right = process(node.right);
boolean isFull = left.isFull && right.isFull && left.height == right.height;
int height = Math.max(left.height, right.height) + 1;
boolean isCBT = isFull;
if (left.isCBT && right.isFull && left.height == right.height + 1) {
isCBT = true;
} else if (left.isFull && right.isFull && left.height == right.height + 1){
isCBT = true;
} else if (left.isFull && right.isCBT && left.height == right.height) {
isCBT = true;
}
return new Info(isFull, isCBT, height);
}
}
static class Recursion2{
static boolean isCBT(Node head) {
return process(head).isCBT;
}
static Info process(Node node){
if (node == null) {
return new Info(true, true, 0);
}
Info left = process(node.left);
Info right = process(node.right);
boolean isFull = left.isFull && right.isFull && right.height == left.height;
int height = Math.max(right.height, left.height) + 1;
boolean isCBT = isFull;
if (left.isFull && right.isCBT && left.height == right.height) {
isCBT = true;
} else if (left.isCBT && right.isFull && left.height == right.height + 1) {
isCBT = true;
} else if (left.isFull && right.isFull && left.height == right.height + 1) {
isCBT = true;
}
return new Info(isFull, isCBT, height);
}
static class Info {
boolean isFull;
boolean isCBT;
int height;
public Info(boolean isFull, boolean isCBT, int h) {
this.isFull = isFull;
this.isCBT = isCBT;
this.height = h;
}
}
}
public static void main(String[] args){
int level = 4;
int range = 100;
@ -24,7 +217,7 @@ public class IsCBT {
for (int i = 0; i < testTime; i++) {
Node head = generateRandomNode(level, range);
boolean queue = ByQueue.isCBT1(head);
boolean recursion = Recursion.isCBT(head);
boolean recursion = Recursion2.isCBT(head);
if (queue != recursion) {
System.out.println("fuck!!!");
break;
@ -49,121 +242,3 @@ public class IsCBT {
}
class ByQueue{
public static boolean isCBT(Node head) {
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(head);
boolean leaf = false;
Node cur = null;
Node l = null;
Node r = null;
while (!queue.isEmpty()) {
cur = queue.poll();
l = cur.left;
r = cur.right;
if ( //是叶节点并且有子孩子
(leaf && (l != null || r != null))
||
(l == null && r != null)//当没有左节点时
) {
return false;
}
if (l != null) {
queue.offer(l);
}
if (r != null) {
queue.offer(r);
}
if (l == null || r == null) {
leaf = true;
}
}
return true;
}
public static boolean isCBT1(Node head){
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(head);
Node cur;
Node l = null;
Node r = null;
boolean leaf = false;
while (!queue.isEmpty()) {
cur = queue.poll();
l = cur.left;
r = cur.right;
if (l!= null) {
queue.offer(l);
}
if (r != null) {
queue.offer(r);
}
if ((leaf &&
(l != null || r != null))
|| (l == null && r != null)
) {
return false;
}
if (l == null || r == null) {
leaf = true;
}
}
return true;
}
}
class Recursion{
public static boolean isCBT(Node head) {
return process(head).isCBT;
}
public static Info process(Node node){
if (node == null) {
return new Info(true, true, 0);
}
Info left = process(node.left);
Info right = process(node.right);
int height = Math.max(left.height, right.height) + 1;
boolean isFull = left.isFull && right.isFull && right.height == left.height;
boolean isCBT = false;
if (isFull) {
isCBT = true;
}else{
//如果左右都不是满二叉树的情况下
//左树是满二叉树并且右树是满二叉树
if (left.isCBT && right.isCBT) {
//左树是二叉树,右树是满二叉树,左树的高度比右树高度大1
if (left.isCBT && right.isFull && left.height == right.height + 1) {
isCBT = true;
}
//左树是满二叉树,右树是满二叉树,左树的高度比右树高度大1
if (left.isFull && right.isFull && left.height == right.height + 1) {
isCBT = true;
}
//左树是满二叉树,右树是二叉树,左树高度与右树高度一样
if (left.isFull && right.isCBT && right.height == left.height) {
isCBT = true;
}
}
}
return new Info(isFull, isCBT, height);
}
static class Info{
boolean isFull;
boolean isCBT;
int height;
public Info(boolean f,boolean c,int h){
this.isFull = f;
this.isCBT = c;
this.height = h;
}
}
}

@ -1,7 +1,5 @@
package leo.class08_12;
import class08_12.Code08_MaxDistance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

@ -0,0 +1,54 @@
package leo.class09_13;
/**
* @author Leo
* @ClassName lowestAncestor
* @DATE 2020/12/16 8:59
* @Description
* headab
* ab
*/
public class LowestAncestor {
static class Code {
static Node lowestAncestor(Node head, Node a, Node b) {
return process(head, a, b).ans;
}
static Info process(Node node, Node a, Node b) {
if (node == null) {
return new Info(null, false, false);
}
Info left = process(node, a, b);
Info right = process(node, a, b);
boolean findA = left.findA || right.findA || a == node;
boolean findB = left.findB || right.findB || b == node;
Node ans = null;
if (left.ans != null) {
ans = left.ans;
}
if (right.ans != null) {
ans = right.ans;
}
if (ans == null) {
if (findA && findB) {
ans = node;
}
}
return new Info(ans, findA, findB);
}
static class Info {
Node ans;
boolean findA;
boolean findB;
public Info(Node ans, boolean findA, boolean findB) {
this.ans = ans;
this.findA = findA;
this.findB = findB;
}
}
}
}

@ -0,0 +1,10 @@
package leo.class09_13;
/**
* @author Leo
* @ClassName LowestLexicography
* @DATE 2020/12/17 2:01
* @Description
*/
public class LowestLexicography {
}

@ -0,0 +1,172 @@
package leo.class09_13;
import java.util.ArrayList;
import java.util.List;
/**
* @author Leo
* @ClassName MaxHappy
* @DATE 2020/12/16 9:01
* @Description
* Employee
*
* (subordinates)
* party
* 1.
* 2.
* 3.
* boss
*/
public class MaxHappy {
static class Code {
public static int maxHappy(Employee node){
if (node == null) {
return 0;
}
Info info = process(node);
return Math.max(info.no, info.yes);
}
public static Info process(Employee node) {
if (node == null) {
return new Info(0, 0);
}
int yes = node.happy;
int no = 0;
List<Employee> nexts = node.nexts;
for (Employee next : nexts) {
Info nextInfo = process(next);
yes += nextInfo.no;
no += Math.max(nextInfo.yes, nextInfo.no);
}
return new Info(no, yes);
}
static class Info {
int no;
int yes;
public Info(int no, int yes) {
this.no = no;
this.yes = yes;
}
}
}
static class Code1 {
static int maxHappy(Employee employee) {
if (employee == null) {
return 0;
}
Info info = process(employee);
return Math.max(info.no, info.yes);
}
static Info process(Employee e) {
if (e == null) {
return new Info(0, 0);
}
int yes = e.happy;
int no = 0;
List<Employee> nexts = e.nexts;
for (Employee next : nexts) {
Info info = process(next);
yes += info.no;
no += Math.max(info.no, info.yes);
}
return new Info(yes, no);
}
static class Info {
int yes;
int no;
public Info(int yes, int no) {
this.yes = yes;
this.no = no;
}
}
}
static class Logarithm {
public static int maxHappy(Employee employee) {
if (employee == null) {
return 0;
}
return process(employee,false);
}
static int process(Employee e,boolean verify) {
if (verify) {
int ans = 0;
List<Employee> nexts = e.nexts;
for (Employee next : nexts) {
ans += process(next, false);
}
return ans;
}
int yes = e.happy;
int no = 0;
List<Employee> nexts = e.nexts;
for (Employee next : nexts) {
yes += process(next, true);
no += process(next, false);
}
return Math.max(yes, no);
}
}
public static void main(String[] args){
int maxLevel = 5;
int listSize = 4;
int range = 1000;
int testTime = 10000;
System.out.println("start");
for (int i = 0; i < testTime; i++) {
Employee head = new Employee((int) (range * Math.random() + 1));
generateNexts(head, 1, maxLevel, listSize, range);
int i1 = Code1.maxHappy(head);
int i2 = Logarithm.maxHappy(head);
if (i1 != i2) {
System.out.println("FUCK!!!");
break;
}
}
System.out.println("end");
}
static void generateNexts(Employee node, int i, int n, int s, int r) {
if (i > n) {
return;
}
int nextSize = (int) (s * Math.random()) + 1;
for (int j = 0; j < nextSize; j++) {
Employee e = new Employee((int) (r * Math.random() + 1));
node.nexts.add(e);
generateNexts(e, i + 1, n, s, r);
}
}
static class Employee {
public int happy;
public List<Employee> nexts;
public Employee(int h) {
happy = h;
nexts = new ArrayList<>();
}
}
}

@ -0,0 +1,204 @@
package leo.class09_13;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
/**
* @author Leo
* @ClassName MaxSubBSTHead
* @DATE 2020/12/15 10:33
* @Description
* head
*
*/
public class MaxSubBSTHead {
static class Code {
public static Node maxSubBSTHead(Node head) {
if (head == null) {
return null;
}
return process(head).maxSubSizeHead;
}
public static Info process(Node node){
if (node == null) {
return null;
}
Info left = process(node.left);
Info right = process(node.right);
Node maxSubBSTHead = null;
int maxSubSize = 0;
int max = node.value;
int min = node.value;
if (left != null) {
max = Math.max(max, left.max);
min = Math.min(min, left.min);
maxSubSize = left.maxSubSize;
maxSubBSTHead = left.maxSubSizeHead;
}
if (right != null) {
max = Math.max(max, right.max);
min = Math.min(min, right.min);
if (right.maxSubSize > maxSubSize) {
maxSubSize = right.maxSubSize;
maxSubBSTHead = right.maxSubSizeHead;
}
}
if (
(left == null ? true : left.max < node.value) &&
(right == null ? true : right.min > node.value) &&
(left == null ? true : left.maxSubSizeHead == node.left) &&
(right == null ? true : right.maxSubSizeHead == node.right)
) {
maxSubSize = (left == null ? 0 : left.maxSubSize) + (right == null ? 0 : right.maxSubSize) + 1;
maxSubBSTHead = node;
}
return new Info(maxSubSize, maxSubBSTHead, max, min);
}
static class Info{
int maxSubSize;
Node maxSubSizeHead;
int max ;
int min;
public Info(int maxSubSize, Node maxSubSizeHead, int max, int min) {
this.maxSubSize = maxSubSize;
this.maxSubSizeHead = maxSubSizeHead;
this.max = max;
this.min = min;
}
}
}
static class Code1 {
static Node maxSubBSTHead(Node head) {
if (head == null) {
return null;
}
return process(head).maxSubHead;
}
public static Info process(Node node) {
if (node == null) {
return null;
}
Info left = process(node.left);
Info right = process(node.right);
Node maxSubHead = null;
int maxSubSize = 0;
int max = node.value;
int min = node.value;
if (left != null) {
max = Math.max(max, left.max);
min = Math.min(min, left.min);
maxSubSize = left.maxSubSize;
maxSubHead = left.maxSubHead;
}
if (right != null) {
max = Math.max(max, right.max);
min = Math.min(min, right.min);
if (right.maxSubSize>maxSubSize){
maxSubHead = right.maxSubHead;
maxSubSize = right.maxSubSize;
}
}
if ((left == null ? true : left.max < node.value) &&
(right == null ? true : right.min > node.value) &&
(left == null ? true : left.maxSubHead == node.left) &&
(right == null ? true : right.maxSubHead == node.right)
) {
maxSubHead = node;
maxSubSize = (left == null ? 0 : left.maxSubSize) + (right == null ? 0 : right.maxSubSize) + 1;
}
Map<Character,String> set = new HashMap<Character,String>();
return new Info(maxSubHead, maxSubSize, max, min);
}
static class Info {
Node maxSubHead;
int maxSubSize;
int max ;
int min;
public Info(Node maxSubHead, int maxSubSize, int max, int min) {
this.maxSubHead = maxSubHead;
this.maxSubSize = maxSubSize;
this.max = max;
this.min = min;
}
}
}
static class Logarithm {
public static int getBSTSize(Node head) {
if (head == null) {
return 0;
}
ArrayList<Node> arr = new ArrayList<>();
in(head, arr);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i).value <= arr.get(i - 1).value) {
return 0;
}
}
return arr.size();
}
public static void in(Node head, ArrayList<Node> arr) {
if (head == null) {
return;
}
in(head.left, arr);
arr.add(head);
in(head.right, arr);
}
public static Node maxSubBSTHead(Node head) {
if (head == null) {
return null;
}
if (getBSTSize(head) != 0) {
return head;
}
Node leftAns = maxSubBSTHead(head.left);
Node rightAns = maxSubBSTHead(head.right);
return getBSTSize(leftAns) >= getBSTSize(rightAns) ? leftAns : rightAns;
}
}
// for test
public static Node generateRandomBST(int maxLevel, int maxValue) {
return generate(1, maxLevel, maxValue);
}
// for test
public static Node generate(int level, int maxLevel, int maxValue) {
if (level > maxLevel || Math.random() < 0.5) {
return null;
}
Node head = new Node((int) (Math.random() * maxValue));
head.left = generate(level + 1, maxLevel, maxValue);
head.right = generate(level + 1, maxLevel, maxValue);
return head;
}
public static void main(String[] args) {
int maxLevel = 4;
int maxValue = 100;
int testTimes = 1000000;
for (int i = 0; i < testTimes; i++) {
Node head = generateRandomBST(maxLevel, maxValue);
if (Code1.maxSubBSTHead(head) != Logarithm.maxSubBSTHead(head)) {
System.out.println("Oops!");
}
}
System.out.println("finish!");
}
}

@ -0,0 +1,16 @@
package leo.class09_13;
/**
* @author Leo
* @ClassName Node
* @DATE 2020/12/11 3:31
* @Description
*/
public class Node {
int value;
Node left;
Node right;
protected Node(int v){
this.value = v;
}
}
Loading…
Cancel
Save