right = trees(i + 1, end);
- for(TreeNode l : left) {
- for (TreeNode r: right) {
+ for (TreeNode l : left) {
+ for (TreeNode r : right) {
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
diff --git a/[0095][Unique Binary Search Trees II]/src/TreeNode.java b/[0095][Unique Binary Search Trees II]/src/TreeNode.java
index 8c356eb..2878b91 100644
--- a/[0095][Unique Binary Search Trees II]/src/TreeNode.java
+++ b/[0095][Unique Binary Search Trees II]/src/TreeNode.java
@@ -3,7 +3,7 @@
* @time: 2019-06-28 16:14
**/
public class TreeNode {
- int val;
+ int val;
TreeNode left;
TreeNode right;
diff --git a/[0096][Unique Binary Search Trees]/src/Solution2.java b/[0096][Unique Binary Search Trees]/src/Solution2.java
index 753d48c..31624d1 100644
--- a/[0096][Unique Binary Search Trees]/src/Solution2.java
+++ b/[0096][Unique Binary Search Trees]/src/Solution2.java
@@ -7,18 +7,18 @@
public class Solution2 {
/**
* Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
- *
+ *
* For example,
* Given n = 3, there are a total of 5 unique BST's.
- * 1 3 3 2 1
- * \ / / / \ \
- * 3 2 1 1 3 2
- * / / \ \
+ * 1 3 3 2 1
+ * \ / / / \ \
+ * 3 2 1 1 3 2
+ * / / \ \
* 2 1 2 3
- *
+ *
* ƹʽ
* f(k)*f(n-1-k)f(k)ʾk㣬е״f(k)f(n-1-k)ʾn-1-k
- *
+ *
* f(n) = 2*f(n-1) + f(1)*f(n-2) + f(2)f(n-3) + f(3)f(n-4) + ... +f(n-2)*f(1)
*
* @param n
@@ -41,8 +41,8 @@ public int numTrees(int n) {
for (int i = 2; i <= n; i++) {
// f(i)
result[i] = 2 * result[i - 1];
- for (int j = 1; j <= i - 1 ; j++) {
- result[i] += result[j]*result[i - 1 -j];
+ for (int j = 1; j <= i - 1; j++) {
+ result[i] += result[j] * result[i - 1 - j];
}
}
diff --git a/[0100][Same Tree]/src/TreeNode.java b/[0100][Same Tree]/src/TreeNode.java
index 58ea6f4..a4b81b6 100644
--- a/[0100][Same Tree]/src/TreeNode.java
+++ b/[0100][Same Tree]/src/TreeNode.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-08-21
* Time: 18:40
* Declaration: All Rights Reserved !!!
@@ -8,5 +8,8 @@ public class TreeNode {
int val;
TreeNode left;
TreeNode right;
- TreeNode(int x) { val = x; }
+
+ TreeNode(int x) {
+ val = x;
+ }
}
diff --git a/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Main.java b/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Main.java
index 232e3c8..c3569f8 100644
--- a/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Main.java
+++ b/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Main.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-06-23
* Time: 10:32
* Declaration: All Rights Reserved !!!
@@ -7,8 +7,8 @@
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
- int[] inorder = {1,2,3,4};
- int[] postorder = {3,2,4,1};
+ int[] inorder = {1, 2, 3, 4};
+ int[] postorder = {3, 2, 4, 1};
TreeNode root = solution.buildTree(inorder, postorder);
print(root);
diff --git a/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java b/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java
index 83cccad..d6415c7 100644
--- a/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java
+++ b/[0106][Construct Binary Tree From Inorder And Postorder Traversal]/src/Solution.java
@@ -1,4 +1,3 @@
-
/**
* Author:
* Date: 2015-06-23
diff --git a/[0114][Flatten Binary Tree To Linked List]/src/TreeNode.java b/[0114][Flatten Binary Tree To Linked List]/src/TreeNode.java
index c988e88..463b6fe 100644
--- a/[0114][Flatten Binary Tree To Linked List]/src/TreeNode.java
+++ b/[0114][Flatten Binary Tree To Linked List]/src/TreeNode.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-08-21
* Time: 19:02
* Declaration: All Rights Reserved !!!
@@ -8,5 +8,8 @@ public class TreeNode {
int val;
TreeNode left;
TreeNode right;
- TreeNode(int x) { val = x; }
+
+ TreeNode(int x) {
+ val = x;
+ }
}
diff --git a/[0126][Word Ladder II]/src/Main.java b/[0126][Word Ladder II]/src/Main.java
index 356b93f..87f620f 100644
--- a/[0126][Word Ladder II]/src/Main.java
+++ b/[0126][Word Ladder II]/src/Main.java
@@ -1,7 +1,5 @@
-import org.junit.Assert;
import org.junit.Test;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
@@ -25,7 +23,7 @@ public void test2() {
Solution solution = new Solution();
String beginWord = "hot";
String endWord = "dog";
- List wordList = new LinkedList<>(Arrays.asList("hot","dog","dot"));
+ List wordList = new LinkedList<>(Arrays.asList("hot", "dog", "dot"));
System.out.println(solution.findLadders(beginWord, endWord, wordList));
}
@@ -34,7 +32,7 @@ public void test3() {
Solution solution = new Solution();
String beginWord = "cet";
String endWord = "ism";
- String[] words = {"kid","tag","pup","ail","tun","woo","erg","luz","brr","gay","sip","kay","per","val","mes","ohs","now","boa","cet","pal","bar","die","war","hay","eco","pub","lob","rue","fry","lit","rex","jan","cot","bid","ali","pay","col","gum","ger","row","won","dan","rum","fad","tut","sag","yip","sui","ark","has","zip","fez","own","ump","dis","ads","max","jaw","out","btu","ana","gap","cry","led","abe","box","ore","pig","fie","toy","fat","cal","lie","noh","sew","ono","tam","flu","mgm","ply","awe","pry","tit","tie","yet","too","tax","jim","san","pan","map","ski","ova","wed","non","wac","nut","why","bye","lye","oct","old","fin","feb","chi","sap","owl","log","tod","dot","bow","fob","for","joe","ivy","fan","age","fax","hip","jib","mel","hus","sob","ifs","tab","ara","dab","jag","jar","arm","lot","tom","sax","tex","yum","pei","wen","wry","ire","irk","far","mew","wit","doe","gas","rte","ian","pot","ask","wag","hag","amy","nag","ron","soy","gin","don","tug","fay","vic","boo","nam","ave","buy","sop","but","orb","fen","paw","his","sub","bob","yea","oft","inn","rod","yam","pew","web","hod","hun","gyp","wei","wis","rob","gad","pie","mon","dog","bib","rub","ere","dig","era","cat","fox","bee","mod","day","apr","vie","nev","jam","pam","new","aye","ani","and","ibm","yap","can","pyx","tar","kin","fog","hum","pip","cup","dye","lyx","jog","nun","par","wan","fey","bus","oak","bad","ats","set","qom","vat","eat","pus","rev","axe","ion","six","ila","lao","mom","mas","pro","few","opt","poe","art","ash","oar","cap","lop","may","shy","rid","bat","sum","rim","fee","bmw","sky","maj","hue","thy","ava","rap","den","fla","auk","cox","ibo","hey","saw","vim","sec","ltd","you","its","tat","dew","eva","tog","ram","let","see","zit","maw","nix","ate","gig","rep","owe","ind","hog","eve","sam","zoo","any","dow","cod","bed","vet","ham","sis","hex","via","fir","nod","mao","aug","mum","hoe","bah","hal","keg","hew","zed","tow","gog","ass","dem","who","bet","gos","son","ear","spy","kit","boy","due","sen","oaf","mix","hep","fur","ada","bin","nil","mia","ewe","hit","fix","sad","rib","eye","hop","haw","wax","mid","tad","ken","wad","rye","pap","bog","gut","ito","woe","our","ado","sin","mad","ray","hon","roy","dip","hen","iva","lug","asp","hui","yak","bay","poi","yep","bun","try","lad","elm","nat","wyo","gym","dug","toe","dee","wig","sly","rip","geo","cog","pas","zen","odd","nan","lay","pod","fit","hem","joy","bum","rio","yon","dec","leg","put","sue","dim","pet","yaw","nub","bit","bur","sid","sun","oil","red","doc","moe","caw","eel","dix","cub","end","gem","off","yew","hug","pop","tub","sgt","lid","pun","ton","sol","din","yup","jab","pea","bug","gag","mil","jig","hub","low","did","tin","get","gte","sox","lei","mig","fig","lon","use","ban","flo","nov","jut","bag","mir","sty","lap","two","ins","con","ant","net","tux","ode","stu","mug","cad","nap","gun","fop","tot","sow","sal","sic","ted","wot","del","imp","cob","way","ann","tan","mci","job","wet","ism","err","him","all","pad","hah","hie","aim","ike","jed","ego","mac","baa","min","com","ill","was","cab","ago","ina","big","ilk","gal","tap","duh","ola","ran","lab","top","gob","hot","ora","tia","kip","han","met","hut","she","sac","fed","goo","tee","ell","not","act","gil","rut","ala","ape","rig","cid","god","duo","lin","aid","gel","awl","lag","elf","liz","ref","aha","fib","oho","tho","her","nor","ace","adz","fun","ned","coo","win","tao","coy","van","man","pit","guy","foe","hid","mai","sup","jay","hob","mow","jot","are","pol","arc","lax","aft","alb","len","air","pug","pox","vow","got","meg","zoe","amp","ale","bud","gee","pin","dun","pat","ten","mob"};
+ String[] words = {"kid", "tag", "pup", "ail", "tun", "woo", "erg", "luz", "brr", "gay", "sip", "kay", "per", "val", "mes", "ohs", "now", "boa", "cet", "pal", "bar", "die", "war", "hay", "eco", "pub", "lob", "rue", "fry", "lit", "rex", "jan", "cot", "bid", "ali", "pay", "col", "gum", "ger", "row", "won", "dan", "rum", "fad", "tut", "sag", "yip", "sui", "ark", "has", "zip", "fez", "own", "ump", "dis", "ads", "max", "jaw", "out", "btu", "ana", "gap", "cry", "led", "abe", "box", "ore", "pig", "fie", "toy", "fat", "cal", "lie", "noh", "sew", "ono", "tam", "flu", "mgm", "ply", "awe", "pry", "tit", "tie", "yet", "too", "tax", "jim", "san", "pan", "map", "ski", "ova", "wed", "non", "wac", "nut", "why", "bye", "lye", "oct", "old", "fin", "feb", "chi", "sap", "owl", "log", "tod", "dot", "bow", "fob", "for", "joe", "ivy", "fan", "age", "fax", "hip", "jib", "mel", "hus", "sob", "ifs", "tab", "ara", "dab", "jag", "jar", "arm", "lot", "tom", "sax", "tex", "yum", "pei", "wen", "wry", "ire", "irk", "far", "mew", "wit", "doe", "gas", "rte", "ian", "pot", "ask", "wag", "hag", "amy", "nag", "ron", "soy", "gin", "don", "tug", "fay", "vic", "boo", "nam", "ave", "buy", "sop", "but", "orb", "fen", "paw", "his", "sub", "bob", "yea", "oft", "inn", "rod", "yam", "pew", "web", "hod", "hun", "gyp", "wei", "wis", "rob", "gad", "pie", "mon", "dog", "bib", "rub", "ere", "dig", "era", "cat", "fox", "bee", "mod", "day", "apr", "vie", "nev", "jam", "pam", "new", "aye", "ani", "and", "ibm", "yap", "can", "pyx", "tar", "kin", "fog", "hum", "pip", "cup", "dye", "lyx", "jog", "nun", "par", "wan", "fey", "bus", "oak", "bad", "ats", "set", "qom", "vat", "eat", "pus", "rev", "axe", "ion", "six", "ila", "lao", "mom", "mas", "pro", "few", "opt", "poe", "art", "ash", "oar", "cap", "lop", "may", "shy", "rid", "bat", "sum", "rim", "fee", "bmw", "sky", "maj", "hue", "thy", "ava", "rap", "den", "fla", "auk", "cox", "ibo", "hey", "saw", "vim", "sec", "ltd", "you", "its", "tat", "dew", "eva", "tog", "ram", "let", "see", "zit", "maw", "nix", "ate", "gig", "rep", "owe", "ind", "hog", "eve", "sam", "zoo", "any", "dow", "cod", "bed", "vet", "ham", "sis", "hex", "via", "fir", "nod", "mao", "aug", "mum", "hoe", "bah", "hal", "keg", "hew", "zed", "tow", "gog", "ass", "dem", "who", "bet", "gos", "son", "ear", "spy", "kit", "boy", "due", "sen", "oaf", "mix", "hep", "fur", "ada", "bin", "nil", "mia", "ewe", "hit", "fix", "sad", "rib", "eye", "hop", "haw", "wax", "mid", "tad", "ken", "wad", "rye", "pap", "bog", "gut", "ito", "woe", "our", "ado", "sin", "mad", "ray", "hon", "roy", "dip", "hen", "iva", "lug", "asp", "hui", "yak", "bay", "poi", "yep", "bun", "try", "lad", "elm", "nat", "wyo", "gym", "dug", "toe", "dee", "wig", "sly", "rip", "geo", "cog", "pas", "zen", "odd", "nan", "lay", "pod", "fit", "hem", "joy", "bum", "rio", "yon", "dec", "leg", "put", "sue", "dim", "pet", "yaw", "nub", "bit", "bur", "sid", "sun", "oil", "red", "doc", "moe", "caw", "eel", "dix", "cub", "end", "gem", "off", "yew", "hug", "pop", "tub", "sgt", "lid", "pun", "ton", "sol", "din", "yup", "jab", "pea", "bug", "gag", "mil", "jig", "hub", "low", "did", "tin", "get", "gte", "sox", "lei", "mig", "fig", "lon", "use", "ban", "flo", "nov", "jut", "bag", "mir", "sty", "lap", "two", "ins", "con", "ant", "net", "tux", "ode", "stu", "mug", "cad", "nap", "gun", "fop", "tot", "sow", "sal", "sic", "ted", "wot", "del", "imp", "cob", "way", "ann", "tan", "mci", "job", "wet", "ism", "err", "him", "all", "pad", "hah", "hie", "aim", "ike", "jed", "ego", "mac", "baa", "min", "com", "ill", "was", "cab", "ago", "ina", "big", "ilk", "gal", "tap", "duh", "ola", "ran", "lab", "top", "gob", "hot", "ora", "tia", "kip", "han", "met", "hut", "she", "sac", "fed", "goo", "tee", "ell", "not", "act", "gil", "rut", "ala", "ape", "rig", "cid", "god", "duo", "lin", "aid", "gel", "awl", "lag", "elf", "liz", "ref", "aha", "fib", "oho", "tho", "her", "nor", "ace", "adz", "fun", "ned", "coo", "win", "tao", "coy", "van", "man", "pit", "guy", "foe", "hid", "mai", "sup", "jay", "hob", "mow", "jot", "are", "pol", "arc", "lax", "aft", "alb", "len", "air", "pug", "pox", "vow", "got", "meg", "zoe", "amp", "ale", "bud", "gee", "pin", "dun", "pat", "ten", "mob"};
// List wordList = new ArrayList<>();
// wordList.addAll(Arrays.asList(words));
System.out.println(solution.findLadders(beginWord, endWord, Arrays.asList(words)));
diff --git a/[0126][Word Ladder II]/src/Solution.java b/[0126][Word Ladder II]/src/Solution.java
index bc6ce38..14edd09 100644
--- a/[0126][Word Ladder II]/src/Solution.java
+++ b/[0126][Word Ladder II]/src/Solution.java
@@ -1,10 +1,4 @@
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Queue;
-import java.util.Set;
+import java.util.*;
/**
* @author: wangjunchao(王俊超)
diff --git a/[0126][Word Ladder II]/src/Solution2.java b/[0126][Word Ladder II]/src/Solution2.java
index 7315d8d..206bddb 100644
--- a/[0126][Word Ladder II]/src/Solution2.java
+++ b/[0126][Word Ladder II]/src/Solution2.java
@@ -1,8 +1,4 @@
-import java.util.Deque;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
/**
* @author: wangjunchao(王俊超)
@@ -58,7 +54,7 @@ public List> findLadders(String beginWord, String endWord, List> result = new LinkedList<>();
Deque current = new LinkedList<>();
findLadders(beginWord, endWord, wordDict, current, result);
- for(List r: result) {
+ for (List r : result) {
r.add(0, beginWord);
}
return result;
@@ -95,7 +91,7 @@ else if (result.get(0).size() > current.size()) {
chars[i] = ch;
String s = new String(chars);
- if (wordDict.contains(s) ) {
+ if (wordDict.contains(s)) {
wordDict.remove(s);
current.add(s);
diff --git a/[0126][Word Ladder II]/src/Solution3.java b/[0126][Word Ladder II]/src/Solution3.java
index 906bc48..ddb5639 100644
--- a/[0126][Word Ladder II]/src/Solution3.java
+++ b/[0126][Word Ladder II]/src/Solution3.java
@@ -1,8 +1,4 @@
-import java.util.Deque;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
/**
* @author: wangjunchao(王俊超)
diff --git a/[0127][Word Ladder]/src/Solution.java b/[0127][Word Ladder]/src/Solution.java
index 5366850..4cb9ab8 100644
--- a/[0127][Word Ladder]/src/Solution.java
+++ b/[0127][Word Ladder]/src/Solution.java
@@ -1,8 +1,4 @@
-import java.util.Deque;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
/**
* Author:
diff --git a/[0130][Surrounded Regions]/src/Solution.java b/[0130][Surrounded Regions]/src/Solution.java
index 636f0a0..4f7cf47 100644
--- a/[0130][Surrounded Regions]/src/Solution.java
+++ b/[0130][Surrounded Regions]/src/Solution.java
@@ -50,7 +50,6 @@ void solve(char[][] board) {
}
-
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[i].length; ++j) {
if (board[i][j] == 'O') {
diff --git a/[0131][Palindrome Partitioning]/src/Main.java b/[0131][Palindrome Partitioning]/src/Main.java
index 8266432..d295707 100644
--- a/[0131][Palindrome Partitioning]/src/Main.java
+++ b/[0131][Palindrome Partitioning]/src/Main.java
@@ -1,7 +1,5 @@
import org.junit.Test;
-import java.util.Arrays;
-
/**
* @author: wangjunchao(王俊超)
* @time: 2019-06-21 18:36
diff --git a/[0136][Single Number]/src/Solution.java b/[0136][Single Number]/src/Solution.java
index 1ec5d37..b32b8a7 100644
--- a/[0136][Single Number]/src/Solution.java
+++ b/[0136][Single Number]/src/Solution.java
@@ -8,13 +8,13 @@ public class Solution {
/**
* Given an array of integers, every element appears twice except for one.
* Find that single one.
- *
+ *
* Note: Your algorithm should have a linear runtime complexity.
* Could you implement it without using extra memory?
- *
+ *
* һ飬ÿԪض2γеһҳֻһε
* ע⣺㷨ʱ临ӶȣԲʹöռʵ
- *
+ *
* ˼·ʹ
*
* @param nums
@@ -27,7 +27,7 @@ public int singleNumber(int[] nums) {
}
- for (int i = 1; i< nums.length; i++) {
+ for (int i = 1; i < nums.length; i++) {
nums[0] ^= nums[i];
}
return nums[0];
diff --git a/[0138][Copy List With Random Pointer]/src/Main.java b/[0138][Copy List With Random Pointer]/src/Main.java
index 85972fb..92f5099 100644
--- a/[0138][Copy List With Random Pointer]/src/Main.java
+++ b/[0138][Copy List With Random Pointer]/src/Main.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-06-24
* Time: 09:18
* Declaration: All Rights Reserved !!!
@@ -21,7 +21,7 @@ public static void main(String[] args) {
}
- public static void print(RandomListNode head) {
+ public static void print(RandomListNode head) {
while (head != null) {
System.out.print(head.label + "->");
head = head.next;
diff --git a/[0138][Copy List With Random Pointer]/src/Solution.java b/[0138][Copy List With Random Pointer]/src/Solution.java
index 5501ab5..f392526 100644
--- a/[0138][Copy List With Random Pointer]/src/Solution.java
+++ b/[0138][Copy List With Random Pointer]/src/Solution.java
@@ -87,7 +87,7 @@ public RandomListNode splitList(RandomListNode head) {
// ǰƵĽ
RandomListNode copy;
- while (node != null){
+ while (node != null) {
// ָƽ
copy = node.next;
diff --git a/[0141][Linked List Cycle]/src/ListNode.java b/[0141][Linked List Cycle]/src/ListNode.java
index a9b5f6a..8892796 100644
--- a/[0141][Linked List Cycle]/src/ListNode.java
+++ b/[0141][Linked List Cycle]/src/ListNode.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-08-21
* Time: 19:17
* Declaration: All Rights Reserved !!!
@@ -7,6 +7,7 @@
public class ListNode {
int val;
ListNode next;
+
ListNode(int x) {
val = x;
next = null;
diff --git a/[0142][Linked List Cycle II]/src/ListNode.java b/[0142][Linked List Cycle II]/src/ListNode.java
index fba59f9..c9da456 100644
--- a/[0142][Linked List Cycle II]/src/ListNode.java
+++ b/[0142][Linked List Cycle II]/src/ListNode.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-08-21
* Time: 19:19
* Declaration: All Rights Reserved !!!
@@ -7,6 +7,7 @@
public class ListNode {
int val;
ListNode next;
+
ListNode(int x) {
val = x;
next = null;
diff --git a/[0143][Reorder List]/src/ListNode.java b/[0143][Reorder List]/src/ListNode.java
index 1f6cb21..dc97875 100644
--- a/[0143][Reorder List]/src/ListNode.java
+++ b/[0143][Reorder List]/src/ListNode.java
@@ -5,7 +5,7 @@
* Declaration: All Rights Reserved !!!
*/
public class ListNode {
- int val;
+ int val;
ListNode next;
ListNode(int x) {
diff --git a/[0143][Reorder List]/src/Solution.java b/[0143][Reorder List]/src/Solution.java
index 8c489cf..c4f61a9 100644
--- a/[0143][Reorder List]/src/Solution.java
+++ b/[0143][Reorder List]/src/Solution.java
@@ -24,6 +24,7 @@ public class Solution {
* 2㣬ͽм֮в֣
*
*
+ *
* @param head
*/
public void reorderList(ListNode head) {
diff --git a/[0150][Evaluate Reverse Polish Notation]/src/Main.java b/[0150][Evaluate Reverse Polish Notation]/src/Main.java
index f6672ab..ce7f89f 100644
--- a/[0150][Evaluate Reverse Polish Notation]/src/Main.java
+++ b/[0150][Evaluate Reverse Polish Notation]/src/Main.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-06-20
* Time: 07:14
* Declaration: All Rights Reserved !!!
@@ -7,6 +7,6 @@
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
- System.out.println(solution.evalRPN(new String[]{"0","3","/"}));
+ System.out.println(solution.evalRPN(new String[]{"0", "3", "/"}));
}
}
diff --git a/[0150][Evaluate Reverse Polish Notation]/src/Solution.java b/[0150][Evaluate Reverse Polish Notation]/src/Solution.java
index 499adc9..17993a7 100644
--- a/[0150][Evaluate Reverse Polish Notation]/src/Solution.java
+++ b/[0150][Evaluate Reverse Polish Notation]/src/Solution.java
@@ -1,4 +1,3 @@
-import java.util.Queue;
import java.util.Stack;
/**
diff --git a/[0160][Intersection of Two Linked Lists]/[0160][Intersection of Two Linked Lists].iml b/[0160][Intersection of Two Linked Lists]/[0160][Intersection of Two Linked Lists].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0160][Intersection of Two Linked Lists]/[0160][Intersection of Two Linked Lists].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0160][Intersection of Two Linked Lists]/src/ListNode.java b/[0160][Intersection of Two Linked Lists]/src/ListNode.java
new file mode 100644
index 0000000..e49628e
--- /dev/null
+++ b/[0160][Intersection of Two Linked Lists]/src/ListNode.java
@@ -0,0 +1,9 @@
+public class ListNode {
+ int val;
+ ListNode next;
+
+ ListNode(int x) {
+ val = x;
+ next = null;
+ }
+}
diff --git a/[0160][Intersection of Two Linked Lists]/src/Solution.java b/[0160][Intersection of Two Linked Lists]/src/Solution.java
new file mode 100644
index 0000000..72f8e1a
--- /dev/null
+++ b/[0160][Intersection of Two Linked Lists]/src/Solution.java
@@ -0,0 +1,130 @@
+//Write a program to find the node at which the intersection of two singly linke
+//d lists begins.
+//
+// For example, the following two linked lists:
+//
+//
+// begin to intersect at node c1.
+//
+//
+//
+// Example 1:
+//
+//
+//
+//Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2
+//, skipB = 3
+//Output: Reference of the node with value = 8
+//Input Explanation: The intersected node's value is 8 (note that this must not
+//be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. F
+//rom the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the inter
+//sected node in A; There are 3 nodes before the intersected node in B.
+//
+//
+//
+// Example 2:
+//
+//
+//
+//Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skip
+//B = 1
+//Output: Reference of the node with value = 2
+//Input Explanation: The intersected node's value is 2 (note that this must not
+//be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. F
+//rom the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected
+// node in A; There are 1 node before the intersected node in B.
+//
+//
+//
+//
+// Example 3:
+//
+//
+//
+//Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
+//Output: null
+//Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B
+//, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be
+//0, while skipA and skipB can be arbitrary values.
+//Explanation: The two lists do not intersect, so return null.
+//
+//
+//
+//
+// Notes:
+//
+//
+// If the two linked lists have no intersection at all, return null.
+// The linked lists must retain their original structure after the function returns.
+// You may assume there are no cycles anywhere in the entire linked structure.
+// Your code should preferably run in O(n) time and use only O(1) memory.
+//
+// Related Topics Linked List
+
+
+//leetcode submit region begin(Prohibit modification and deletion)
+
+/**
+ * Definition for singly-linked list.
+ * public class ListNode {
+ * int val;
+ * ListNode next;
+ * ListNode(int x) {
+ * val = x;
+ * next = null;
+ * }
+ * }
+ */
+public class Solution {
+ /**
+ * 解题思路:将headA链表的尾结点链接到headB的头部,问题等价于求链表中环的入口节点,最后再还原两个表
+ */
+ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
+
+ if (headA == null || headB == null) {
+ return null;
+ }
+
+ if (headA == headB) {
+ return headA;
+ }
+
+ ListNode tailA = headA;
+ while (tailA.next != null) {
+ tailA = tailA.next;
+ }
+
+ tailA.next = headB;
+
+ // 定义快慢指针
+ ListNode fast = headA;
+ ListNode slow = headA;
+
+ while (fast.next != null && fast.next.next != null) {
+ slow = slow.next;
+ fast = fast.next.next;
+
+ //如果有环,想遇于环中某点
+ if (fast == slow) {
+ break;
+ }
+ }
+
+ //如果没有环,return null
+ if (fast.next == null || fast.next.next == null) {
+ tailA.next = null; // 还原链表
+ return null;
+ }
+
+ slow = headA;
+ while (slow != fast) {
+ slow = slow.next;
+ fast = fast.next;
+ }
+
+
+ tailA.next = null; // 还原链表
+ return fast;
+ }
+}
+//leetcode submit region end(Prohibit modification and deletion)
diff --git a/[0167][Two Sum II - Input array is sorted]/src/Solution.java b/[0167][Two Sum II - Input array is sorted]/src/Solution.java
index 74f81cc..fd34284 100644
--- a/[0167][Two Sum II - Input array is sorted]/src/Solution.java
+++ b/[0167][Two Sum II - Input array is sorted]/src/Solution.java
@@ -5,6 +5,7 @@
public class Solution {
/**
* 如果数组很大,target很小,可以考虑先用二分查找法找到不大于target元素的下标,再作处理
+ *
* @param numbers
* @param target
* @return
diff --git a/[0173][Binary Search Tree Iterator]/src/BSTIterator.java b/[0173][Binary Search Tree Iterator]/src/BSTIterator.java
index a1dddf3..8465ef7 100644
--- a/[0173][Binary Search Tree Iterator]/src/BSTIterator.java
+++ b/[0173][Binary Search Tree Iterator]/src/BSTIterator.java
@@ -7,8 +7,8 @@
* @time: 2019-06-28 16:54
**/
public class BSTIterator {
- private TreeNode root;
- private TreeNode prev = null;
+ private TreeNode root;
+ private TreeNode prev = null;
// 最后一个元素表示next元素的值
private Deque deque = new LinkedList<>();
@@ -33,9 +33,9 @@ public int next() {
TreeNode temp = deque.removeLast();
- if (temp.right!= null) {
+ if (temp.right != null) {
TreeNode n = temp.right;
- while (n!=null) {
+ while (n != null) {
deque.addLast(n);
n = n.left;
}
diff --git a/[0173][Binary Search Tree Iterator]/src/Solution.java b/[0173][Binary Search Tree Iterator]/src/Solution.java
index f65bfd9..6fb2141 100644
--- a/[0173][Binary Search Tree Iterator]/src/Solution.java
+++ b/[0173][Binary Search Tree Iterator]/src/Solution.java
@@ -8,40 +8,40 @@
public class Solution {
@Test
public void test1() {
- TreeNode root = new TreeNode(7);
+ TreeNode root = new TreeNode(7);
root.left = new TreeNode(3);
root.right = new TreeNode(15);
root.right.left = new TreeNode(9);
root.right.right = new TreeNode(20);
BSTIterator iterator = new BSTIterator(root);
- Assert.assertEquals(3, iterator.next() ); // return 3
- Assert.assertEquals(7, iterator.next() ); // return 7
+ Assert.assertEquals(3, iterator.next()); // return 3
+ Assert.assertEquals(7, iterator.next()); // return 7
Assert.assertEquals(true, iterator.hasNext()); // return true
- Assert.assertEquals(9, iterator.next() ); // return 9
+ Assert.assertEquals(9, iterator.next()); // return 9
Assert.assertEquals(true, iterator.hasNext()); // return true
- Assert.assertEquals(15, iterator.next() ); // return 15
+ Assert.assertEquals(15, iterator.next()); // return 15
Assert.assertEquals(true, iterator.hasNext()); // return true
- Assert.assertEquals(20, iterator.next() ); // return 20
+ Assert.assertEquals(20, iterator.next()); // return 20
Assert.assertEquals(false, iterator.hasNext()); // return false
}
@Test
public void test2() {
- TreeNode root = new TreeNode(7);
+ TreeNode root = new TreeNode(7);
BSTIterator iterator = new BSTIterator(root);
- Assert.assertEquals(7, iterator.next() ); // return 3
+ Assert.assertEquals(7, iterator.next()); // return 3
Assert.assertEquals(false, iterator.hasNext()); // return false
Assert.assertEquals(false, iterator.hasNext()); // return false
}
@Test
public void test3() {
- TreeNode root = new TreeNode(1);
+ TreeNode root = new TreeNode(1);
root.right = new TreeNode(2);
BSTIterator iterator = new BSTIterator(root);
- Assert.assertEquals(true, iterator.hasNext() ); // return 3
- Assert.assertEquals(1, iterator.next() ); // return 3
+ Assert.assertEquals(true, iterator.hasNext()); // return 3
+ Assert.assertEquals(1, iterator.next()); // return 3
Assert.assertEquals(true, iterator.hasNext()); // return false
Assert.assertEquals(2, iterator.next()); // return false
Assert.assertEquals(false, iterator.hasNext()); // return false
diff --git a/[0191][Number Of 1 Bits]/src/Solution.java b/[0191][Number Of 1 Bits]/src/Solution.java
index 5b782f6..3e63a99 100644
--- a/[0191][Number Of 1 Bits]/src/Solution.java
+++ b/[0191][Number Of 1 Bits]/src/Solution.java
@@ -1,18 +1,18 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-06-17
* Time: 21:51
* Declaration: All Rights Reserved !!!
*/
public class Solution {
/**
- * Write a function that takes an unsigned integer and returns the number of 1' bits it has
+ * Write a function that takes an unsigned integer and returns the number of ��1' bits it has
* (also known as the Hamming weight).
- *
- * For example, the 32-bit integer 11' has binary representation 00000000000000000000000000001011,
+ *
+ * For example, the 32-bit integer ��11' has binary representation 00000000000000000000000000001011,
* so the function should return 3.
- *
- * еλ
+ *
+ * ���������е�λ��
*
* @param n
* @return
diff --git a/[0200][Number Of Islands]/src/Solution.java b/[0200][Number Of Islands]/src/Solution.java
index 47991f4..ff8698a 100644
--- a/[0200][Number Of Islands]/src/Solution.java
+++ b/[0200][Number Of Islands]/src/Solution.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-06-20
* Time: 11:00
* Declaration: All Rights Reserved !!!
@@ -25,12 +25,12 @@ public class Solution {
* 00011
* Answer: 3
*
- * Ŀ⣺
- * һάĵͼ'1'½أ0ˮ滷ˮ
- * ڵ½ˮƽֱӶγɵġԼĸ߶ˮΧ
+ * ��Ŀ���⣺
+ * ������һ����ά����ĵ�ͼ��'1'��½�أ���0��ˮ�����������������������������滷ˮ��
+ * �������ڵ�½��ˮƽ��ֱ���Ӷ��γɵġ�����Լ��������������ĸ��߶���ˮ��Χ��
*
- * ˼·
- * һһӦķʱǾÿԪʱйȱȱͳƵĿ
+ * ����˼·��
+ * һ��������һ����Ӧ�ķ��ʱ�Ǿ�������ÿ��Ԫ��ʱ�й�����ȱ���������������ȱ�������ͳ�Ƶ�����Ŀ
*
*
*
@@ -38,18 +38,18 @@ public class Solution {
* @return
*/
public int numIslands(char[][] grid) {
- // У
+ // ������
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
- // ԪĬֵfalse
+ // Ԫ��Ĭ��ֵ��false
boolean[][] visited = new boolean[grid.length][grid[0].length];
int result = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
- // λûбʹҴλǵ¹ȱ
+ // �����λ��û�б����ʹ������Ҵ�λ���ǵ�������¹�����ȱ���
if (!visited[i][j] && grid[i][j] == '1') {
result++;
bfs(grid, visited, i, j);
@@ -60,29 +60,30 @@ public int numIslands(char[][] grid) {
}
/**
- *
- * @param grid
- * @param visited ʱǾ
- * @param row
- * @param col
+ * �����������
+ *
+ * @param grid ����
+ * @param visited ���ʱ�Ǿ���
+ * @param row ������
+ * @param col ������
*/
private void bfs(char[][] grid, boolean[][] visited, int row, int col) {
- if (row >= 0 && row < grid.length // кϷ
- && col >= 0 && col < grid[0].length // кϷ
- && !visited[row][col] // ûзʹ
- && grid[row][col] == '1') { // ǵ½
+ if (row >= 0 && row < grid.length // �кϷ�
+ && col >= 0 && col < grid[0].length // �кϷ�
+ && !visited[row][col] // û�з��ʹ�
+ && grid[row][col] == '1') { // �ǵ���½��
- // ǴλѾʹ
+ // ��Ǵ�λ���Ѿ����ʹ���
visited[row][col] = true;
- //
+ // ��
bfs(grid, visited, row - 1, col);
- //
+ // ��
bfs(grid, visited, row, col + 1);
- //
+ // ��
bfs(grid, visited, row + 1, col);
- //
+ // ��
bfs(grid, visited, row, col - 1);
}
}
diff --git a/[0202][Happy Number ]/src/Solution.java b/[0202][Happy Number ]/src/Solution.java
index 5df793f..46f562d 100644
--- a/[0202][Happy Number ]/src/Solution.java
+++ b/[0202][Happy Number ]/src/Solution.java
@@ -1,7 +1,7 @@
import java.util.HashSet;
/**
- * Author:
+ * Author: ������
* Date: 2015-06-17
* Time: 21:36
* Declaration: All Rights Reserved !!!
@@ -13,9 +13,9 @@ public class Solution {
* replace the number by the sum of the squares of its digits, and repeat the process until the number
* equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those
* numbers for which this process ends in 1 are happy numbers.
- *
- * ĿҪһϸλֵƽͣΪ1Ϊhappy number
- * ܴijʼѭ
+ *
+ * ��ĿҪ�������һ�������������ϸ�����λ�����ֵ�ƽ���ͣ�����������Ϊ1���������Ϊhappy number��
+ * ���������ܴ�ij������ʼ����ѭ����
*
* @param n
* @return
@@ -26,13 +26,13 @@ public boolean isHappy(int n) {
return false;
}
- // ڱмֵĽ
+ // ���ڱ����м���ֵĽ��
HashSet set = new HashSet<>(32);
int tmp;
int newN;
- // nΪ1nֵظ֣ѭ
+ // n��Ϊ1������n��ֵ�����ظ����֣��������ѭ��
while (n != 1 && !set.contains(n)) {
set.add(n);
newN = 0;
diff --git a/[0206][Reverse Linked List]/src/Solution.java b/[0206][Reverse Linked List]/src/Solution.java
index 3799055..360bebd 100644
--- a/[0206][Reverse Linked List]/src/Solution.java
+++ b/[0206][Reverse Linked List]/src/Solution.java
@@ -32,6 +32,7 @@ public ListNode reverseList(ListNode head) {
/**
* TODO ʹõݹⷨ
+ *
* @param head
* @return
*/
diff --git a/[0207][Course Schedule]/src/Main.java b/[0207][Course Schedule]/src/Main.java
index ac84c6d..a94abab 100644
--- a/[0207][Course Schedule]/src/Main.java
+++ b/[0207][Course Schedule]/src/Main.java
@@ -39,7 +39,7 @@ public void test4() {
@Test
public void test5() {
Solution solution = new Solution();
- int[][] arr = {{1,0},{2,6},{1,7},{6,4},{7,0},{0,5}};
+ int[][] arr = {{1, 0}, {2, 6}, {1, 7}, {6, 4}, {7, 0}, {0, 5}};
Assert.assertEquals(true, solution.canFinish(8, arr));
}
}
diff --git a/[0207][Course Schedule]/src/Solution.java b/[0207][Course Schedule]/src/Solution.java
index a929c91..208c49e 100644
--- a/[0207][Course Schedule]/src/Solution.java
+++ b/[0207][Course Schedule]/src/Solution.java
@@ -1,8 +1,4 @@
-import java.util.Deque;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
/**
* @author: wangjunchao(王俊超)
@@ -93,7 +89,7 @@ private boolean dfs(int course, Map> dependencies, boolea
path.addLast(course);
- for(int i: dependency) {
+ for (int i : dependency) {
boolean result = dfs(i, dependencies, visit, path);
if (!result) {
return false;
diff --git a/[0215][Kth Largest Element In An Array]/src/Main.java b/[0215][Kth Largest Element In An Array]/src/Main.java
index cd77742..ec1a203 100644
--- a/[0215][Kth Largest Element In An Array]/src/Main.java
+++ b/[0215][Kth Largest Element In An Array]/src/Main.java
@@ -1,5 +1,5 @@
/**
- * Author:
+ * Author: ������
* Date: 2015-06-19
* Time: 20:01
* Declaration: All Rights Reserved !!!
@@ -7,6 +7,6 @@
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
- System.out.println(solution.findKthLargest(new int[]{2 , 1}, 2));
+ System.out.println(solution.findKthLargest(new int[]{2, 1}, 2));
}
}
diff --git a/[0215][Kth Largest Element In An Array]/src/Solution.java b/[0215][Kth Largest Element In An Array]/src/Solution.java
index 8119edf..7648127 100644
--- a/[0215][Kth Largest Element In An Array]/src/Solution.java
+++ b/[0215][Kth Largest Element In An Array]/src/Solution.java
@@ -1,7 +1,5 @@
-import java.util.Collections;
-
/**
- * Author:
+ * Author: ������
* Date: 2015-06-19
* Time: 19:36
* Declaration: All Rights Reserved !!!
@@ -16,14 +14,14 @@ public class Solution {
* Given [3,2,1,5,6,4] and k = 2, return 5.
*
* Note:
- * You may assume k is always valid, 1 k array's length.
+ * You may assume k is always valid, 1 �� k �� array's length.
*
- * Ŀ⣺
- * һδҳkԪءע֮ĵkǵkظԪ
- * ԼkһЧģ 1 k 鳤
+ * ��Ŀ���⣺
+ * ��һ��δ��������������ҳ���k���Ԫ�ء�ע��������֮��ĵ�k���ǵ�k�����ظ���Ԫ��
+ * ���Լ���kһ������Ч�ģ� 1 �� k �� ���鳤��
*
- * ˼·
- * O(n)ⷨѡQuickSelect㷨
+ * ����˼·��
+ * O(n)�ⷨ������ѡ��QuickSelect���㷨
*
*
* @param nums
@@ -41,19 +39,19 @@ public int findKthLargest(int[] nums, int k) {
public int findKthLargest(int[] nums, int start, int end, int k) {
- // ֵ
+ // ����ֵ
int pivot = nums[start];
int lo = start;
int hi = end;
while (lo < hi) {
- // Сֵƶ
+ // ��С������ֵ�����ƶ����������
while (lo < hi && nums[hi] >= pivot) {
hi--;
}
nums[lo] = nums[hi];
- // ֵƶұ
+ // ����������ֵ�����ƶ��������ұ�
while (lo < hi && nums[lo] <= pivot) {
lo++;
}
@@ -62,19 +60,19 @@ public int findKthLargest(int[] nums, int start, int end, int k) {
nums[lo] = pivot;
- // Ѿҵ
+ // ����Ѿ��ҵ���
if (end - lo + 1 == k) {
return pivot;
}
- // kloλõұ
- else if (end - lo + 1 > k){
+ // ��k�������loλ�õ��ұ�
+ else if (end - lo + 1 > k) {
return findKthLargest(nums, lo + 1, end, k);
}
- // kloλõ
+ // ��k�������loλ�õ����
else {
// k-(end-lo+1)
- // (end-lo+1)ʾloλÿʼendλõԪظҰ벿
- // ԭĵkk-(end-lo+1)
+ // (end-lo+1)����ʾ��loλ�ÿ�ʼ��endλ�õ�Ԫ�ظ�������������Ұ벿��
+ // ԭ���ĵ�k����k-(end-lo+1)��
return findKthLargest(nums, start, lo - 1, k - (end - lo + 1));
}
}
diff --git a/[0216][Combination Sum III]/src/Solution.java b/[0216][Combination Sum III]/src/Solution.java
index e2c8e6f..563a5f7 100644
--- a/[0216][Combination Sum III]/src/Solution.java
+++ b/[0216][Combination Sum III]/src/Solution.java
@@ -1,9 +1,8 @@
-import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
- * Author:
+ * Author: ������
* Date: 2015-06-21
* Time: 21:31
* Declaration: All Rights Reserved !!!
@@ -27,12 +26,12 @@ public class Solution {
* Output:
* [[1,2,6], [1,3,5], [2,3,4]]
*
- * Ŀ⣺
- * Ѱk֮͵nϣֻʹ1-9ÿһеӦΨһġ
- * ȷеԵ˳С
+ * ��Ŀ���⣺
+ * Ѱ����������k����֮�͵���n����ϣ�ֻ����ʹ������1-9������ÿһ������е�����Ӧ����Ψһ�ġ�
+ * ȷ������е������Ե���˳�����С�
*
- * ˼·
- * ݷ
+ * ����˼·��
+ * ���ݷ�
*
*
* @param k
@@ -40,57 +39,57 @@ public class Solution {
* @return
*/
public List> combinationSum3(int k, int n) {
- // ڱн
+ // ���ڱ������н��
List> result = new LinkedList<>();
- // ڱм
+ // ���ڱ����м���
List list = new LinkedList<>();
- // ͽн
+ // ��������ͽ��н������
if (k > 0 && k <= 9) {
- solve(k, 1, n, 0, list, result);
+ solve(k, 1, n, 0, list, result);
}
- // ؽ
+ // ���ؽ��
return result;
}
/**
- * ⷽ
+ * ��ⷽ��
*
- * @param k ÿԪظ
- * @param cur ǰkԪ
- * @param remainder k - cur + 1Ԫصĺ
- * @param prevVal cur-1Ԫصȡֵ
- * @param list Ԫصļ
- * @param result н
+ * @param k ÿ�����Ԫ�ظ���
+ * @param cur ��ǰ�����k��Ԫ��
+ * @param remainder k - cur + 1��Ԫ�صĺ�
+ * @param prevVal ��cur-1��Ԫ�ص�ȡֵ
+ * @param list �����Ԫ�صļ�����
+ * @param result ��������������
*/
public void solve(int k, int cur, int remainder, int prevVal, List list, List> result) {
- // һԪ
+ // �������һ��Ԫ��
if (cur == k) {
- // remainderΪһԪصֵǰһԪصֵҲܳ9
+ // remainderΪ���һ����Ԫ�ص�ֵ�����������ǰһ����Ԫ�ص�ֵ�����Ҳ��ܳ���9
if (remainder > prevVal && remainder <= 9) {
- // Ԫֵ
+ // ���Ԫ��ֵ
list.add(remainder);
- // µĶ
+ // ����������µĶ�����
List one = new LinkedList<>();
for (Integer i : list) {
one.add(i);
}
- //
+ // ������
result.add(one);
- // ɾһԪأֳԭ
+ // ɾ�����һ��Ԫ�أ������ֳ���ԭ
list.remove(list.size() - 1);
}
}
- // һԪ
- else if (cur < k){
+ // �������һ��Ԫ��
+ else if (cur < k) {
for (int i = prevVal + 1; i <= 9 - (k - cur); i++) {
- // Ԫ
+ // ���Ԫ��
list.add(i);
- // ݹ
+ // �ݹ����
solve(k, cur + 1, remainder - i, i, list, result);
- // ֳԭ
+ // �ֳ���ԭ
list.remove(list.size() - 1);
}
diff --git a/[0217][Contains Duplicate ]/src/Solution.java b/[0217][Contains Duplicate ]/src/Solution.java
index 67fdbb9..d144b96 100644
--- a/[0217][Contains Duplicate ]/src/Solution.java
+++ b/[0217][Contains Duplicate ]/src/Solution.java
@@ -2,7 +2,7 @@
import java.util.Set;
/**
- * Author:
+ * Author: ������
* Date: 2015-06-18
* Time: 09:40
* Declaration: All Rights Reserved !!!
@@ -15,26 +15,26 @@ public class Solution {
* Your function should return true if any value appears at least twice in the
* array, and it should return false if every element is distinct.
*
- * Ŀ⣺һ飬жǷظԪءһֳΣ
- * ĺӦ÷trueÿһԪضΨһģfalse
- *
- * ˼·setݽṹ
+ * ��Ŀ���⣺����һ���������飬�ж��������Ƿ�����ظ�Ԫ�ء��������������һ�����ֳ������������Σ�
+ * ��ĺ���Ӧ�÷���true�����ÿһ��Ԫ�ض���Ψһ�ģ�����false��
+ *
+ * ����˼·����set���ݽṹ
*
* @param nums
* @return
*/
public boolean containsDuplicate(int[] nums) {
- // Ԫظ1ŽIJ
+ // Ԫ�ظ�������1�Ž�������IJ���
if (nums != null && nums.length > 1) {
- //һhashSet
+ //����һ��hashSet
Set set = new HashSet<>(nums.length);
- for(int i : nums) {
- // ԪѾھͷtrue
+ for (int i : nums) {
+ // ���Ԫ���Ѿ����ھͷ���true
if (set.contains(i)) {
return true;
}
- // ûоӵԪؼ
+ // û�о���ӵ�Ԫ�ؼ�����
else {
set.add(i);
}
diff --git a/[0219][Contains Duplicate II]/src/Solution.java b/[0219][Contains Duplicate II]/src/Solution.java
index 2fa0545..3c0f97e 100644
--- a/[0219][Contains Duplicate II]/src/Solution.java
+++ b/[0219][Contains Duplicate II]/src/Solution.java
@@ -2,7 +2,7 @@
import java.util.Map;
/**
- * Author:
+ * Author: ������
* Date: 2015-06-19
* Time: 14:10
* Declaration: All Rights Reserved !!!
@@ -13,22 +13,22 @@ public class Solution {
* Given an array of integers and an integer k, find out whether there there are
* two distinct indices i and j in the array such that nums[i] = nums[j] and the
* difference between i and j is at most k.
- *
- * Ŀ⣺
- * һnumsһkҽͬ±ijnums[i] = nums[j]
- * |i-j|<=kʱtruefalse
- *
- * ˼·
- * nums[0...n-1]һmapУ(muns[i], i)nums[k]Ѿڣ
- * Ƚ֮ǰ±ڵ±IJֵֵk˵ֵ
- * ʹµ±Ϊֵ
+ *
+ * ��Ŀ���⣺
+ * ����һ����������nums��һ������k�����ҽ�������������ͬ���±�i��j����nums[i] = nums[j]
+ * ����|i-j|<=kʱ����true������false��
+ *
+ * ����˼·��
+ * ��nums[0...n-1]������һ��map�У�(muns[i], i)�������nums[k]�Ѿ����ڣ�
+ * ��Ƚ�֮ǰ���±�����ڵ��±�IJ�ֵ�������ֵ������k��˵��������������������ֵ��
+ * ����ʹ���µ��±���Ϊֵ
*
* @param nums
* @param k
* @return
*/
public boolean containsNearbyDuplicate(int[] nums, int k) {
- // ж
+ // ���������ж�
if (nums == null || nums.length < 2 || k < 1) {
return false;
}
@@ -36,13 +36,13 @@ public boolean containsNearbyDuplicate(int[] nums, int k) {
Map map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
- // ûжӦkeyӽȥ
+ // ���û�ж�Ӧ��key��ӽ�ȥ
if (!map.containsKey(nums[i])) {
map.put(nums[i], i);
}
- // ѾжӦkey-value
+ // �Ѿ��ж�Ӧ��key-value��
else {
- // ԭֵӦ±꣬һСڵ±
+ // ԭ�������ֵ��Ӧ���±꣬��һ��С�����ڵ��±�
int value = map.get(nums[i]);
if (i - value <= k) {
return true;
diff --git a/[0223][Rectangle Area]/src/Solution.java b/[0223][Rectangle Area]/src/Solution.java
index f2ea311..b8d05d5 100644
--- a/[0223][Rectangle Area]/src/Solution.java
+++ b/[0223][Rectangle Area]/src/Solution.java
@@ -1,7 +1,5 @@
-import java.util.*;
-
/**
- * Author:
+ * Author: ������
* Date: 2015-06-19
* Time: 15:32
* Declaration: All Rights Reserved !!!
@@ -28,7 +26,7 @@ public class Solution {
* ***************************** *
* (A,B):(-3,0)* *
* * *
- * * (E,F):(0-1) *
+ * * (E,F):(0��-1) *
* *************************************
*
* *************************** ***************************
@@ -43,11 +41,11 @@ public class Solution {
*
*
*
- * Ŀ⣺
- * ܹǵ
+ * ��Ŀ���⣺
+ * �������������ܹ����ǵ����
*
- * ˼·
- * 㷽Ϊ1+2-ιͬĸ
+ * ����˼·��
+ * ���㷽��Ϊ����1�����+����2�����-�������ι�ͬ�ĸ��������
*
* @param A
* @param B
@@ -60,18 +58,18 @@ public class Solution {
* @return
*/
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
- long area = (long) (C - A) * (D - B) + (long)(G - E) * (H - F);
- // Math.min(C, G) ʾұ߱ߵСı
- // Math.max(A, E) ʾ߱ߵı
- // ļԽ飬ҪݷΧ
- long width = Math.max((long)Math.min(C, G) - (long)Math.max(A, E), 0);
- // Math.min(D, H) ʾ߱ߵСı
- // Math.max(B, F) ʾױ߱ߵı
- long height = Math.max((long)Math.min(D, H) - (long)Math.max(B, F), 0);
+ long area = (long) (C - A) * (D - B) + (long) (G - E) * (H - F);
+ // Math.min(C, G) ��ʾ�ұ߱ߵ���С�ı�
+ // Math.max(A, E) ��ʾ��߱ߵ����ı�
+ // ����ļ������������Խ�飬Ҫ�������ݷ�Χ
+ long width = Math.max((long) Math.min(C, G) - (long) Math.max(A, E), 0);
+ // Math.min(D, H) ��ʾ���߱ߵ���С�ı�
+ // Math.max(B, F) ��ʾ�ױ߱ߵ����ı�
+ long height = Math.max((long) Math.min(D, H) - (long) Math.max(B, F), 0);
- System.out.println(width +":" + height + "=" + width*height);
+ System.out.println(width + ":" + height + "=" + width * height);
- return (int)(area - width * height);
+ return (int) (area - width * height);
}
diff --git a/[0232][Implement Queue using Stacks]/src/MyQueue.java b/[0232][Implement Queue using Stacks]/src/MyQueue.java
index a4cdc0c..25f2f3c 100644
--- a/[0232][Implement Queue using Stacks]/src/MyQueue.java
+++ b/[0232][Implement Queue using Stacks]/src/MyQueue.java
@@ -1,6 +1,5 @@
import java.util.LinkedList;
import java.util.List;
-import java.util.Queue;
/**
* @author: wangjunchao(王俊超)
@@ -8,7 +7,7 @@
**/
class MyQueue {
- private List list;
+ private List list;
/**
* Initialize your data structure here.
diff --git a/[0234][Palindrome Linked List]/src/ListNode.java b/[0234][Palindrome Linked List]/src/ListNode.java
index 0304f36..4d4fcb5 100644
--- a/[0234][Palindrome Linked List]/src/ListNode.java
+++ b/[0234][Palindrome Linked List]/src/ListNode.java
@@ -3,7 +3,7 @@
* @time: 2018-10-09 11:03
**/
public class ListNode {
- int val;
+ int val;
ListNode next;
ListNode(int x) {
diff --git a/[0234][Palindrome Linked List]/src/Solution.java b/[0234][Palindrome Linked List]/src/Solution.java
index 9ace1e4..65ba7ea 100644
--- a/[0234][Palindrome Linked List]/src/Solution.java
+++ b/[0234][Palindrome Linked List]/src/Solution.java
@@ -27,7 +27,7 @@ public boolean isPalindrome(ListNode head) {
// 找反向链的起始位置
count = (count + 1) / 2;
node = head;
- while (count >0) {
+ while (count > 0) {
count--;
node = node.next;
}
diff --git a/[0234][Palindrome Linked List]/src/Test.java b/[0234][Palindrome Linked List]/src/Test.java
index 97b7f35..28f5ec5 100644
--- a/[0234][Palindrome Linked List]/src/Test.java
+++ b/[0234][Palindrome Linked List]/src/Test.java
@@ -21,11 +21,11 @@ public static void test2() {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
- head.next.next= new ListNode(2);
- head.next.next.next= new ListNode(1);
- head.next.next.next.next= new ListNode(2);
- head.next.next.next.next.next= new ListNode(2);
- head.next.next.next.next.next.next= new ListNode(1);
+ head.next.next = new ListNode(2);
+ head.next.next.next = new ListNode(1);
+ head.next.next.next.next = new ListNode(2);
+ head.next.next.next.next.next = new ListNode(2);
+ head.next.next.next.next.next.next = new ListNode(1);
System.out.println(solution.isPalindrome(head));
}
@@ -34,8 +34,8 @@ public static void test3() {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
- head.next.next= new ListNode(2);
- head.next.next.next= new ListNode(1);
+ head.next.next = new ListNode(2);
+ head.next.next.next = new ListNode(1);
System.out.println(solution.isPalindrome(head));
}
diff --git a/[0235][Lowest Common Ancestor of a Binary Search Tree]/src/TreeNode.java b/[0235][Lowest Common Ancestor of a Binary Search Tree]/src/TreeNode.java
index cb9448f..141d430 100644
--- a/[0235][Lowest Common Ancestor of a Binary Search Tree]/src/TreeNode.java
+++ b/[0235][Lowest Common Ancestor of a Binary Search Tree]/src/TreeNode.java
@@ -3,7 +3,7 @@
* @time: 2018-10-09 11:33
**/
public class TreeNode {
- int val;
+ int val;
TreeNode left;
TreeNode right;
diff --git a/[0237][Delete Node in a Linked List]/src/ListNode.java b/[0237][Delete Node in a Linked List]/src/ListNode.java
index 50f6a11..9e0f1da 100644
--- a/[0237][Delete Node in a Linked List]/src/ListNode.java
+++ b/[0237][Delete Node in a Linked List]/src/ListNode.java
@@ -3,7 +3,7 @@
* @time: 2018-10-10 09:58
**/
public class ListNode {
- int val;
+ int val;
ListNode next;
ListNode(int x) {
diff --git a/[0237][Delete Node in a Linked List]/src/Solution.java b/[0237][Delete Node in a Linked List]/src/Solution.java
index b440a61..6c60753 100644
--- a/[0237][Delete Node in a Linked List]/src/Solution.java
+++ b/[0237][Delete Node in a Linked List]/src/Solution.java
@@ -7,6 +7,7 @@
* ListNode(int x) { val = x; }
* }
*
+ *
* @author: wangjunchao(王俊超)
* @time: 2018-10-10 09:57
**/
diff --git a/[0237][Delete Node in a Linked List]/src/Test.java b/[0237][Delete Node in a Linked List]/src/Test.java
index 8863287..a6ee290 100644
--- a/[0237][Delete Node in a Linked List]/src/Test.java
+++ b/[0237][Delete Node in a Linked List]/src/Test.java
@@ -19,7 +19,7 @@ private static void print(ListNode node) {
do {
if (node == null) {
System.out.println("null");
- } else if (node.next != null){
+ } else if (node.next != null) {
System.out.print(node.val + "->");
node = node.next;
} else {
@@ -27,6 +27,6 @@ private static void print(ListNode node) {
node = node.next;
}
- }while (node != null);
+ } while (node != null);
}
}
diff --git a/[0238][Product of Array Except Self]/src/Solution.java b/[0238][Product of Array Except Self]/src/Solution.java
index 0c25aa7..e0c6b32 100644
--- a/[0238][Product of Array Except Self]/src/Solution.java
+++ b/[0238][Product of Array Except Self]/src/Solution.java
@@ -22,6 +22,7 @@ public class Solution {
* Could you solve it with constant space complexity? (The output array does not count as
* extra space for the purpose of space complexity analysis.)
*
+ *
* @param nums
* @return
*/
@@ -33,7 +34,7 @@ public int[] productExceptSelf(int[] nums) {
long product = 1;
- for (int i = 0; i < nums.length; i++) {
+ for (int i = 0; i < nums.length; i++) {
product *= nums[i];
if (nums[i] == 0) {
zero.add(i);
@@ -43,13 +44,13 @@ public int[] productExceptSelf(int[] nums) {
// 0的个数大于1个
if (zero.size() > 1) {
return result;
- } else if (zero.size() == 1){
+ } else if (zero.size() == 1) {
int firstZeroIndex = 0;
- for (Integer i: zero) {
+ for (Integer i : zero) {
firstZeroIndex = i;
}
- result[firstZeroIndex] = 1;
+ result[firstZeroIndex] = 1;
for (int i = 0; i < nums.length; i++) {
if (i != firstZeroIndex) {
result[firstZeroIndex] *= nums[i];
diff --git a/[0240][Search a 2D Matrix II]/src/Solution.java b/[0240][Search a 2D Matrix II]/src/Solution.java
index 0119f00..4b780fe 100644
--- a/[0240][Search a 2D Matrix II]/src/Solution.java
+++ b/[0240][Search a 2D Matrix II]/src/Solution.java
@@ -32,7 +32,7 @@ public class Solution {
*/
public boolean searchMatrix(int[][] matrix, int target) {
- if (matrix == null ||matrix.length == 0) {
+ if (matrix == null || matrix.length == 0) {
return false;
}
diff --git a/[0241][Different Ways to Add Parentheses]/src/Solution.java b/[0241][Different Ways to Add Parentheses]/src/Solution.java
index 4a5c8ad..d996d4c 100644
--- a/[0241][Different Ways to Add Parentheses]/src/Solution.java
+++ b/[0241][Different Ways to Add Parentheses]/src/Solution.java
@@ -30,6 +30,7 @@ public class Solution {
* (2*((3-4)*5)) = -10
* (((2*3)-4)*5) = 10
*
+ *
* @param input
* @return
*/
diff --git a/[0278][First Bad Version]/src/VersionControl.java b/[0278][First Bad Version]/src/VersionControl.java
index d124b97..327bc14 100644
--- a/[0278][First Bad Version]/src/VersionControl.java
+++ b/[0278][First Bad Version]/src/VersionControl.java
@@ -6,7 +6,7 @@ public class VersionControl {
private boolean[] badVersion;
- public boolean isBadVersion(int version){
+ public boolean isBadVersion(int version) {
return badVersion[version - 1];
}
diff --git a/[0292][Nim Game]/src/Solution.java b/[0292][Nim Game]/src/Solution.java
index 2193932..a6fa330 100644
--- a/[0292][Nim Game]/src/Solution.java
+++ b/[0292][Nim Game]/src/Solution.java
@@ -1,5 +1,6 @@
/**
* https://leetcode.com/problems/nim-game/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-08 18:39
**/
@@ -26,6 +27,7 @@ public class Solution {
* 轮完后都有4的倍数个石头被取走。这样,如果我们先走的话,先把n除4的余数个石头拿走,这样不管怎样,到最
* 后都会留4个下来,对方取1个你就取3个,对方取2个你就取2个,就必赢了。
*
+ *
* @param n
* @return
*/
@@ -34,6 +36,6 @@ public boolean canWinNim(int n) {
}
public boolean canWinNim2(int n) {
- return n %4 != 0;
+ return n % 4 != 0;
}
}
diff --git a/[0299][Bulls and Cows]/[0299][Bulls and Cows].iml b/[0299][Bulls and Cows]/[0299][Bulls and Cows].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0299][Bulls and Cows]/[0299][Bulls and Cows].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0299][Bulls and Cows]/src/Solution.java b/[0299][Bulls and Cows]/src/Solution.java
new file mode 100644
index 0000000..cb785ab
--- /dev/null
+++ b/[0299][Bulls and Cows]/src/Solution.java
@@ -0,0 +1,85 @@
+//You are playing the following Bulls and Cows game with your friend: You write
+//down a number and ask your friend to guess what the number is. Each time your fr
+//iend makes a guess, you provide a hint that indicates how many digits in said gu
+//ess match your secret number exactly in both digit and position (called "bulls")
+// and how many digits match the secret number but locate in the wrong position (c
+//alled "cows"). Your friend will use successive guesses and hints to eventually d
+//erive the secret number.
+//
+// Write a function to return a hint according to the secret number and friend's
+// guess, use A to indicate the bulls and B to indicate the cows.
+//
+// Please note that both secret number and friend's guess may contain duplicate
+//digits.
+//
+// Example 1:
+//
+//
+//Input: secret = "1807", guess = "7810"
+//
+//Output: "1A3B"
+//
+//Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.
+//
+// Example 2:
+//
+//
+//Input: secret = "1123", guess = "0111"
+//
+//Output: "1A1B"
+//
+//Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.
+//
+//
+// Note: You may assume that the secret number and your friend's guess only cont
+//ain digits, and their lengths are always equal. Related Topics Hash Table
+
+
+import java.util.HashMap;
+import java.util.Map;
+
+//leetcode submit region begin(Prohibit modification and deletion)
+class Solution {
+ public String getHint(String secret, String guess) {
+
+ // 假如参数不合法
+ if (secret == null || guess == null || secret.length() != guess.length()) {
+ throw new IllegalArgumentException("secret: " + secret + ", guess: " + guess);
+ }
+
+ Map indexMap = new HashMap<>();
+ Map countMap = new HashMap<>();
+
+ for (int i = 0; i < secret.length(); i++) {
+ char c = secret.charAt(i);
+ indexMap.put(i, c);
+ countMap.put(c, 1 + countMap.getOrDefault(c, 0)); // 记录字符出现的次数
+ }
+
+ int bull = 0;
+ int cows = 0;
+
+ // 先计算bull
+ for (int i = 0; i < guess.length(); i++) {
+ char c = guess.charAt(i);
+ if (indexMap.get(i) == c) {
+ bull++;
+ indexMap.remove(i); // 删除已经匹配过的位置
+ countMap.put(c, countMap.get(c) - 1);
+ }
+ }
+
+ // 再计算cows
+ for (int i = 0; i < guess.length(); i++) {
+ char c = guess.charAt(i);
+ // 位置没有被匹配过,并且值大于0,说明猜测的值是有的,但是位置错误了
+ if (indexMap.containsKey(i) && countMap.getOrDefault(c, 0) > 0) {
+ cows++;
+ countMap.put(c, countMap.get(c) - 1);
+ }
+ }
+
+ return bull + "A" + cows + "B";
+ }
+}
+//leetcode submit region end(Prohibit modification and deletion)
diff --git a/[0299][Bulls and Cows]/src/TestSolution.java b/[0299][Bulls and Cows]/src/TestSolution.java
new file mode 100644
index 0000000..edc42ed
--- /dev/null
+++ b/[0299][Bulls and Cows]/src/TestSolution.java
@@ -0,0 +1,14 @@
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestSolution {
+ @Test
+ public void test1() {
+ String[][] data = {{"1807", "7810", "1A3B"}, {"1123", "0111", "1A1B"}, {"11", "10", "1A0B"}};
+
+ for (String[] datum : data) {
+ String result = new Solution().getHint(datum[0], datum[1]);
+ Assert.assertEquals(result, datum[2]);
+ }
+ }
+}
diff --git a/[0303][Range Sum Query - Immutable]/src/NumArray.java b/[0303][Range Sum Query - Immutable]/src/NumArray.java
index fb70aae..c64d89d 100644
--- a/[0303][Range Sum Query - Immutable]/src/NumArray.java
+++ b/[0303][Range Sum Query - Immutable]/src/NumArray.java
@@ -12,11 +12,11 @@ public NumArray(int[] nums) {
public int sumRange(int i, int j) {
if (i > j) {
- throw new IllegalArgumentException("i = " + i + ", j = " + j +", i must not greater than j");
+ throw new IllegalArgumentException("i = " + i + ", j = " + j + ", i must not greater than j");
}
if (j < 0) {
- throw new IllegalArgumentException("i = " + i + ", j = " + j +", i must not less than 0");
+ throw new IllegalArgumentException("i = " + i + ", j = " + j + ", i must not less than 0");
}
i = i < 0 ? 0 : i;
diff --git a/[0326][Power of Three]/src/Solution.java b/[0326][Power of Three]/src/Solution.java
index 8ab7b8d..71d743c 100644
--- a/[0326][Power of Three]/src/Solution.java
+++ b/[0326][Power of Three]/src/Solution.java
@@ -6,7 +6,7 @@ public class Solution {
public boolean isPowerOfThree(int n) {
double rst = (Math.log10(n) / Math.log10(3));
- return n > 0 &&Double.doubleToLongBits((int)rst) == Double.doubleToLongBits(rst);
+ return n > 0 && Double.doubleToLongBits((int) rst) == Double.doubleToLongBits(rst);
}
public boolean isPowerOfThree2(int n) {
diff --git a/[0326][Power of Three]/src/Test.java b/[0326][Power of Three]/src/Test.java
index c99e19b..9cb8806 100644
--- a/[0326][Power of Three]/src/Test.java
+++ b/[0326][Power of Three]/src/Test.java
@@ -4,10 +4,10 @@
**/
public class Test {
public static void main(String[] args) {
- System.out.println(Double.doubleToLongBits((int)1.00000000001)
+ System.out.println(Double.doubleToLongBits((int) 1.00000000001)
== Double.doubleToLongBits(1.00000000001));
- System.out.println(Double.doubleToLongBits((int)1.00000000000)
+ System.out.println(Double.doubleToLongBits((int) 1.00000000000)
== Double.doubleToLongBits(1.00000000000));
}
}
diff --git a/[0338][Counting Bits ]/src/Solution.java b/[0338][Counting Bits ]/src/Solution.java
index 68effd5..901792b 100644
--- a/[0338][Counting Bits ]/src/Solution.java
+++ b/[0338][Counting Bits ]/src/Solution.java
@@ -31,6 +31,7 @@ public class Solution {
* i为偶数 bit(i) = bit(i/2)
* i为奇数 bit(i) = bit(i/2) + 1
*
+ *
* @param num
* @return
*/
diff --git a/[0344][Reverse String]/src/Solution.java b/[0344][Reverse String]/src/Solution.java
index 955ed6b..b7bc9c5 100644
--- a/[0344][Reverse String]/src/Solution.java
+++ b/[0344][Reverse String]/src/Solution.java
@@ -10,7 +10,7 @@ public String reverseString(String s) {
char[] chars = s.toCharArray();
- for (int i = 0, j = chars.length - 1; i
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0371][Sum of Two Integers]/src/Solution.java b/[0371][Sum of Two Integers]/src/Solution.java
new file mode 100644
index 0000000..411fe08
--- /dev/null
+++ b/[0371][Sum of Two Integers]/src/Solution.java
@@ -0,0 +1,52 @@
+//Calculate the sum of two integers a and b, but you are not allowed to use the
+//operator + and -.
+//
+//
+// Example 1:
+//
+//
+//Input: a = 1, b = 2
+//Output: 3
+//
+//
+//
+// Example 2:
+//
+//
+//Input: a = -2, b = 3
+//Output: 1
+//
+//
+//
+// Related Topics Bit Manipulation
+
+
+//leetcode submit region begin(Prohibit modification and deletion)
+
+/**
+ * Author: 王俊超
+ * Time: 2016-07-03 21-25 10:58
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ */
+public class Solution {
+ public int getSum(int a, int b) {
+
+ // 保存不进位值,异或运算结果
+ int xor;
+ // 保存进位结果,与运算,然后左移一位结果
+ int and;
+ do {
+ xor = a ^ b;
+ and = a & b;
+ and <<= 1;
+ a = xor;
+ b = and;
+
+ } while (and != 0);
+
+ return xor;
+ }
+}
+//leetcode submit region end(Prohibit modification and deletion)
diff --git a/[0371][Sum of Two Integers]/src/SolutionTest.java b/[0371][Sum of Two Integers]/src/SolutionTest.java
new file mode 100644
index 0000000..275433e
--- /dev/null
+++ b/[0371][Sum of Two Integers]/src/SolutionTest.java
@@ -0,0 +1,2 @@
+public class SolutionTest {
+}
diff --git a/[0374][Guess Number Higher or Lower]/src/Solution.java b/[0374][Guess Number Higher or Lower]/src/Solution.java
index a0b637a..05dec89 100644
--- a/[0374][Guess Number Higher or Lower]/src/Solution.java
+++ b/[0374][Guess Number Higher or Lower]/src/Solution.java
@@ -21,6 +21,7 @@ public class Solution extends GuessGame {
* Input: n = 10, pick = 6
* Output: 6
*
+ *
* @param n
* @return
*/
diff --git a/[0387][First Unique Character in a String]/src/Solution.java b/[0387][First Unique Character in a String]/src/Solution.java
index 349a460..ea4eea0 100644
--- a/[0387][First Unique Character in a String]/src/Solution.java
+++ b/[0387][First Unique Character in a String]/src/Solution.java
@@ -20,6 +20,7 @@ public class Solution {
* s = "loveleetcode",
* return 2.
*
+ *
* @param s
* @return
*/
@@ -43,7 +44,7 @@ public int firstUniqChar(String s) {
Integer result = null;
- for(Integer n: map.values()) {
+ for (Integer n : map.values()) {
if (n >= 0) {
if (result == null) {
result = n;
diff --git a/[0389][Find the Difference]/src/Solution.java b/[0389][Find the Difference]/src/Solution.java
index b2fcd9a..a22ff15 100644
--- a/[0389][Find the Difference]/src/Solution.java
+++ b/[0389][Find the Difference]/src/Solution.java
@@ -26,6 +26,7 @@ public class Solution {
* Explanation:
* 'e' is the letter that was added.
*
+ *
* @param s
* @param t
* @return
diff --git a/[0392][Is Subsequence]/[0392][Is Subsequence].iml b/[0392][Is Subsequence]/[0392][Is Subsequence].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0392][Is Subsequence]/[0392][Is Subsequence].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0392][Is Subsequence]/src/Solution.java b/[0392][Is Subsequence]/src/Solution.java
new file mode 100644
index 0000000..fd4ddb2
--- /dev/null
+++ b/[0392][Is Subsequence]/src/Solution.java
@@ -0,0 +1,81 @@
+//Given a string s and a string t, check if s is subsequence of t.
+//
+// A subsequence of a string is a new string which is formed from the original s
+//tring by deleting some (can be none) of the characters without disturbing the re
+//lative positions of the remaining characters. (ie, "ace" is a subsequence of "ab
+//cde" while "aec" is not).
+//
+// Follow up:
+//If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you w
+//ant to check one by one to see if T has its subsequence. In this scenario, how w
+//ould you change your code?
+//
+// Credits:
+//Special thanks to @pbrother for adding this problem and creating all test case
+//s.
+//
+//
+// Example 1:
+// Input: s = "abc", t = "ahbgdc"
+//Output: true
+// Example 2:
+// Input: s = "axc", t = "ahbgdc"
+//Output: false
+//
+//
+// Constraints:
+//
+//
+// 0 <= s.length <= 100
+// 0 <= t.length <= 10^4
+// Both strings consists only of lowercase characters.
+//
+// Related Topics Binary Search Dynamic Programming Greedy
+
+
+//leetcode submit region begin(Prohibit modification and deletion)
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-24 10:48
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public boolean isSubsequence(String s, String t) {
+
+ if (s == null && t == null) {
+ return true;
+ }
+
+ if (s == null || t == null) {
+ return false;
+ }
+
+ if (s.length() == 0) {
+ return true;
+ }
+
+ if (t.length() == 0) {
+ return false;
+ }
+
+ int idxT = 0;
+ int idxS = 0;
+ while (idxS < s.length() && idxT < t.length()) {
+
+ if (s.charAt(idxS) == t.charAt(idxT)) {
+ idxS++;
+ idxT++;
+ } else {
+ idxT++;
+ }
+ }
+
+
+ return idxS == s.length();
+
+ }
+}
+//leetcode submit region end(Prohibit modification and deletion)
diff --git a/[0392][Is Subsequence]/src/SolutionTest.java b/[0392][Is Subsequence]/src/SolutionTest.java
new file mode 100644
index 0000000..7cd621c
--- /dev/null
+++ b/[0392][Is Subsequence]/src/SolutionTest.java
@@ -0,0 +1,29 @@
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-25 14:03
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+ @Test
+ public void test1() {
+ Object[][] data = {
+
+ {"", "", true},
+ {"", "ahbgdc", true},
+ {"ahbgdc", "", false},
+ {"abc", "ahbgdc", true},
+ {"axc", "ahbgdc", false},
+ {"leetcode", "lleettccooddee", true},
+ };
+
+ for (Object[] datum : data) {
+ Solution s = new Solution();
+ Assert.assertEquals(s.isSubsequence((String) datum[0], (String) datum[1]), datum[2]);
+ }
+ }
+}
diff --git a/[0400][Nth Digit]/src/Solution2.java b/[0400][Nth Digit]/src/Solution2.java
index b4fc5cb..4a5b2c9 100644
--- a/[0400][Nth Digit]/src/Solution2.java
+++ b/[0400][Nth Digit]/src/Solution2.java
@@ -1,5 +1,6 @@
/**
* https://leetcode.com/problems/nth-digit/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-09 15:56
**/
@@ -31,6 +32,7 @@ public class Solution2 {
*
* https://www.cnblogs.com/grandyang/p/5891871.html
*
+ *
* @param n
* @return
*/
diff --git a/[0401][Binary Watch]/src/Main.java b/[0401][Binary Watch]/src/Main.java
index 7369853..efea389 100644
--- a/[0401][Binary Watch]/src/Main.java
+++ b/[0401][Binary Watch]/src/Main.java
@@ -1,4 +1,3 @@
-import org.junit.Assert;
import org.junit.Test;
/**
diff --git a/[0404][Sum of Left Leaves]/src/Main.java b/[0404][Sum of Left Leaves]/src/Main.java
index b80e566..cf253ab 100644
--- a/[0404][Sum of Left Leaves]/src/Main.java
+++ b/[0404][Sum of Left Leaves]/src/Main.java
@@ -9,7 +9,7 @@ public class Main {
@Test
public void test1() {
TreeNode root = new TreeNode(3);
- root.left =new TreeNode(9);
+ root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
diff --git a/[0404][Sum of Left Leaves]/src/Solution.java b/[0404][Sum of Left Leaves]/src/Solution.java
index fb3f2d6..d5a8ad8 100644
--- a/[0404][Sum of Left Leaves]/src/Solution.java
+++ b/[0404][Sum of Left Leaves]/src/Solution.java
@@ -1,4 +1,3 @@
-
/**
* @author: wangjunchao(王俊超)
* @time: 2019-06-30 14:11
@@ -30,6 +29,7 @@ public int sumOfLeftLeaves(TreeNode root) {
/**
* 计算所有左叶子的和
+ *
* @param root
* @param result
*/
@@ -39,7 +39,7 @@ private void sumOfLeftLeaves(TreeNode root, boolean isLeftLeaf, int[] result) {
return;
}
- if (root.left == null && root.right==null && isLeftLeaf) {
+ if (root.left == null && root.right == null && isLeftLeaf) {
result[0] += root.val;
return;
}
diff --git a/[0405][Convert a Number to Hexadecimal]/src/Main.java b/[0405][Convert a Number to Hexadecimal]/src/Main.java
index 343277e..0e56b0e 100644
--- a/[0405][Convert a Number to Hexadecimal]/src/Main.java
+++ b/[0405][Convert a Number to Hexadecimal]/src/Main.java
@@ -7,19 +7,19 @@
**/
public class Main {
@Test
- public void test1(){
+ public void test1() {
Solution solution = new Solution();
Assert.assertEquals("1a", solution.toHex(26));
}
@Test
- public void test2(){
+ public void test2() {
Solution solution = new Solution();
Assert.assertEquals("ffffffff", solution.toHex(-1));
}
@Test
- public void test3(){
+ public void test3() {
Solution solution = new Solution();
Assert.assertEquals("10", solution.toHex(16));
}
diff --git a/[0409][Longest Palindrome]/src/Solution2.java b/[0409][Longest Palindrome]/src/Solution2.java
index 4546ba5..ad898f8 100644
--- a/[0409][Longest Palindrome]/src/Solution2.java
+++ b/[0409][Longest Palindrome]/src/Solution2.java
@@ -1,5 +1,6 @@
/**
* https://leetcode.com/problems/longest-palindrome/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-03 04:08
**/
diff --git a/[0412][Fizz Buzz]/src/Solution.java b/[0412][Fizz Buzz]/src/Solution.java
index 9689a9c..12a2ebe 100644
--- a/[0412][Fizz Buzz]/src/Solution.java
+++ b/[0412][Fizz Buzz]/src/Solution.java
@@ -39,6 +39,7 @@ public class Solution {
* "FizzBuzz"
* ]
*
+ *
* @param n
* @return
*/
diff --git a/[0415][Add Strings]/src/Solution.java b/[0415][Add Strings]/src/Solution.java
index 9ce5f26..6c1cdab 100644
--- a/[0415][Add Strings]/src/Solution.java
+++ b/[0415][Add Strings]/src/Solution.java
@@ -1,67 +1,67 @@
+/**
+ * https://leetcode.com/problems/add-strings/
+ *
+ * @author: wangjunchao(王俊超)
+ * @time: 2019-07-03 04:26
+ **/
+public class Solution {
/**
- * https://leetcode.com/problems/add-strings/
+ *
+ * Given two non-negative integers num1 and num2 represented as string, return the sum of
+ * num1 and num2.
*
- * @author: wangjunchao(王俊超)
- * @time: 2019-07-03 04:26
- **/
- public class Solution {
- /**
- *
- * Given two non-negative integers num1 and num2 represented as string, return the sum of
- * num1 and num2.
- *
- * Note:
- *
- * The length of both num1 and num2 is < 5100.
- * Both num1 and num2 contains only digits 0-9.
- * Both num1 and num2 does not contain any leading zero.
- * You must not use any built-in BigInteger library or convert the inputs to integer directly.
- *
- *
- * @param num1
- * @param num2
- * @return
- */
- public String addStrings(String num1, String num2) {
- if (num1.length() > num2.length()) {
- // 保证第两个字符串不短于第一个
- return addStrings(num2, num1);
- }
+ * Note:
+ *
+ * The length of both num1 and num2 is < 5100.
+ * Both num1 and num2 contains only digits 0-9.
+ * Both num1 and num2 does not contain any leading zero.
+ * You must not use any built-in BigInteger library or convert the inputs to integer directly.
+ *
+ *
+ * @param num1
+ * @param num2
+ * @return
+ */
+ public String addStrings(String num1, String num2) {
+ if (num1.length() > num2.length()) {
+ // 保证第两个字符串不短于第一个
+ return addStrings(num2, num1);
+ }
- int[] result = new int[num2.length()];
+ int[] result = new int[num2.length()];
- int idx1 = num1.length() - 1;
- int idx2 = num2.length() - 1;
- int idx3 = result.length - 1;
- int carry = 0;
- while (idx1 >= 0) {
- int a = num1.charAt(idx1) - '0';
- int b = num2.charAt(idx2) - '0';
- result[idx3] = (a + b + carry) % 10;
- carry = (a + b + carry) / 10;
- idx1--;
- idx2--;
- idx3--;
- }
+ int idx1 = num1.length() - 1;
+ int idx2 = num2.length() - 1;
+ int idx3 = result.length - 1;
+ int carry = 0;
+ while (idx1 >= 0) {
+ int a = num1.charAt(idx1) - '0';
+ int b = num2.charAt(idx2) - '0';
+ result[idx3] = (a + b + carry) % 10;
+ carry = (a + b + carry) / 10;
+ idx1--;
+ idx2--;
+ idx3--;
+ }
- // num2还未处理完
- while (idx2 >= 0) {
- int a = num2.charAt(idx2) - '0';
- result[idx3] = (a + carry) % 10;
- carry = (a + carry) / 10;
- idx2--;
- idx3--;
- }
+ // num2还未处理完
+ while (idx2 >= 0) {
+ int a = num2.charAt(idx2) - '0';
+ result[idx3] = (a + carry) % 10;
+ carry = (a + carry) / 10;
+ idx2--;
+ idx3--;
+ }
- StringBuilder builder = new StringBuilder();
- if (carry > 0) {
- builder.append(carry);
- }
+ StringBuilder builder = new StringBuilder();
+ if (carry > 0) {
+ builder.append(carry);
+ }
- for(int i : result) {
- builder.append(i);
- }
- return builder.toString();
+ for (int i : result) {
+ builder.append(i);
}
+ return builder.toString();
}
+}
diff --git a/[0419][Battleships in a Board]/src/Main.java b/[0419][Battleships in a Board]/src/Main.java
index 2ef0da0..1b46aca 100644
--- a/[0419][Battleships in a Board]/src/Main.java
+++ b/[0419][Battleships in a Board]/src/Main.java
@@ -9,9 +9,9 @@ public class Main {
@Test
public void test1() {
char[][] board = {
- {'X','.', ',', 'X'},
- {'.','.', ',', 'X'},
- {'.','.', ',', 'X'}
+ {'X', '.', ',', 'X'},
+ {'.', '.', ',', 'X'},
+ {'.', '.', ',', 'X'}
};
Solution solution = new Solution();
diff --git a/[0419][Battleships in a Board]/src/Solution.java b/[0419][Battleships in a Board]/src/Solution.java
index 65b0a27..0452809 100644
--- a/[0419][Battleships in a Board]/src/Solution.java
+++ b/[0419][Battleships in a Board]/src/Solution.java
@@ -16,11 +16,11 @@ public int countBattleships(char[][] board) {
if (board[i][j] == 'X') {
// 如果左边一个并且上面一个不是X,说明是战舰的起点
// 考虑边界情况
- if (i > 0 && j > 0 && board[i -1][j] != board[i][j] && board[i][j - 1] != board[i][j]) {
+ if (i > 0 && j > 0 && board[i - 1][j] != board[i][j] && board[i][j - 1] != board[i][j]) {
result++;
- } else if (i > 0 && j == 0 && board[i -1][j] != board[i][j]) {
+ } else if (i > 0 && j == 0 && board[i - 1][j] != board[i][j]) {
result++;
- } else if (i == 0 && j > 0 && board[i][j - 1] != board[i][j] ) {
+ } else if (i == 0 && j > 0 && board[i][j - 1] != board[i][j]) {
result++;
} else if (i == 0 && j == 0) {
result++;
diff --git a/[0427][Construct Quad Tree]/src/Node.java b/[0427][Construct Quad Tree]/src/Node.java
index 3123e2a..6dbf8a7 100644
--- a/[0427][Construct Quad Tree]/src/Node.java
+++ b/[0427][Construct Quad Tree]/src/Node.java
@@ -5,10 +5,10 @@
public class Node {
public boolean val;
public boolean isLeaf;
- public Node topLeft;
- public Node topRight;
- public Node bottomLeft;
- public Node bottomRight;
+ public Node topLeft;
+ public Node topRight;
+ public Node bottomLeft;
+ public Node bottomRight;
public Node() {
}
diff --git a/[0427][Construct Quad Tree]/src/Solution.java b/[0427][Construct Quad Tree]/src/Solution.java
index a46f286..7489596 100644
--- a/[0427][Construct Quad Tree]/src/Solution.java
+++ b/[0427][Construct Quad Tree]/src/Solution.java
@@ -38,7 +38,7 @@ private void construct(Node node, int[][] grid, int x, int y, int size) {
construct(topLeft, grid, x, y, half);
construct(topRight, grid, x, y + half, half);
construct(bottomLeft, grid, x + half, y, half);
- construct(bottomRight, grid, x+ half, y + half, half);
+ construct(bottomRight, grid, x + half, y + half, half);
// 四个孩子都是叶子节点,并且值都相等
if (topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf) {
diff --git a/[0429][N-ary Tree Level Order Traversal]/src/Main.java b/[0429][N-ary Tree Level Order Traversal]/src/Main.java
index aa6fc47..45d4d16 100644
--- a/[0429][N-ary Tree Level Order Traversal]/src/Main.java
+++ b/[0429][N-ary Tree Level Order Traversal]/src/Main.java
@@ -1,4 +1,3 @@
-import com.sun.tools.corba.se.idl.constExpr.Not;
import org.junit.Test;
import java.util.LinkedList;
diff --git a/[0429][N-ary Tree Level Order Traversal]/src/Node.java b/[0429][N-ary Tree Level Order Traversal]/src/Node.java
index 2eee60a..4a21647 100644
--- a/[0429][N-ary Tree Level Order Traversal]/src/Node.java
+++ b/[0429][N-ary Tree Level Order Traversal]/src/Node.java
@@ -5,7 +5,7 @@
* @time: 2019-07-01 14:44
**/
public class Node {
- public int val;
+ public int val;
public List children;
public Node() {
diff --git a/[0434][Number of Segments in a String]/src/Solution.java b/[0434][Number of Segments in a String]/src/Solution.java
index 1bcf1ee..8fd15cf 100644
--- a/[0434][Number of Segments in a String]/src/Solution.java
+++ b/[0434][Number of Segments in a String]/src/Solution.java
@@ -15,6 +15,7 @@ public class Solution {
* Input: "Hello, my name is John"
* Output: 5
*
+ *
* @param s
* @return
*/
diff --git a/[0437][Path Sum III]/src/Main.java b/[0437][Path Sum III]/src/Main.java
index bde432a..30b8b7d 100644
--- a/[0437][Path Sum III]/src/Main.java
+++ b/[0437][Path Sum III]/src/Main.java
@@ -13,8 +13,8 @@ public void test1() {
root.right = new TreeNode(-3);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(2);
- root.left.left.left =new TreeNode(3);
- root.left.left.right =new TreeNode(-2);
+ root.left.left.left = new TreeNode(3);
+ root.left.left.right = new TreeNode(-2);
root.left.right.right = new TreeNode(1);
root.right.right = new TreeNode(11);
@@ -29,8 +29,8 @@ public void test2() {
root.right = new TreeNode(-3);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(2);
- root.left.left.left =new TreeNode(3);
- root.left.left.right =new TreeNode(-2);
+ root.left.left.left = new TreeNode(3);
+ root.left.left.right = new TreeNode(-2);
root.left.right.right = new TreeNode(1);
root.right.right = new TreeNode(11);
@@ -45,8 +45,8 @@ public void test3() {
root.right = new TreeNode(-3);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(2);
- root.left.left.left =new TreeNode(3);
- root.left.left.right =new TreeNode(-2);
+ root.left.left.left = new TreeNode(3);
+ root.left.left.right = new TreeNode(-2);
root.left.right.right = new TreeNode(1);
root.right.right = new TreeNode(11);
@@ -61,8 +61,8 @@ public void test4() {
root.right = new TreeNode(-3);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(2);
- root.left.left.left =new TreeNode(3);
- root.left.left.right =new TreeNode(-2);
+ root.left.left.left = new TreeNode(3);
+ root.left.left.right = new TreeNode(-2);
root.left.right.right = new TreeNode(1);
root.right.right = new TreeNode(11);
diff --git a/[0437][Path Sum III]/src/TreeNode.java b/[0437][Path Sum III]/src/TreeNode.java
index a09e411..23d0f71 100644
--- a/[0437][Path Sum III]/src/TreeNode.java
+++ b/[0437][Path Sum III]/src/TreeNode.java
@@ -4,7 +4,7 @@
**/
public class TreeNode {
- int val;
+ int val;
TreeNode left;
TreeNode right;
diff --git a/[0438][Find All Anagrams in a String]/src/Solution.java b/[0438][Find All Anagrams in a String]/src/Solution.java
index 766fa5c..8e32c89 100644
--- a/[0438][Find All Anagrams in a String]/src/Solution.java
+++ b/[0438][Find All Anagrams in a String]/src/Solution.java
@@ -1,9 +1,6 @@
import java.util.Arrays;
-import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
-import java.util.Objects;
/**
* @author: wangjunchao(王俊超)
@@ -71,7 +68,7 @@ public List findAnagrams(String s, String p) {
sArr[s.charAt(i + p.length() - 1) - 'a']++;
}
- if (Arrays.equals(sArr, pArr) ) {
+ if (Arrays.equals(sArr, pArr)) {
result.add(i);
}
}
diff --git a/[0438][Find All Anagrams in a String]/src/Solution2.java b/[0438][Find All Anagrams in a String]/src/Solution2.java
index f72d6e4..1f805b7 100644
--- a/[0438][Find All Anagrams in a String]/src/Solution2.java
+++ b/[0438][Find All Anagrams in a String]/src/Solution2.java
@@ -1,8 +1,4 @@
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
/**
* @author: wangjunchao(王俊超)
diff --git a/[0441][Arranging Coins]/[0441][Arranging Coins].iml b/[0441][Arranging Coins]/[0441][Arranging Coins].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0441][Arranging Coins]/[0441][Arranging Coins].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0441][Arranging Coins]/src/Solution.java b/[0441][Arranging Coins]/src/Solution.java
new file mode 100644
index 0000000..f1c8291
--- /dev/null
+++ b/[0441][Arranging Coins]/src/Solution.java
@@ -0,0 +1,83 @@
+//You have a total of n coins that you want to form in a staircase shape, where
+//every k-th row must have exactly k coins.
+//
+// Given n, find the total number of full staircase rows that can be formed.
+//
+// n is a non-negative integer and fits within the range of a 32-bit signed inte
+//ger.
+//
+// Example 1:
+//
+//n = 5
+//
+//The coins can form the following rows:
+//¤
+//¤ ¤
+//¤ ¤
+//
+//Because the 3rd row is incomplete, we return 2.
+//
+//
+//
+// Example 2:
+//
+//n = 8
+//
+//The coins can form the following rows:
+//¤
+//¤ ¤
+//¤ ¤ ¤
+//¤ ¤
+//
+//Because the 4th row is incomplete, we return 3.
+//
+// Related Topics Math Binary Search
+
+
+//leetcode submit region begin(Prohibit modification and deletion)
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-25 14:22
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 本质是求最大的整数x,使得 x >= n - x(x + 1)/2 >= 0
+ * ==>
+ * 2x >= 2n - x^2 - x >= 0
+ * ==>
+ * x^2 + 3x >= 2n >= x^2 + x
+ *
+ * @param n
+ * @return
+ */
+ public int arrangeCoins(int n) {
+
+ if (n <= 0) {
+ return 0;
+ }
+
+ long t = 2 * (long) (n);
+
+ long x = (long) Math.sqrt(t);
+
+ while (true) {
+ if (x * x + 3 * x < t) {
+ x++;
+ continue;
+ }
+ if (x * x + x > t) {
+ x--;
+ continue;
+ }
+
+ break;
+ }
+
+ return (int) x;
+ }
+}
+//leetcode submit region end(Prohibit modification and deletion)
diff --git a/[0441][Arranging Coins]/src/SolutionTest.java b/[0441][Arranging Coins]/src/SolutionTest.java
new file mode 100644
index 0000000..378df37
--- /dev/null
+++ b/[0441][Arranging Coins]/src/SolutionTest.java
@@ -0,0 +1,35 @@
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-25 14:32
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+ @Test
+ public void test1() {
+ Solution s = new Solution();
+
+ int[][] data = {
+ {-1, 0},
+ {0, 0},
+ {1, 1},
+ {2, 1},
+ {5, 2},
+ {8, 3},
+ {10, 4},
+ {11, 4},
+ {12, 4},
+ {13, 4},
+ {14, 4},
+ {15, 5},
+ };
+
+ for (int[] d : data) {
+ Assert.assertEquals(s.arrangeCoins(d[0]), d[1]);
+ }
+ }
+}
diff --git a/[0442][Find All Duplicates in an Array]/src/Solution.java b/[0442][Find All Duplicates in an Array]/src/Solution.java
index 5582095..15e0aea 100644
--- a/[0442][Find All Duplicates in an Array]/src/Solution.java
+++ b/[0442][Find All Duplicates in an Array]/src/Solution.java
@@ -1,9 +1,4 @@
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
/**
* @author: wangjunchao(王俊超)
@@ -19,7 +14,7 @@ public List findDuplicates(int[] nums) {
Set set = new HashSet<>();
- for(int i: nums) {
+ for (int i : nums) {
if (set.contains(i)) {
result.add(i);
} else {
diff --git a/[0442][Find All Duplicates in an Array]/src/Solution2.java b/[0442][Find All Duplicates in an Array]/src/Solution2.java
index 5616c28..3a26109 100644
--- a/[0442][Find All Duplicates in an Array]/src/Solution2.java
+++ b/[0442][Find All Duplicates in an Array]/src/Solution2.java
@@ -31,7 +31,7 @@ public List findDuplicates(int[] nums) {
result.add(nums[i - 1]);
}
}
-
+
Collections.sort(result);
return result;
}
diff --git a/[0443][String Compression]/[0443][String Compression].iml b/[0443][String Compression]/[0443][String Compression].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0443][String Compression]/[0443][String Compression].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0443][String Compression]/src/Solution.java b/[0443][String Compression]/src/Solution.java
new file mode 100644
index 0000000..487f236
--- /dev/null
+++ b/[0443][String Compression]/src/Solution.java
@@ -0,0 +1,76 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-26 07:29
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 从向前处理
+ *
+ * @param chars
+ * @return
+ */
+ public int compress(char[] chars) {
+ if (chars == null || chars.length == 0) {
+ return 0;
+ }
+
+ int end;
+ int start = 0;
+ int length = 0;
+ while (start < chars.length) {
+ end = start + 1;
+ while (end < chars.length && chars[start] == chars[end]) {
+ end++;
+ }
+
+ String num = "" + (end - start);
+
+ length += 1;
+ if (end - start > 1) { // 有重复的字符
+ length += num.length(); // 重复字符的个数
+ int tmp = start + 1;
+ for (int i = 0; i < num.length(); i++) {
+ chars[tmp] = num.charAt(i);
+ tmp++;
+ }
+
+ while (tmp < end) { // 不需要的字符清零
+ chars[tmp] = 0;
+ tmp++;
+ }
+ }
+
+ start = end; // 开始处理下一个字符
+
+ }
+
+
+ // 找第一个0值的字符位置
+ start = 0;
+ while (start < chars.length && chars[start] != 0) {
+ start++;
+ }
+
+ end = start;
+ while (end < chars.length) {
+ // 找start位置之后第一个值不为0的位置
+ while (end < chars.length && chars[end] == 0) {
+ end++;
+ }
+
+ // 将end及之后不为0的值向前移动
+ while (end < chars.length && chars[end] != 0) {
+ chars[start] = chars[end];
+ start++;
+ end++;
+ }
+
+ }
+
+
+ return length;
+ }
+}
diff --git a/[0443][String Compression]/src/SolutionTest.java b/[0443][String Compression]/src/SolutionTest.java
new file mode 100644
index 0000000..278614c
--- /dev/null
+++ b/[0443][String Compression]/src/SolutionTest.java
@@ -0,0 +1,41 @@
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-26 07:51
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+ @Test
+ public void test1() {
+ Solution s = new Solution();
+ Object[][] data = {
+// {null,null, 0},
+// {"","", 0},
+// {"a","a", 1},
+// {"aabbccc", "a2b2c3", 6},
+// {"abbbbbbbbbbbb","ab12", 4},
+// {"abcdefg","abcdefg", 7},
+// {"aaab","a3b", 3},
+// {"aaabcccc","a3bc4", 5},
+ {"aaabbbccc", "a3b3c3", 6},
+ };
+
+ for (Object[] d : data) {
+
+ if (d[0] == null) {
+ Assert.assertNull(d[1]);
+ Assert.assertEquals(0, d[2]);
+ continue;
+ }
+
+ char[] arr = ((String) (d[0])).toCharArray();
+ int length = s.compress(arr);
+ Assert.assertEquals(length, d[2]);
+ Assert.assertEquals(new String(arr, 0, length), d[1]);
+ }
+ }
+}
diff --git a/[0447][Number of Boomerangs]/[0447][Number of Boomerangs].iml b/[0447][Number of Boomerangs]/[0447][Number of Boomerangs].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0447][Number of Boomerangs]/[0447][Number of Boomerangs].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0447][Number of Boomerangs]/src/Solution.java b/[0447][Number of Boomerangs]/src/Solution.java
new file mode 100644
index 0000000..102a1df
--- /dev/null
+++ b/[0447][Number of Boomerangs]/src/Solution.java
@@ -0,0 +1,69 @@
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-26 16:47
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int numberOfBoomerangs(int[][] points) {
+ int count = 0;
+ if (points == null || points.length < 2) {
+ return count;
+ }
+ Map map = new HashMap<>();
+
+ for (int i = 0; i < points.length; i++) {
+ for (int j = 0; j < points.length; j++) {
+
+ if (i == j) {
+ continue;
+ }
+ int square = square(points[i], points[j]);
+ if (!map.containsKey(square)) {
+ map.put(square, 1);
+ } else {
+ count += map.get(square) * 2; // A
+ int oldCount = map.get(square);
+ map.put(square, oldCount + 1);
+ }
+ }
+
+ // 如果不在A处理,那么对于大于1的value,取组合值
+ // C(2, n) * 2 = 1*2*...*(n-2)*2
+ // 放在A处理进行了分散处理
+
+ map.clear();
+ }
+
+ return count;
+
+ }
+
+ public int numberOfBoomerangsSlow(int[][] points) {
+
+ int answer = 0;
+ for (int i = 0; i < points.length; i++) {
+ for (int j = i + 1; j < points.length; j++) {
+ for (int k = 0; k < points.length; k++) {
+ if (k != i && k != j && square(points[k], points[i]) == square(points[k], points[j])) {
+ answer += 2;
+ }
+ }
+ }
+ }
+
+ return answer;
+ }
+
+ public int square(int[] a, int[] b) {
+ int x = a[0] - b[0];
+ int y = a[1] - b[1];
+
+ return x * x + y * y;
+ }
+
+}
diff --git a/[0447][Number of Boomerangs]/src/SolutionTest.java b/[0447][Number of Boomerangs]/src/SolutionTest.java
new file mode 100644
index 0000000..b961285
--- /dev/null
+++ b/[0447][Number of Boomerangs]/src/SolutionTest.java
@@ -0,0 +1,26 @@
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-26 16:55
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+ @Test
+ public void test1() {
+ Solution s = new Solution();
+ Object[][] data = {
+// {new int[][]{{0, 0}, {1, 0}, {2, 0}}, 2},
+// {new int[][]{{1, 1}, {2, 2}, {3, 3}}, 2},
+ {new int[][]{{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}}, 20}
+ };
+
+ for (Object[] d : data) {
+ Assert.assertEquals(d[1], s.numberOfBoomerangs((int[][]) d[0]));
+ }
+
+ }
+}
diff --git a/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml b/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java b/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java
new file mode 100644
index 0000000..395fde3
--- /dev/null
+++ b/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java
@@ -0,0 +1,40 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-28 21:38
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 解题思路:
+ * 假设数组最小的值是xmin,一共加了k次,加完后的值是W,元素个数是n
+ * 则:W <= xmin + k
+ * k(n-1) + sum(nums) = W*n
+ * ==>
+ * k(n-1) + sum(nums) <= (xmin + k)n
+ * ==>
+ * sum(nums) - n*xmin <= k
+ *
+ * @param nums
+ * @return
+ */
+ public int minMoves(int[] nums) {
+
+ if (nums == null) {
+ return 0;
+ }
+
+ int sum = 0;
+ int min = Integer.MAX_VALUE;
+
+ for (int i : nums) {
+ sum += i;
+ if (i < min) {
+ min = i;
+ }
+ }
+
+ return sum - min * nums.length;
+ }
+}
diff --git a/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java b/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java
new file mode 100644
index 0000000..1b8b239
--- /dev/null
+++ b/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java
@@ -0,0 +1,27 @@
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.lang.annotation.Target;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-28 22:09
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+ @Test
+ public void test1() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{1, 2, 3}, 3},
+ {new int[]{1, 1, 1}, 0},
+ {new int[]{1, 1, 2}, 1},
+ };
+
+ for (Object[] d : data) {
+ Assert.assertEquals(s.minMoves((int[]) d[0]), d[1]);
+ }
+ }
+}
diff --git a/[0455][Assign Cookies]/src/Solution.java b/[0455][Assign Cookies]/src/Solution.java
index 9d3bcaf..58f164e 100644
--- a/[0455][Assign Cookies]/src/Solution.java
+++ b/[0455][Assign Cookies]/src/Solution.java
@@ -2,6 +2,7 @@
/**
* https://leetcode.com/problems/assign-cookies/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-10 08:30
**/
@@ -41,6 +42,7 @@ public class Solution {
* 思路:
* 先将饼干尺寸和小孩需求都排个序,然后从小到大去遍历地给。
*
+ *
* @param g
* @param s
* @return
diff --git a/[0461][Hamming Distance]/src/Solution.java b/[0461][Hamming Distance]/src/Solution.java
index 78490f0..7204175 100644
--- a/[0461][Hamming Distance]/src/Solution.java
+++ b/[0461][Hamming Distance]/src/Solution.java
@@ -26,6 +26,7 @@ public class Solution {
*
* The above arrows point to positions where the corresponding bits are different.
*
+ *
* @param x
* @param y
* @return
diff --git a/[0463][Island Perimeter]/src/Solution2.java b/[0463][Island Perimeter]/src/Solution2.java
index 498b571..bde8363 100644
--- a/[0463][Island Perimeter]/src/Solution2.java
+++ b/[0463][Island Perimeter]/src/Solution2.java
@@ -29,6 +29,7 @@ public class Solution2 {
*
* Explanation: The perimeter is the 16 yellow stripes in the image below:
*
+ *
* @param grid
* @return
*/
diff --git a/[0475][Heaters]/src/Solution.java b/[0475][Heaters]/src/Solution.java
index 39b8c0b..e353380 100644
--- a/[0475][Heaters]/src/Solution.java
+++ b/[0475][Heaters]/src/Solution.java
@@ -2,6 +2,7 @@
/**
* https://leetcode.com/problems/heaters/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-10 08:58
**/
diff --git a/[0476][Number Complement]/src/Solution.java b/[0476][Number Complement]/src/Solution.java
index c509d85..a992057 100644
--- a/[0476][Number Complement]/src/Solution.java
+++ b/[0476][Number Complement]/src/Solution.java
@@ -22,6 +22,7 @@ public class Solution {
* Explanation: The binary representation of 1 is 1 (no leading zero bits), and its
* complement is 0. So you need to output 0.
*
+ *
* @param num
* @return
*/
diff --git a/[0482][License Key Formatting]/src/Solution.java b/[0482][License Key Formatting]/src/Solution.java
index 73ec646..d9e9120 100644
--- a/[0482][License Key Formatting]/src/Solution.java
+++ b/[0482][License Key Formatting]/src/Solution.java
@@ -38,6 +38,7 @@ public class Solution {
* 思路:
* 从后向前处理,每k个分成一个组
*
+ *
* @param s
* @param k
* @return
@@ -74,6 +75,6 @@ public String licenseKeyFormatting(String s, int k) {
return "";
}
- return result.charAt(0) == '-' ? result.substring(1) : result;
+ return result.charAt(0) == '-' ? result.substring(1) : result;
}
}
diff --git a/[0492][Construct the Rectangle]/src/Solution.java b/[0492][Construct the Rectangle]/src/Solution.java
index d929935..834f7ce 100644
--- a/[0492][Construct the Rectangle]/src/Solution.java
+++ b/[0492][Construct the Rectangle]/src/Solution.java
@@ -28,6 +28,7 @@ public class Solution {
* The given area won't exceed 10,000,000 and is a positive integer
* The web page's width and length you designed must be positive integers.
*
+ *
* @param area
* @return
*/
diff --git a/[0500][Keyboard Row]/src/Solution.java b/[0500][Keyboard Row]/src/Solution.java
index 39dbb1b..d0cf17b 100644
--- a/[0500][Keyboard Row]/src/Solution.java
+++ b/[0500][Keyboard Row]/src/Solution.java
@@ -1,9 +1,9 @@
-
import java.util.LinkedList;
import java.util.List;
/**
* https://leetcode.com/problems/keyboard-row/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-11 10:25
**/
diff --git a/[0501][Find Mode in Binary Search Tree]/src/Solution.java b/[0501][Find Mode in Binary Search Tree]/src/Solution.java
index af9abc6..5b104f5 100644
--- a/[0501][Find Mode in Binary Search Tree]/src/Solution.java
+++ b/[0501][Find Mode in Binary Search Tree]/src/Solution.java
@@ -1,8 +1,4 @@
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
+import java.util.*;
/**
* @author: wangjunchao(王俊超)
@@ -15,14 +11,14 @@ public int[] findMode(TreeNode root) {
findMode(root, map);
int max = 0;
- for (int v: map.values()) {
+ for (int v : map.values()) {
if (max < v) {
max = v;
}
}
List list = new LinkedList<>();
- for(Map.Entry e: map.entrySet()) {
+ for (Map.Entry e : map.entrySet()) {
if (e.getValue() == max) {
list.add(e.getKey());
}
@@ -30,7 +26,7 @@ public int[] findMode(TreeNode root) {
int[] result = new int[list.size()];
int i = 0;
- for (int v: list) {
+ for (int v : list) {
result[i] = v;
i++;
}
diff --git a/[0501][Find Mode in Binary Search Tree]/src/TreeNode.java b/[0501][Find Mode in Binary Search Tree]/src/TreeNode.java
index 8e2131e..8b88e24 100644
--- a/[0501][Find Mode in Binary Search Tree]/src/TreeNode.java
+++ b/[0501][Find Mode in Binary Search Tree]/src/TreeNode.java
@@ -1,4 +1,3 @@
-
/**
* Author: 王俊超
* Date: 2015-08-21
diff --git a/[0504][Base 7]/src/Solution.java b/[0504][Base 7]/src/Solution.java
index 8eba5e2..62947d6 100644
--- a/[0504][Base 7]/src/Solution.java
+++ b/[0504][Base 7]/src/Solution.java
@@ -15,6 +15,7 @@ public class Solution {
* Output: "-10"
* Note: The input will be in range of [-1e7, 1e7].
*
+ *
* @param num
* @return
*/
diff --git a/[0506][Relative Ranks]/[0506][Relative Ranks].iml b/[0506][Relative Ranks]/[0506][Relative Ranks].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0506][Relative Ranks]/[0506][Relative Ranks].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0506][Relative Ranks]/src/Solution.java b/[0506][Relative Ranks]/src/Solution.java
new file mode 100644
index 0000000..3cc25f6
--- /dev/null
+++ b/[0506][Relative Ranks]/src/Solution.java
@@ -0,0 +1,139 @@
+import java.util.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 07:34
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+
+ class Rank {
+ Rank(int s, int i) {
+ this.Score = s;
+ this.Index = i;
+ }
+ int Score;
+ int Index;
+ }
+
+ /**
+ * 题解思路:
+ * 1、记录分数对应的下标;
+ * 2、对分数进行排序,求行排名
+ * 3、再根据分数将原始数组的下标和排名对应起来
+ *
+ * @param nums
+ * @return
+ */
+ public String[] findRelativeRanks(int[] nums) {
+ if (nums == null) {
+ return null;
+ }
+
+ Rank[] ranks = new Rank[nums.length];
+ for (int i = 0; i < nums.length; i++) {
+ ranks[i] = new Rank(nums[i], i);
+ }
+
+ Arrays.sort(ranks, new Comparator() {
+ @Override
+ public int compare(Rank o1, Rank o2) {
+ return -(o1.Score - o2.Score);
+ }
+ });
+
+ String[] result = new String[nums.length];
+ for (int i = 0; i < ranks.length; i++) {
+ switch (i) {
+ case 0:
+ result[ranks[i].Index] = "Gold Medal";
+ break;
+ case 1:
+ result[ranks[i].Index] = "Silver Medal";
+ break;
+
+ case 2:
+ result[ranks[i].Index] = "Bronze Medal";
+ break;
+ default:
+ result[ranks[i].Index] = "" + (i + 1);
+ }
+ }
+ return result;
+ }
+
+ public String[] findRelativeRanks3(int[] nums) {
+ if (nums == null) {
+ return null;
+ }
+
+ Map map = new HashMap<>();
+ Queue queue = new PriorityQueue<>(nums.length + 1); // 使用优先堆,加1是为了防止参数为0
+
+ for (int i = 0; i < nums.length; i++) {
+ map.put(nums[i], i);
+ queue.add(nums[i]); // 创建小顶堆
+ }
+
+
+ String[] result = new String[nums.length];
+ int i = nums.length + 1;
+ while (!queue.isEmpty()) {
+ int score = queue.poll();
+ i--;
+ switch (i) {
+ case 1:
+ result[map.get(score)] = "Gold Medal";
+ break;
+ case 2:
+ result[map.get(score)] = "Silver Medal";
+ break;
+ case 3:
+ result[map.get(score)] = "Bronze Medal";
+ break;
+ default:
+ result[map.get(score)] = "" + i;
+ }
+
+ }
+
+ return result;
+ }
+
+ public String[] findRelativeRanks2(int[] nums) {
+ if (nums == null) {
+ return null;
+ }
+
+ Map map = new HashMap<>();
+
+ for (int i = 0; i < nums.length; i++) {
+ map.put(nums[i], i);
+ }
+
+ String[] result = new String[nums.length];
+ Arrays.sort(nums);
+
+ for (int i = 0; i < nums.length; i++) {
+ int score = nums[nums.length - 1 - i];
+ switch (i) {
+ case 0:
+ result[map.get(score)] = "Gold Medal";
+ break;
+ case 1:
+ result[map.get(score)] = "Silver Medal";
+ break;
+
+ case 2:
+ result[map.get(score)] = "Bronze Medal";
+ break;
+ default:
+ result[map.get(score)] = "" + (i + 1);
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/[0506][Relative Ranks]/src/SolutionTest.java b/[0506][Relative Ranks]/src/SolutionTest.java
new file mode 100644
index 0000000..6af869f
--- /dev/null
+++ b/[0506][Relative Ranks]/src/SolutionTest.java
@@ -0,0 +1,25 @@
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 07:46
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @Test
+ public void test1() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{5, 4, 3, 2, 1}, new String[]{"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"}},
+ {new int[]{1, 2, 3, 4, 5}, new String[]{"5", "4", "Bronze Medal", "Silver Medal", "Gold Medal"}},
+ };
+
+ for (Object[] d: data) {
+ Assert.assertArrayEquals(s.findRelativeRanks((int[]) d[0]), (Object[]) d[1]);
+ }
+ }
+}
diff --git a/[0507][Perfect Number]/[0507][Perfect Number].iml b/[0507][Perfect Number]/[0507][Perfect Number].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0507][Perfect Number]/[0507][Perfect Number].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0507][Perfect Number]/src/Solution.java b/[0507][Perfect Number]/src/Solution.java
new file mode 100644
index 0000000..036581e
--- /dev/null
+++ b/[0507][Perfect Number]/src/Solution.java
@@ -0,0 +1,26 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 08:33
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public boolean checkPerfectNumber(int num) {
+ if (num < 6) {
+ return false;
+ }
+
+ int sum = 1;
+ int sqrt = (int) Math.sqrt(num);
+
+ for (int i = 2; i <= sqrt; i++) {
+ if (num % i == 0) {
+ sum += i;
+ sum += num / i;
+ }
+ }
+
+ return sum == num;
+ }
+}
diff --git a/[0507][Perfect Number]/src/SolutionTest.java b/[0507][Perfect Number]/src/SolutionTest.java
new file mode 100644
index 0000000..287f91d
--- /dev/null
+++ b/[0507][Perfect Number]/src/SolutionTest.java
@@ -0,0 +1,29 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 08:36
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void checkPerfectNumber() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {28, true},
+ {-1, false},
+ {0, false},
+ {1, false},
+ {6, true},
+ {7, false},
+ {8, false},
+ };
+
+ for (Object[] d: data) {
+ Assert.assertEquals(s.checkPerfectNumber((Integer) d[0]), d[1]);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml b/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0509][Fibonacci Number]/src/Solution.java b/[0509][Fibonacci Number]/src/Solution.java
new file mode 100644
index 0000000..4e5fa95
--- /dev/null
+++ b/[0509][Fibonacci Number]/src/Solution.java
@@ -0,0 +1,27 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 08:44
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int fib(int N) {
+ if (N <= 0) {
+ return 0;
+ } else if (N == 1) {
+ return 1;
+ }
+
+ int n2 = 0;
+ int n1 = 1;
+ int n0;
+ for (int i = 2; i <= N; i++) {
+ n0 = n1 + n2;
+ n2 = n1;
+ n1 = n0;
+ }
+
+ return n1;
+ }
+}
diff --git a/[0509][Fibonacci Number]/src/SolutionTest.java b/[0509][Fibonacci Number]/src/SolutionTest.java
new file mode 100644
index 0000000..c959dd8
--- /dev/null
+++ b/[0509][Fibonacci Number]/src/SolutionTest.java
@@ -0,0 +1,26 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 08:48
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void fib() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {0, 0},
+ {1, 1},
+ {2, 1},
+ {3, 2},
+ };
+
+ for (Object[] d : data) {
+ Assert.assertEquals(s.fib((Integer) d[0]), d[1]);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0520][Detect Capital]/[0520][Detect Capital].iml b/[0520][Detect Capital]/[0520][Detect Capital].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0520][Detect Capital]/[0520][Detect Capital].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0520][Detect Capital]/src/Solution.java b/[0520][Detect Capital]/src/Solution.java
new file mode 100644
index 0000000..c8c6c13
--- /dev/null
+++ b/[0520][Detect Capital]/src/Solution.java
@@ -0,0 +1,73 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 08:42
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ *
+ * Given a word, you need to judge whether the usage of capitals in it is right or not.
+ *
+ * We define the usage of capitals in a word to be right when one of the following
+ * cases holds:
+ *
+ * All letters in this word are capitals, like "USA".
+ * All letters in this word are not capitals, like "leetcode".
+ * Only the first letter in this word is capital, like "Google".
+ * Otherwise, we define that this word doesn't use capitals in a right way.
+ *
+ *
+ * Example 1:
+ *
+ * Input: "USA"
+ * Output: True
+ *
+ *
+ * Example 2:
+ *
+ * Input: "FlaG"
+ * Output: False
+ *
+ *
+ * Note: The input will be a non-empty word consisting of uppercase
+ * and lowercase latin letters.
+ *
+ *
+ * @param word
+ * @return
+ */
+ public boolean detectCapitalUse(String word) {
+
+ if (Character.isUpperCase(word.charAt(0))) {
+ return isAllUpperCase(word.substring(1)) || isAllLowerCase(word.substring(1));
+ } else if (Character.isLowerCase(word.charAt(0))) {
+ return isAllLowerCase(word.substring(1));
+ }
+
+ return false;
+ }
+
+ private boolean isAllLowerCase(String s) {
+ for (int i = 0; i < s.length(); i++) {
+ if (!Character.isLowerCase(s.charAt(i))) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private boolean isAllUpperCase(String s) {
+
+ for (int i = 0; i < s.length(); i++) {
+ if (!Character.isUpperCase(s.charAt(i))) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
+
diff --git a/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml b/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0521][Longest Uncommon Subsequence I]/src/Solution.java b/[0521][Longest Uncommon Subsequence I]/src/Solution.java
new file mode 100644
index 0000000..7f99841
--- /dev/null
+++ b/[0521][Longest Uncommon Subsequence I]/src/Solution.java
@@ -0,0 +1,20 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 08:55
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * @param a
+ * @param b
+ * @return
+ */
+ public int findLUSlength(String a, String b) {
+ if (a.equals(b)) {
+ return -1;
+ }
+ return Math.max(a.length(), b.length());
+ }
+}
diff --git a/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml b/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0523][K Diff Pairs In An Array]/src/Solution.java b/[0523][K Diff Pairs In An Array]/src/Solution.java
new file mode 100644
index 0000000..c461f31
--- /dev/null
+++ b/[0523][K Diff Pairs In An Array]/src/Solution.java
@@ -0,0 +1,40 @@
+import java.util.Arrays;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-30 08:52
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int findPairs(int[] nums, int k) {
+ if (nums == null || nums.length < 2 || k < 0) {
+ return 0;
+ }
+
+ int result = 0;
+
+ Arrays.sort(nums);
+
+ for (int i = 0; i < nums.length - 1;i++) {
+
+ for (int j = i + 1; j < nums.length && nums[j] - nums[i] <= k; j++) {
+ while (j + 1 < nums.length && nums[j] == nums[j + 1]) {
+ j++;
+ }
+
+ if (nums[j] - nums[i] == k) {
+ result++;
+ }
+ }
+
+ while (i + 1 < nums.length && nums[i] == nums[i + 1]) {
+ i++;
+ }
+
+ }
+
+ return result;
+ }
+}
diff --git a/[0523][K Diff Pairs In An Array]/src/SolutionTest.java b/[0523][K Diff Pairs In An Array]/src/SolutionTest.java
new file mode 100644
index 0000000..2e368fa
--- /dev/null
+++ b/[0523][K Diff Pairs In An Array]/src/SolutionTest.java
@@ -0,0 +1,27 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-30 08:56
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findPairs() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{3, 1, 4, 1, 5}, 2, 2},
+ {new int[]{1, 2, 3, 4, 5}, 1, 4},
+ {new int[]{1, 3, 1, 5, 4}, 0, 1},
+ {new int[]{1, 1, 1, 1, 1}, 0, 1},
+ {new int[]{1, 1, 2, 2, 1}, 0, 2},
+ };
+
+ for (Object[] d: data) {
+ Assert.assertEquals(d[2], s.findPairs((int[]) d[0], (Integer) d[1]));
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml b/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0530][Minimum Absolute Difference in BST]/src/Solution.java b/[0530][Minimum Absolute Difference in BST]/src/Solution.java
new file mode 100644
index 0000000..603e965
--- /dev/null
+++ b/[0530][Minimum Absolute Difference in BST]/src/Solution.java
@@ -0,0 +1,92 @@
+import java.util.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 10:09
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 解题思路
+ * root提个二叉搜索树
+ * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树:
+ * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
+ * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
+ * 它的左、右子树也分别为二叉排序树。
+ *
+ * @param root
+ * @return
+ */
+ public int getMinimumDifference(TreeNode root) {
+
+ if (root == null) {
+ return 0;
+ }
+ Deque s = new LinkedList<>();
+ int min = Integer.MAX_VALUE;
+ int prev = 0;
+ boolean first = true;
+ while (root != null || !s.isEmpty()) {
+ while (root != null) {
+ s.push(root);//先访问再入栈
+ root = root.left;
+ }
+ root = s.pop();
+
+ if (first) {
+ first = false;
+ } else {
+ min = Math.min(min, root.val - prev);
+ }
+
+ prev = root.val;
+ root = root.right;//如果是null,出栈并处理右子树
+ }
+
+ return min;
+ }
+
+ public int getMinimumDifference2(TreeNode root) {
+
+ if (root == null) {
+ return 0;
+ }
+
+ List list = new ArrayList<>();
+
+ Deque queue = new LinkedList<>();
+ queue.add(root);
+ while (!queue.isEmpty()) {
+ TreeNode node = queue.poll();
+ list.add(node.val);
+ if (node.right != null) {
+ queue.addFirst(node.right);
+ }
+ if (node.left != null) {
+ queue.addFirst(node.left);
+ }
+ }
+
+
+ if (list.size() == 1) {
+ return list.get(0);
+ }
+
+ list.sort(new Comparator() {
+ @Override
+ public int compare(Integer o1, Integer o2) {
+ return o1 - o2;
+ }
+ });
+
+ int min = Integer.MAX_VALUE;
+
+ for (int i = 1; i < list.size(); i++) {
+ min = Math.min(min, Math.abs(list.get(i - 1) - list.get(i)));
+ }
+
+ return min;
+ }
+}
diff --git a/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java b/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java
new file mode 100644
index 0000000..b0523af
--- /dev/null
+++ b/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java
@@ -0,0 +1,35 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 11:33
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void getMinimumDifference() {
+ TreeNode root = new TreeNode(1);
+ root.right = new TreeNode(3);
+ root.right.left = new TreeNode(2);
+
+ Solution s = new Solution();
+ Assert.assertEquals(s.getMinimumDifference(root), 1);
+ }
+
+// [236,104,701,null,227,null,911]
+
+ @org.junit.Test
+ public void getMinimumDifference2() {
+ TreeNode root = new TreeNode(236);
+ root.left = new TreeNode(104);
+ root.left.right = new TreeNode(227);
+ root.right = new TreeNode(701);
+ root.right.right = new TreeNode(911);
+
+ Solution s = new Solution();
+ Assert.assertEquals(s.getMinimumDifference(root), 9);
+ }
+}
\ No newline at end of file
diff --git a/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java b/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java
new file mode 100644
index 0000000..bff99a6
--- /dev/null
+++ b/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java
@@ -0,0 +1,16 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 11:06
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
diff --git a/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml b/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0538][Convert BST to Greater Tree]/src/Solution.java b/[0538][Convert BST to Greater Tree]/src/Solution.java
new file mode 100644
index 0000000..0e52f13
--- /dev/null
+++ b/[0538][Convert BST to Greater Tree]/src/Solution.java
@@ -0,0 +1,52 @@
+import java.util.Deque;
+import java.util.LinkedList;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-30 10:06
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 解题思路
+ * root提个二叉搜索树
+ * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树:
+ * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
+ * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
+ * 它的左、右子树也分别为二叉排序树。
+ * 先处理右子树,再处理自己,再处理左子树
+ *
+ * @param root
+ * @return
+ */
+ public TreeNode convertBST(TreeNode root) {
+
+ if (root == null) {
+ return null;
+ }
+ Deque s = new LinkedList<>();
+ TreeNode prev = null;
+ TreeNode curr = root;
+
+ while (curr != null || !s.isEmpty()) {
+ while (curr != null) {
+ s.push(curr);//先访问再入栈
+ curr = curr.right;
+ }
+ curr = s.pop();
+
+ if (prev == null) {
+ prev = curr;
+ } else {
+ curr.val += prev.val;
+ }
+
+ prev = curr;
+ curr = curr.left;//如果是null,出栈并处理左子树
+ }
+
+ return root;
+ }
+}
diff --git a/[0538][Convert BST to Greater Tree]/src/SolutionTest.java b/[0538][Convert BST to Greater Tree]/src/SolutionTest.java
new file mode 100644
index 0000000..5b181bf
--- /dev/null
+++ b/[0538][Convert BST to Greater Tree]/src/SolutionTest.java
@@ -0,0 +1,39 @@
+import static org.junit.Assert.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-30 10:14
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void convertBST() {
+ Solution s = new Solution();
+
+ TreeNode root = new TreeNode(4);
+ root.left = new TreeNode(2);
+ root.left.left = new TreeNode(1);
+ root.left.right = new TreeNode(3);
+ root.right =new TreeNode(6);
+ root.right.left = new TreeNode(5);
+ root.right.right = new TreeNode(7);
+
+ s.convertBST(root);
+
+ print(root);
+ }
+
+ private void print(TreeNode root) {
+ if (root == null) {
+ return;
+ }
+
+ print(root.left);
+ System.out.println(root.val);
+ print(root.right);
+
+ }
+}
\ No newline at end of file
diff --git a/[0538][Convert BST to Greater Tree]/src/TreeNode.java b/[0538][Convert BST to Greater Tree]/src/TreeNode.java
new file mode 100644
index 0000000..bff99a6
--- /dev/null
+++ b/[0538][Convert BST to Greater Tree]/src/TreeNode.java
@@ -0,0 +1,16 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 11:06
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
diff --git a/[0541][Reverse String II]/[0541][Reverse String II].iml b/[0541][Reverse String II]/[0541][Reverse String II].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0541][Reverse String II]/[0541][Reverse String II].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0541][Reverse String II]/src/Solution.java b/[0541][Reverse String II]/src/Solution.java
new file mode 100644
index 0000000..df9a379
--- /dev/null
+++ b/[0541][Reverse String II]/src/Solution.java
@@ -0,0 +1,32 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-30 12:50
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public String reverseStr(String s, int k) {
+ if (s == null || s.length() < 2 || k < 1) {
+ return s;
+ }
+ char[] chars = s.toCharArray();
+ int index = 0;
+ while (index < chars.length) {
+ swap(chars, index, Math.min(chars.length - 1, index + k - 1));
+ index += 2 * k;
+ }
+
+ return new String(chars);
+ }
+
+ public void swap(char[] chars, int i, int j) {
+ while (i < j) {
+ char ch = chars[i];
+ chars[i] = chars[j];
+ chars[j] = ch;
+ i++;
+ j--;
+ }
+ }
+}
diff --git a/[0541][Reverse String II]/src/SolutionTest.java b/[0541][Reverse String II]/src/SolutionTest.java
new file mode 100644
index 0000000..373a1d1
--- /dev/null
+++ b/[0541][Reverse String II]/src/SolutionTest.java
@@ -0,0 +1,24 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-30 12:54
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void reverseStr() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {"abcdefg", 2, "bacdfeg"},
+ {"abcdefgh", 2, "bacdfegh"},
+ };
+
+ for (Object[] d : data) {
+ Assert.assertEquals(d[2], s.reverseStr((String) d[0], (Integer) d[1]));
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml b/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0543][Diameter of Binary Tree]/src/Solution.java b/[0543][Diameter of Binary Tree]/src/Solution.java
new file mode 100644
index 0000000..5bca04a
--- /dev/null
+++ b/[0543][Diameter of Binary Tree]/src/Solution.java
@@ -0,0 +1,59 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-01 08:53
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int diameterOfBinaryTree(TreeNode root) {
+ return layerAndDiameter(root)[1];
+ }
+
+ /**
+ * 解题思路:
+ * 最大直径的三种情况:
+ * 1、在左子树中
+ * 2、在右子树中
+ * 3、对过root节点,如果经过root节点,那么直径就是 Height(root.left) + Height(root.right) + 2
+ * 对每个节点都是同样的。所以可以使用递归求解,先求左右子树的高度和最大直径,再求通过root的高度和直径,再找出最大的直径
+ *
+ *
+ * 第一个反回参数表示root最大有多少层,
+ * 第二个返回参数表示root的最大直径
+ *
+ * @param root
+ * @return
+ */
+ public int[] layerAndDiameter(TreeNode root) {
+ if (root == null) { // 边界条件
+ return new int[]{0, 0};
+ }
+
+ if (root.left == null && root.right == null) { // 边界条件
+ return new int[]{0, 0};
+ } else if (root.left != null && root.right == null) {
+ int[] result = layerAndDiameter(root.left);
+ result[0] += 1; // 层升高1
+ result[1] = Math.max(result[0], result[1]); // 因为没有右子树,最大直径要么经过root,要么不经过
+
+ return result;
+ } else if (root.left == null && root.right != null) {
+ int[] result = layerAndDiameter(root.right);
+ result[0] += 1; // 层升高1
+ result[1] = Math.max(result[0], result[1]); // 因为没有左子树,最大直径要么经过root,要么不经过
+ return result;
+ }
+
+ int[] left = layerAndDiameter(root.left);
+ int[] right = layerAndDiameter(root.right);
+
+ int[] result = {0, 0};
+ result[0] = 1 + Math.max(left[0], right[0]); // 层升高1
+
+ result[1] = Math.max(left[1], right[1]); // 最大直径可能在左子树或者右子树中
+ result[1] = Math.max(result[1], 2 + left[0] + right[0]); // 最大直径可能经过root
+
+ return result;
+ }
+}
diff --git a/[0543][Diameter of Binary Tree]/src/SolutionTest.java b/[0543][Diameter of Binary Tree]/src/SolutionTest.java
new file mode 100644
index 0000000..c3cab02
--- /dev/null
+++ b/[0543][Diameter of Binary Tree]/src/SolutionTest.java
@@ -0,0 +1,25 @@
+import org.junit.Assert;
+
+import static org.junit.Assert.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-01 08:57
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void diameterOfBinaryTree() {
+ Solution s = new Solution();
+ TreeNode root = new TreeNode(1);
+ root.left = new TreeNode(2);
+ root.right = new TreeNode(3);
+ root.left.left = new TreeNode(4);
+ root.left.right = new TreeNode(5);
+
+ Assert.assertEquals(3, s.diameterOfBinaryTree(root));
+ }
+}
\ No newline at end of file
diff --git a/[0543][Diameter of Binary Tree]/src/TreeNode.java b/[0543][Diameter of Binary Tree]/src/TreeNode.java
new file mode 100644
index 0000000..bff99a6
--- /dev/null
+++ b/[0543][Diameter of Binary Tree]/src/TreeNode.java
@@ -0,0 +1,16 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 11:06
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
diff --git a/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml b/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0551][Student Attendance Record I]/src/Solution.java b/[0551][Student Attendance Record I]/src/Solution.java
new file mode 100644
index 0000000..3ca6969
--- /dev/null
+++ b/[0551][Student Attendance Record I]/src/Solution.java
@@ -0,0 +1,41 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 08:05
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public boolean checkRecord(String s) {
+ int a = 0;
+ int l = 0;
+ int preLIndex = -1;
+ if (s != null) {
+ for (int i = 0; i < s.length(); i++) {
+ switch (s.charAt(i)) {
+ case 'A':
+ a++;
+ break;
+ case 'L':
+ if (l < 3) { // 连续两个L后面步不用记录了
+ if (preLIndex == -1) { // 刚开始记录
+ preLIndex = i;
+ l = 1;
+ } else if (preLIndex + 1 == i) { // 当前和前一个都是L
+ l++;
+ preLIndex = i;
+ } else { // 前一个不是L,重新记数
+ preLIndex = i;
+ l = 1;
+ }
+ }
+ break;
+ default:
+ // do nothing
+ }
+ }
+ }
+
+ return a <= 1 && l < 3;
+ }
+}
diff --git a/[0551][Student Attendance Record I]/src/SolutionTest.java b/[0551][Student Attendance Record I]/src/SolutionTest.java
new file mode 100644
index 0000000..4c102f4
--- /dev/null
+++ b/[0551][Student Attendance Record I]/src/SolutionTest.java
@@ -0,0 +1,27 @@
+import org.junit.Assert;
+import org.junit.Test;
+
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 08:12
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @Test
+ public void checkRecord() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {"PPALLP", true},
+ {"PPALLL", false},
+ };
+
+ for (Object[] d : data) {
+ Assert.assertEquals(d[1], s.checkRecord((String) d[0]));
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml b/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0559][Maximum Depth of N-ary Tree]/src/Node.java b/[0559][Maximum Depth of N-ary Tree]/src/Node.java
new file mode 100644
index 0000000..bf5b87a
--- /dev/null
+++ b/[0559][Maximum Depth of N-ary Tree]/src/Node.java
@@ -0,0 +1,25 @@
+import java.util.List;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 07:58
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Node {
+ public int val;
+ public List children;
+
+ public Node() {
+ }
+
+ public Node(int val) {
+ this.val = val;
+ }
+
+ public Node(int val, List children) {
+ this.val = val;
+ this.children = children;
+ }
+}
diff --git a/[0559][Maximum Depth of N-ary Tree]/src/Solution.java b/[0559][Maximum Depth of N-ary Tree]/src/Solution.java
new file mode 100644
index 0000000..d43fa08
--- /dev/null
+++ b/[0559][Maximum Depth of N-ary Tree]/src/Solution.java
@@ -0,0 +1,27 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 07:58
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int maxDepth(Node root) {
+ if (root == null) {
+ return 0;
+ }
+
+ if (root.children == null || root.children.isEmpty()) {
+ return 1;
+ }
+
+ int max = 0;
+
+ for (Node child : root.children) {
+ max = Math.max(max, maxDepth(child));
+ }
+
+ return 1 + max;
+ }
+}
+
diff --git a/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java b/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java
new file mode 100644
index 0000000..4aebf27
--- /dev/null
+++ b/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java
@@ -0,0 +1,31 @@
+import org.junit.Assert;
+
+import java.util.ArrayList;
+
+import static org.junit.Assert.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 08:00
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void maxDepth() {
+ Node root = new Node(1);
+ root.children = new ArrayList<>();
+ root.children.add(new Node(3));
+ root.children.add(new Node(2));
+ root.children.add(new Node(4));
+ root.children.get(0).children = new ArrayList<>();
+ root.children.get(0).children.add(new Node(5));
+ root.children.get(0).children.add(new Node(6));
+
+ Solution s =new Solution();
+
+ Assert.assertEquals(3, s.maxDepth(root));
+ }
+}
\ No newline at end of file
diff --git a/[0561][Array Partition I]/src/Main.java b/[0561][Array Partition I]/src/Main.java
index 92bec11..9deca22 100644
--- a/[0561][Array Partition I]/src/Main.java
+++ b/[0561][Array Partition I]/src/Main.java
@@ -7,9 +7,9 @@
**/
public class Main {
@Test
- public void test1(){
+ public void test1() {
Solution solution = new Solution();
- int[] arr = {1,4,3,2};
+ int[] arr = {1, 4, 3, 2};
Assert.assertEquals(4, solution.arrayPairSum(arr));
}
}
diff --git a/[0561][Array Partition I]/src/Solution.java b/[0561][Array Partition I]/src/Solution.java
index 4c41f06..7169b92 100644
--- a/[0561][Array Partition I]/src/Solution.java
+++ b/[0561][Array Partition I]/src/Solution.java
@@ -2,6 +2,7 @@
/**
* https://leetcode.com/problems/array-partition-i/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-04 08:46
**/
@@ -25,6 +26,7 @@ public class Solution {
* 先将数据组排序,取偶数位置的数相加,即为结果
* 证明略
*
+ *
* @param nums
* @return
*/
@@ -32,7 +34,7 @@ public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int result = 0;
- for (int i = 0; i < nums.length; i+=2) {
+ for (int i = 0; i < nums.length; i += 2) {
result += nums[i];
}
diff --git a/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml b/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0563][Binary Tree Tilt]/src/Solution.java b/[0563][Binary Tree Tilt]/src/Solution.java
new file mode 100644
index 0000000..a3ae597
--- /dev/null
+++ b/[0563][Binary Tree Tilt]/src/Solution.java
@@ -0,0 +1,47 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 08:26
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 解题思路
+ * 一个树的坡度等于=左子树的坡度+右子树的坡度+根节点的坡度
+ * 子树同样适应
+ *
+ * @param root
+ * @return
+ */
+ public int findTilt(TreeNode root) {
+ return findTiltHelp(root)[0];
+ }
+
+ /**
+ * 第一个返回值表示root的坡度
+ * 第二个返回值表示root的为根的树的节点和
+ *
+ * @param root
+ * @return
+ */
+ public int[] findTiltHelp(TreeNode root) {
+ // 空结点或者左右孩子为都空的结点
+ if (root == null) {
+ return new int[]{0, 0};
+ } else if (root.left == null && root.right == null) {
+ return new int[]{0, root.val};
+ }
+
+
+ int[] left = findTiltHelp(root.left);
+ int[] right = findTiltHelp(root.right);
+
+ int[] result = {0, 0};
+
+ result[0] = left[0] + right[0] + Math.abs(left[1] - right[1]);
+ result[1] = left[1] + right[1] + root.val;
+
+ return result;
+ }
+}
diff --git a/[0563][Binary Tree Tilt]/src/SolutionTest.java b/[0563][Binary Tree Tilt]/src/SolutionTest.java
new file mode 100644
index 0000000..358cd67
--- /dev/null
+++ b/[0563][Binary Tree Tilt]/src/SolutionTest.java
@@ -0,0 +1,22 @@
+import org.junit.Assert;
+
+import static org.junit.Assert.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 08:38
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findTilt() {
+ Solution s = new Solution();
+ TreeNode root = new TreeNode(1);
+ root.left = new TreeNode(2);
+ root.right = new TreeNode(3);
+ Assert.assertEquals(1, s.findTilt(root));
+ }
+}
\ No newline at end of file
diff --git a/[0563][Binary Tree Tilt]/src/TreeNode.java b/[0563][Binary Tree Tilt]/src/TreeNode.java
new file mode 100644
index 0000000..8b88e24
--- /dev/null
+++ b/[0563][Binary Tree Tilt]/src/TreeNode.java
@@ -0,0 +1,16 @@
+/**
+ * Author: 王俊超
+ * Date: 2015-08-21
+ * Time: 18:45
+ * Declaration: All Rights Reserved !!!
+ */
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
+
diff --git a/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml b/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0566][Reshape the Matrix]/src/Solution.java b/[0566][Reshape the Matrix]/src/Solution.java
new file mode 100644
index 0000000..f4516d8
--- /dev/null
+++ b/[0566][Reshape the Matrix]/src/Solution.java
@@ -0,0 +1,29 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 08:43
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int[][] matrixReshape(int[][] nums, int r, int c) {
+ int row = nums.length;
+ int col = nums[0].length;
+ if (row * col != r * c) {
+ return nums;
+ }
+
+ int[][] result = new int[r][c];
+ for (int i = 0; i < r; i++) {
+ result[i] = new int[c];
+ }
+
+ int num = row * col;
+
+ for (int i = 0; i < num; i++) {
+ result[i / c][i % c] = nums[i / col][i % col];
+ }
+
+ return result;
+ }
+}
diff --git a/[0566][Reshape the Matrix]/src/SolutionTest.java b/[0566][Reshape the Matrix]/src/SolutionTest.java
new file mode 100644
index 0000000..d21e1f9
--- /dev/null
+++ b/[0566][Reshape the Matrix]/src/SolutionTest.java
@@ -0,0 +1,32 @@
+import org.junit.Assert;
+
+import static org.junit.Assert.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 08:46
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void matrixReshape() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[][]{{1, 2}, {3, 4}}, 1, 4, new int[][]{{1, 2, 3, 4}}},
+ {new int[][]{{1, 2}, {3, 4}}, 2, 4, new int[][]{{1, 2}, {3, 4}}},
+ };
+
+ for (Object[] d : data) {
+ int[][] result = s.matrixReshape((int[][]) d[0], (Integer) d[1], (Integer) d[2]);
+ int[][] actual = (int[][]) d[3];
+ Assert.assertEquals(result.length, actual.length);
+
+ for (int i = 0; i < result.length; i++) {
+ Assert.assertArrayEquals(actual[i], result[i]);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml b/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0572][Subtree of Another Tree]/src/Solution.java b/[0572][Subtree of Another Tree]/src/Solution.java
new file mode 100644
index 0000000..f14f241
--- /dev/null
+++ b/[0572][Subtree of Another Tree]/src/Solution.java
@@ -0,0 +1,56 @@
+import java.util.Deque;
+import java.util.LinkedList;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 09:02
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public boolean isSubtree(TreeNode s, TreeNode t) {
+
+ if (s == t || t == null) {
+ return true;
+ }
+
+ if (s == null) {
+ return false;
+ }
+
+ Deque queue = new LinkedList<>();
+ queue.addFirst(s);
+ while (!queue.isEmpty()) {
+ TreeNode root = queue.removeFirst();
+ if (root.right != null) {
+ queue.addFirst(root.right);
+ }
+ if (root.left != null) {
+ queue.addFirst(root.left);
+ }
+
+ if(isSame(root, t)) {
+ return true;
+ }
+
+ }
+
+ return false;
+ }
+
+ public boolean isSame(TreeNode s, TreeNode t) {
+ if (s == t) {
+ return true;
+ }
+
+ if (s == null || t == null) {
+ return false;
+ }
+
+ boolean leftSame = isSame(s.left, t.left);
+ boolean rightSame = isSame(s.right, t.right);
+
+ return s.val == t.val && leftSame && rightSame;
+ }
+}
diff --git a/[0572][Subtree of Another Tree]/src/SolutionTest.java b/[0572][Subtree of Another Tree]/src/SolutionTest.java
new file mode 100644
index 0000000..15b8f13
--- /dev/null
+++ b/[0572][Subtree of Another Tree]/src/SolutionTest.java
@@ -0,0 +1,46 @@
+import org.junit.Assert;
+
+import static org.junit.Assert.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-02 09:11
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void isSubtree() {
+ Solution s = new Solution();
+ TreeNode root = new TreeNode(3);
+ root.left = new TreeNode(4);
+ root.right = new TreeNode(5);
+ root.left.left = new TreeNode(1);
+ root.left.right = new TreeNode(2);
+
+ TreeNode t = new TreeNode(4);
+ t.left = new TreeNode(1);
+ t.right = new TreeNode(2);
+
+ Assert.assertEquals(true, s.isSubtree(root, t));
+ }
+
+ @org.junit.Test
+ public void isSubtree2() {
+ Solution s = new Solution();
+ TreeNode root = new TreeNode(3);
+ root.left = new TreeNode(4);
+ root.right = new TreeNode(5);
+ root.left.left = new TreeNode(1);
+ root.left.right = new TreeNode(2);
+ root.left.right.left = new TreeNode(0);
+
+ TreeNode t = new TreeNode(4);
+ t.left = new TreeNode(1);
+ t.right = new TreeNode(2);
+
+ Assert.assertEquals(false, s.isSubtree(root, t));
+ }
+}
\ No newline at end of file
diff --git a/[0572][Subtree of Another Tree]/src/TreeNode.java b/[0572][Subtree of Another Tree]/src/TreeNode.java
new file mode 100644
index 0000000..ac7b907
--- /dev/null
+++ b/[0572][Subtree of Another Tree]/src/TreeNode.java
@@ -0,0 +1,22 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 11:06
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+
+ TreeNode(int val, TreeNode left, TreeNode right) {
+ this.val = val;
+ this.left = left;
+ this.right = right;
+ }
+}
diff --git a/[0575][Distribute Candies]/[0575][Distribute Candies].iml b/[0575][Distribute Candies]/[0575][Distribute Candies].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0575][Distribute Candies]/[0575][Distribute Candies].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0575][Distribute Candies]/src/Solution.java b/[0575][Distribute Candies]/src/Solution.java
new file mode 100644
index 0000000..660aaac
--- /dev/null
+++ b/[0575][Distribute Candies]/src/Solution.java
@@ -0,0 +1,32 @@
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-03 10:03
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 找出共有多少种糖果,如果种类大于糖果数目的一半就取糖果数目的一半,否则就取糖果的种类数
+ *
+ * @param candies
+ * @return
+ */
+ public int distributeCandies(int[] candies) {
+ if (candies == null || candies.length % 2 != 0) {
+ throw new IllegalArgumentException("illegal candies: " + Arrays.toString(candies));
+ }
+
+ Set set = new HashSet<>(candies.length);
+ for (int c : candies) {
+ set.add(c);
+ }
+
+ return Math.min(set.size(), candies.length / 2);
+ }
+
+}
diff --git a/[0575][Distribute Candies]/src/SolutionTest.java b/[0575][Distribute Candies]/src/SolutionTest.java
new file mode 100644
index 0000000..a066444
--- /dev/null
+++ b/[0575][Distribute Candies]/src/SolutionTest.java
@@ -0,0 +1,27 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-03 10:08
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void distributeCandies() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{1, 1, 2, 2, 3, 3}, 3},
+ {new int[]{1, 1, 2, 3}, 2},
+ {new int[]{1, 2, 3, 4}, 2},
+ };
+
+ for (Object[] d : data) {
+ int result = s.distributeCandies((int[]) d[0]);
+ Assert.assertEquals(d[1], result);
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/[0581][Shortest Unsorted Continuous Subarray]/[0581][Shortest Unsorted Continuous Subarray].iml b/[0581][Shortest Unsorted Continuous Subarray]/[0581][Shortest Unsorted Continuous Subarray].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0581][Shortest Unsorted Continuous Subarray]/[0581][Shortest Unsorted Continuous Subarray].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0581][Shortest Unsorted Continuous Subarray]/src/Solution.java b/[0581][Shortest Unsorted Continuous Subarray]/src/Solution.java
new file mode 100644
index 0000000..bace39f
--- /dev/null
+++ b/[0581][Shortest Unsorted Continuous Subarray]/src/Solution.java
@@ -0,0 +1,39 @@
+import java.util.Arrays;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 19:24
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int findUnsortedSubarray(int[] nums) {
+ if (nums == null || nums.length < 2) {
+ return 0;
+ }
+
+ int[] sorted = Arrays.copyOf(nums, nums.length);
+ Arrays.sort(sorted);
+
+
+ int diffStart = 0; // 从开始找第一个位置变化的下标
+ while (diffStart < nums.length && nums[diffStart] == sorted[diffStart]) {
+ diffStart++;
+ }
+
+ if (diffStart >= nums.length) {
+ return 0;
+ }
+
+
+ int diffEnd = nums.length - 1; // 从最后找第一个位置变化的下标
+ while (diffEnd >= 0 && nums[diffEnd] == sorted[diffEnd]) {
+ diffEnd--;
+ }
+
+
+
+ return diffEnd - diffStart + 1;
+ }
+}
diff --git a/[0581][Shortest Unsorted Continuous Subarray]/src/SolutionTest.java b/[0581][Shortest Unsorted Continuous Subarray]/src/SolutionTest.java
new file mode 100644
index 0000000..dcda38c
--- /dev/null
+++ b/[0581][Shortest Unsorted Continuous Subarray]/src/SolutionTest.java
@@ -0,0 +1,26 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 19:32
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findUnsortedSubarray() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{2, 6, 4, 8, 10, 9, 15}, 5},
+ {new int[]{1, 2, 3, 4, 5, 6, 7}, 0},
+ {new int[]{7, 6, 5, 4, 3, 2, 1}, 7},
+ };
+
+ for (Object[] d : data) {
+ Object result = s.findUnsortedSubarray((int[]) d[0]);
+ Assert.assertEquals(d[1], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0589][N-ary Tree Preorder Traversal]/[0589][N-ary Tree Preorder Traversal].iml b/[0589][N-ary Tree Preorder Traversal]/[0589][N-ary Tree Preorder Traversal].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0589][N-ary Tree Preorder Traversal]/[0589][N-ary Tree Preorder Traversal].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0589][N-ary Tree Preorder Traversal]/src/Node.java b/[0589][N-ary Tree Preorder Traversal]/src/Node.java
new file mode 100644
index 0000000..6df4c7f
--- /dev/null
+++ b/[0589][N-ary Tree Preorder Traversal]/src/Node.java
@@ -0,0 +1,24 @@
+import java.util.List;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-03 10:20
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+class Node {
+ public int val;
+ public List children;
+
+ public Node() {}
+
+ public Node(int _val) {
+ this.val = _val;
+ }
+
+ public Node(int val, List children) {
+ this.val = val;
+ this.children = children;
+ }
+}
diff --git a/[0589][N-ary Tree Preorder Traversal]/src/Solution.java b/[0589][N-ary Tree Preorder Traversal]/src/Solution.java
new file mode 100644
index 0000000..f4cc00d
--- /dev/null
+++ b/[0589][N-ary Tree Preorder Traversal]/src/Solution.java
@@ -0,0 +1,56 @@
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-03 10:20
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public List preorder(Node root) {
+ List result = new LinkedList<>();
+// preorderReserve(root, result);
+ preorderIterator(root, result);
+ return result;
+ }
+
+ public void preorderIterator(Node root, List result) {
+ if (root == null) {
+ return;
+ }
+
+ Deque deque = new LinkedList<>();
+ deque.addFirst(root);
+
+ while (!deque.isEmpty()) {
+ Node n = deque.removeFirst();
+ result.add(n.val);
+ if (n.children != null) {
+ int size = n.children.size();
+ for (int i = size - 1; i >= 0; i--) {
+ Node child = n.children.get(i);
+ if (child != null) {
+ deque.addFirst(child);
+ }
+ }
+ }
+ }
+ }
+
+ public void preorderReserve(Node root, List result) {
+
+ if (root == null) {
+ return;
+ }
+
+ result.add(root.val);
+ if (root.children != null) {
+ for (Node n : root.children) {
+ preorderReserve(n, result);
+ }
+ }
+ }
+}
diff --git a/[0590][N-ary Tree postorder Traversal]/[0590][N-ary Tree postorder Traversal].iml b/[0590][N-ary Tree postorder Traversal]/[0590][N-ary Tree postorder Traversal].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0590][N-ary Tree postorder Traversal]/[0590][N-ary Tree postorder Traversal].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0590][N-ary Tree postorder Traversal]/src/Node.java b/[0590][N-ary Tree postorder Traversal]/src/Node.java
new file mode 100644
index 0000000..a6c2e09
--- /dev/null
+++ b/[0590][N-ary Tree postorder Traversal]/src/Node.java
@@ -0,0 +1,24 @@
+import java.util.List;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-03 10:20
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+class Node {
+ public int val;
+ public List children;
+
+ public Node() {}
+
+ public Node(int val) {
+ this.val = val;
+ }
+
+ public Node(int val, List children) {
+ this.val = val;
+ this.children = children;
+ }
+}
diff --git a/[0590][N-ary Tree postorder Traversal]/src/Solution.java b/[0590][N-ary Tree postorder Traversal]/src/Solution.java
new file mode 100644
index 0000000..0dcb602
--- /dev/null
+++ b/[0590][N-ary Tree postorder Traversal]/src/Solution.java
@@ -0,0 +1,63 @@
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-03 10:20
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public List postorder(Node root) {
+ List result = new LinkedList<>();
+// postorderReserve(root, result);
+ postorderIterator(root, result);
+ return result;
+ }
+
+ public void postorderIterator(Node root, List result) {
+ if (root == null) {
+ return;
+ }
+
+ Deque deque = new LinkedList<>();
+ deque.addFirst(root);
+ Node curr;
+ Node prev = null;
+ while (!deque.isEmpty()) {
+ curr = deque.getFirst();
+ if (curr.children == null || curr.children.isEmpty() // 没有子孩子
+ ||(prev != null && prev == curr.children.get(curr.children.size() - 1))) { // 前一个遍历的元素是最后一个子孩子,说明子树已经遍历完了
+ result.add(curr.val);
+ prev = curr;
+ deque.removeFirst();
+ } else {
+ // 有孩子先处理孩子
+ int size = curr.children.size();
+ for (int i = size - 1; i >= 0; i--) {
+ Node child = curr.children.get(i);
+ if (child != null) {
+ deque.addFirst(child);
+ }
+ }
+ }
+ }
+ }
+
+ public void postorderReserve(Node root, List result) {
+
+ if (root == null) {
+ return;
+ }
+
+ if (root.children != null) {
+ for (Node n : root.children) {
+ postorderReserve(n, result);
+ }
+ }
+
+ result.add(root.val);
+ }
+}
diff --git a/[0590][N-ary Tree postorder Traversal]/src/SolutionTest.java b/[0590][N-ary Tree postorder Traversal]/src/SolutionTest.java
new file mode 100644
index 0000000..36f1259
--- /dev/null
+++ b/[0590][N-ary Tree postorder Traversal]/src/SolutionTest.java
@@ -0,0 +1,35 @@
+import org.junit.Assert;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-03 10:47
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void postorder() {
+ Solution s = new Solution();
+
+ Node root = new Node(1);
+ root.children = new ArrayList<>();
+ root.children.add(new Node(3));
+ root.children.add(new Node(2));
+ root.children.add(new Node(4));
+ root.children.get(0).children = new ArrayList<>();
+ root.children.get(0).children.add(new Node(5));
+ root.children.get(0).children.add(new Node(6));
+
+ List result = Arrays.asList(5,6,3,2,4,1);
+
+ Assert.assertEquals(result, s.postorder(root));
+ }
+}
\ No newline at end of file
diff --git a/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml b/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0594][Longest Harmonious Subsequence]/src/Solution.java b/[0594][Longest Harmonious Subsequence]/src/Solution.java
new file mode 100644
index 0000000..d3ec9aa
--- /dev/null
+++ b/[0594][Longest Harmonious Subsequence]/src/Solution.java
@@ -0,0 +1,78 @@
+import java.util.Arrays;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 15:09
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+
+ /**
+ * 求每个元素的个数,再找相邻元素之和最大的
+ * 相邻指元素之差为1
+ *
+ * @param nums
+ * @return
+ */
+ public int findLHS(int[] nums) {
+ if (nums == null || nums.length < 2) {
+ return 0;
+ }
+
+ int max = 0;
+ Arrays.sort(nums);
+
+ int prevNum = Integer.MIN_VALUE;
+ int prevCnt = 0;
+ for (int i = 0; i < nums.length; ) {
+ int next = i + 1;
+ while (next < nums.length && nums[next] == nums[i]) {
+ next++;
+ }
+ if (prevNum + 1 == nums[i]) {
+ max = Math.max(max, prevCnt + next - i);
+ }
+
+ prevNum = nums[i];
+ prevCnt = next - i;
+ i = next;
+
+ }
+
+ return max;
+ }
+
+ /**
+ * 求每个元素的个数,再找相邻元素之和最大的
+ * 相邻指元素之差为1
+ *
+ * @param nums
+ * @return
+ */
+ public int findLHS2(int[] nums) {
+ if (nums == null || nums.length < 2) {
+ return 0;
+ }
+
+ SortedMap map = new TreeMap<>();
+ for (int n : nums) {
+ map.put(n, map.getOrDefault(n, 0) + 1);
+ }
+
+ int max = 0;
+ Map.Entry prev = null; // 前一个遍历的元素
+ for (Map.Entry entry : map.entrySet()) {
+ if (prev != null && prev.getKey() + 1 == entry.getKey()) { // 相邻元素
+ max = Math.max(max, prev.getValue() + entry.getValue()); // 取较大的
+ }
+ prev = entry;
+ }
+
+ return max;
+ }
+}
diff --git a/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java b/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java
new file mode 100644
index 0000000..f51d032
--- /dev/null
+++ b/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java
@@ -0,0 +1,26 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 15:20
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findLHS() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{1, 3, 2, 2, 5, 2, 3, 7}, 5},
+ {new int[]{-2, 0, 2, 2, 5, 5, 5, 5}, 0},
+ {new int[]{1, 1, 1, 1}, 0},
+ };
+
+ for (Object[] d : data) {
+ int result = s.findLHS((int[]) d[0]);
+ Assert.assertEquals(d[1], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0598][Range Addition II]/[0598][Range Addition II].iml b/[0598][Range Addition II]/[0598][Range Addition II].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0598][Range Addition II]/[0598][Range Addition II].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0598][Range Addition II]/src/Solution.java b/[0598][Range Addition II]/src/Solution.java
new file mode 100644
index 0000000..49747ce
--- /dev/null
+++ b/[0598][Range Addition II]/src/Solution.java
@@ -0,0 +1,29 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 15:50
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 数组的第一元素一定是最大的,要找最大的元素个数,只要找ops中min(ops[0])*min(ops[1])
+ *
+ * @param m
+ * @param n
+ * @param ops
+ * @return
+ */
+ public int maxCount(int m, int n, int[][] ops) {
+
+
+ int row = m;
+ int col = n;
+ for (int[] op : ops) {
+ row = Math.min(op[0], row);
+ col = Math.min(op[1], col);
+ }
+
+ return row * col;
+ }
+}
diff --git a/[0598][Range Addition II]/src/SolutionTest.java b/[0598][Range Addition II]/src/SolutionTest.java
new file mode 100644
index 0000000..8550371
--- /dev/null
+++ b/[0598][Range Addition II]/src/SolutionTest.java
@@ -0,0 +1,26 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 16:04
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void maxCount() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {3, 3, new int[][]{{2, 2}, {3, 3}}, 4},
+ {3, 3, new int[][]{{2, 3}, {3, 2}}, 4},
+ {3, 3, new int[][]{}, 9},
+ };
+
+ for (Object[] d : data) {
+ Object result = s.maxCount((int) d[0], (int)d[1], (int[][]) d[2]);
+ Assert.assertEquals(d[3], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml b/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0599][Minimum Index Sum of Two Lists]/src/Solution.java b/[0599][Minimum Index Sum of Two Lists]/src/Solution.java
new file mode 100644
index 0000000..238a202
--- /dev/null
+++ b/[0599][Minimum Index Sum of Two Lists]/src/Solution.java
@@ -0,0 +1,39 @@
+import java.util.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 16:13
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public String[] findRestaurant(String[] list1, String[] list2) {
+ if (list1 == null || list2 == null){
+ return null;
+ }
+
+ Map map = new HashMap<>();
+ for (int i = 0; i < list1.length; i++) {
+ map.put(list1[i], i);
+ }
+ List result = new ArrayList<>(list1.length);
+
+ int min = Integer.MAX_VALUE;
+
+ for (int i = 0; i < list2.length; i++) {
+ if (map.containsKey(list2[i])) {
+ if (map.get(list2[i]) + i < min) {
+ result.clear();
+ min = map.get(list2[i]) + i;
+ result.add(list2[i]);
+ } else if (map.get(list2[i]) + i == min) {
+ result.add(list2[i]);
+ }
+
+ }
+ }
+
+ return result.toArray(new String[0]);
+ }
+}
diff --git a/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java b/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java
new file mode 100644
index 0000000..671b81b
--- /dev/null
+++ b/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java
@@ -0,0 +1,34 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 16:23
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findRestaurant() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {
+ new String[]{"Shogun", "Tapioca Express", "Burger King", "KFC"},
+ new String[]{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"},
+ new String[]{"Shogun"},
+ },
+ {
+ new String[]{"Shogun","Tapioca Express","Burger King","KFC"},
+ new String[]{"KFC","Shogun","Burger King"},
+ new String[]{"Shogun"},
+ }
+
+ };
+
+ for (Object[] d : data) {
+ String[] result = s.findRestaurant((String[]) d[0], (String[]) d[1]);
+ Assert.assertArrayEquals((String[]) d[2], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0605][Can Place Flowers]/[0605][Can Place Flowers].iml b/[0605][Can Place Flowers]/[0605][Can Place Flowers].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0605][Can Place Flowers]/[0605][Can Place Flowers].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0605][Can Place Flowers]/src/Solution.java b/[0605][Can Place Flowers]/src/Solution.java
new file mode 100644
index 0000000..d6aed3c
--- /dev/null
+++ b/[0605][Can Place Flowers]/src/Solution.java
@@ -0,0 +1,54 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 19:42
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public boolean canPlaceFlowers(int[] flowerbed, int n) {
+ if (flowerbed == null || flowerbed.length < 1) {
+ return false;
+ }
+
+ if (n < 1) {
+ return true;
+ }
+
+ // 找第一个1的位置
+ int firstOneIdx = 0;
+ while (firstOneIdx < flowerbed.length && flowerbed[firstOneIdx] == 0) {
+ firstOneIdx++;
+ }
+
+ // [0,0,0] => 2
+ // [0,0] => 1
+ // [0] => 0
+ int num = firstOneIdx / 2 + firstOneIdx % 2;
+
+ if (firstOneIdx >= flowerbed.length) {
+ return num >= n;
+ } else {
+ // [0,0,0,1] => 1
+ // [0,0,1] => 1
+ // [0,1] => 0
+ num -= firstOneIdx % 2;
+ }
+
+ int prev = flowerbed[firstOneIdx];
+ for (int i = firstOneIdx + 1; i < flowerbed.length; i++) {
+ if (prev == 0 && flowerbed[i] == 0) {
+ // i是最后一个,或者下一个位置也没有种花
+ if (i + 1 >= flowerbed.length || flowerbed[i + 1] == 0) {
+ num++;
+ flowerbed[i] = 1;
+ }
+ }
+
+ prev = flowerbed[i];
+ }
+
+
+ return num >= n;
+ }
+}
diff --git a/[0605][Can Place Flowers]/src/SolutionTest.java b/[0605][Can Place Flowers]/src/SolutionTest.java
new file mode 100644
index 0000000..b37b2f1
--- /dev/null
+++ b/[0605][Can Place Flowers]/src/SolutionTest.java
@@ -0,0 +1,31 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 19:52
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void canPlaceFlowers() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{1, 0, 0, 0, 1}, 1, true},
+ {new int[]{1, 0, 0, 0, 1}, 2, false},
+ {new int[]{1, 0, 1, 0, 1}, 1, false},
+ {new int[]{1, 0, 1, 0, 0}, 2, false},
+ {new int[]{0, 0, 0, 0, 0}, 3, true},
+ {new int[]{1, 0, 0, 0, 0, 1}, 2, false},
+ {new int[]{1, 0, 0, 0, 0, 1}, 2, false},
+ {new int[]{ 0, 0, 1, 0, 1}, 1, true},
+ };
+
+ for (Object[] d : data) {
+ Object result = s.canPlaceFlowers((int[]) d[0], (Integer) d[1]);
+ Assert.assertEquals(d[2], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0606][Construct String from Binary Tree]/[0606][Construct String from Binary Tree].iml b/[0606][Construct String from Binary Tree]/[0606][Construct String from Binary Tree].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0606][Construct String from Binary Tree]/[0606][Construct String from Binary Tree].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0606][Construct String from Binary Tree]/src/Solution.java b/[0606][Construct String from Binary Tree]/src/Solution.java
new file mode 100644
index 0000000..36c2245
--- /dev/null
+++ b/[0606][Construct String from Binary Tree]/src/Solution.java
@@ -0,0 +1,41 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 20:09
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public String tree2str(TreeNode t) {
+ StringBuilder builder = new StringBuilder(100);
+ tree2str(t, builder);
+ return builder.toString();
+ }
+
+ private void tree2str(TreeNode root, StringBuilder builder) {
+ if (root == null) {
+ return;
+ }
+
+ builder.append(root.val);
+
+ if (root.left == null && root.right == null) {
+ return;
+ }
+
+ if (root.left != null && root.right == null) {
+ builder.append("(");
+ tree2str(root.left, builder);
+ builder.append(")");
+ }
+
+ if (root.right != null) {
+ builder.append("(");
+ tree2str(root.left, builder);
+ builder.append(")");
+ builder.append("(");
+ tree2str(root.right, builder);
+ builder.append(")");
+ }
+ }
+}
diff --git a/[0606][Construct String from Binary Tree]/src/SolutionTest.java b/[0606][Construct String from Binary Tree]/src/SolutionTest.java
new file mode 100644
index 0000000..08f47cc
--- /dev/null
+++ b/[0606][Construct String from Binary Tree]/src/SolutionTest.java
@@ -0,0 +1,33 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 20:12
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void tree2str() {
+ TreeNode root = new TreeNode(1);
+ root.left = new TreeNode(2);
+ root.right = new TreeNode(3);
+ root.left.left = new TreeNode(4);
+
+ Solution s = new Solution();
+ Assert.assertEquals("1(2(4))(3)", s.tree2str(root));
+ }
+
+ @org.junit.Test
+ public void tree2str2() {
+ TreeNode root = new TreeNode(1);
+ root.left = new TreeNode(2);
+ root.right = new TreeNode(3);
+ root.left.right = new TreeNode(4);
+
+ Solution s = new Solution();
+ Assert.assertEquals("1(2()(4))(3)", s.tree2str(root));
+ }
+}
\ No newline at end of file
diff --git a/[0606][Construct String from Binary Tree]/src/TreeNode.java b/[0606][Construct String from Binary Tree]/src/TreeNode.java
new file mode 100644
index 0000000..8b88e24
--- /dev/null
+++ b/[0606][Construct String from Binary Tree]/src/TreeNode.java
@@ -0,0 +1,16 @@
+/**
+ * Author: 王俊超
+ * Date: 2015-08-21
+ * Time: 18:45
+ * Declaration: All Rights Reserved !!!
+ */
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
+
diff --git a/[0617][Merge Two Binary Trees]/src/Main.java b/[0617][Merge Two Binary Trees]/src/Main.java
index 64abb99..f0f2e17 100644
--- a/[0617][Merge Two Binary Trees]/src/Main.java
+++ b/[0617][Merge Two Binary Trees]/src/Main.java
@@ -7,7 +7,7 @@
public class Main {
@Test
public void test1() {
- TreeNode t1 = new TreeNode(1);
+ TreeNode t1 = new TreeNode(1);
t1.left = new TreeNode(3);
t1.left.left = new TreeNode(5);
t1.right = new TreeNode(2);
@@ -24,6 +24,7 @@ public void test1() {
print(node);
}
+
private void print(TreeNode node) {
if (node != null) {
System.out.print(node.val + ", ");
diff --git a/[0617][Merge Two Binary Trees]/src/Solution.java b/[0617][Merge Two Binary Trees]/src/Solution.java
index c9aa871..3123ffe 100644
--- a/[0617][Merge Two Binary Trees]/src/Solution.java
+++ b/[0617][Merge Two Binary Trees]/src/Solution.java
@@ -1,4 +1,3 @@
-
/**
* @author: wangjunchao(王俊超)
* @time: 2019-07-04 08:57
diff --git a/[0617][Merge Two Binary Trees]/src/TreeNode.java b/[0617][Merge Two Binary Trees]/src/TreeNode.java
index 8e2131e..8b88e24 100644
--- a/[0617][Merge Two Binary Trees]/src/TreeNode.java
+++ b/[0617][Merge Two Binary Trees]/src/TreeNode.java
@@ -1,4 +1,3 @@
-
/**
* Author: 王俊超
* Date: 2015-08-21
diff --git a/[0628][Maximum Product of Three Numbers]/[0628][Maximum Product of Three Numbers].iml b/[0628][Maximum Product of Three Numbers]/[0628][Maximum Product of Three Numbers].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0628][Maximum Product of Three Numbers]/[0628][Maximum Product of Three Numbers].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0628][Maximum Product of Three Numbers]/src/Solution.java b/[0628][Maximum Product of Three Numbers]/src/Solution.java
new file mode 100644
index 0000000..a39a8e8
--- /dev/null
+++ b/[0628][Maximum Product of Three Numbers]/src/Solution.java
@@ -0,0 +1,29 @@
+import java.util.Arrays;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 20:23
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int maximumProduct(int[] nums) {
+ if (nums == null || nums.length < 3) {
+ return 0;
+ }
+
+ Arrays.sort(nums);
+
+ int len = nums.length;
+
+ int max = nums[len - 1] * nums[len - 2] * nums[len - 3];
+
+ if (nums[1] < 0) { // 前两个是负数
+ max = Math.max(max, nums[0]*nums[1]*nums[len-1]);
+ }
+
+
+ return max;
+ }
+}
diff --git a/[0628][Maximum Product of Three Numbers]/src/SolutionTest.java b/[0628][Maximum Product of Three Numbers]/src/SolutionTest.java
new file mode 100644
index 0000000..04697b4
--- /dev/null
+++ b/[0628][Maximum Product of Three Numbers]/src/SolutionTest.java
@@ -0,0 +1,24 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 20:45
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void maximumProduct() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{-4, -3, -2, -1, 60}, 720},
+ };
+
+ for (Object[] d : data) {
+ int result = s.maximumProduct((int[]) d[0]);
+ Assert.assertEquals(d[1], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0633][Sum of Square Numbers]/[0633][Sum of Square Numbers].iml b/[0633][Sum of Square Numbers]/[0633][Sum of Square Numbers].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0633][Sum of Square Numbers]/[0633][Sum of Square Numbers].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0633][Sum of Square Numbers]/src/Solution.java b/[0633][Sum of Square Numbers]/src/Solution.java
new file mode 100644
index 0000000..816b16e
--- /dev/null
+++ b/[0633][Sum of Square Numbers]/src/Solution.java
@@ -0,0 +1,36 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-05 16:55
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 解题思路:
+ * 1、假设b >=a >= 0
+ * ==> sqrt(c/2) <= b <= sqrt(c)
+ * @param c
+ * @return
+ */
+ public boolean judgeSquareSum(int c) {
+
+ if (c < 0) {
+ return false;
+ }
+
+
+ int sqrt = (int) Math.sqrt(c);
+ int halfSqrt = (int) Math.sqrt(c / 2.0);
+
+ for (int i = halfSqrt; i <= sqrt; i++) {
+
+ int sqrt2 = (int) Math.sqrt(c - i * i);
+ if (sqrt2 * sqrt2 + i * i == c) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/[0633][Sum of Square Numbers]/src/SolutionTest.java b/[0633][Sum of Square Numbers]/src/SolutionTest.java
new file mode 100644
index 0000000..a859ba2
--- /dev/null
+++ b/[0633][Sum of Square Numbers]/src/SolutionTest.java
@@ -0,0 +1,30 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-05 17:00
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void judgeSquareSum() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {0, true},
+ {1, true},
+ {2, true},
+ {3, false},
+ {4, true},
+ {5, true},
+ {1000, true},
+ };
+
+ for (Object[] d : data) {
+ Object result = s.judgeSquareSum((int) d[0]);
+ Assert.assertEquals(d[1], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml b/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0637][Average of Levels in Binary Tree]/src/Solution.java b/[0637][Average of Levels in Binary Tree]/src/Solution.java
new file mode 100644
index 0000000..9e113fb
--- /dev/null
+++ b/[0637][Average of Levels in Binary Tree]/src/Solution.java
@@ -0,0 +1,48 @@
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 16:32
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public List averageOfLevels(TreeNode root) {
+ List result = new ArrayList<>();
+
+ if (root == null) {
+ return result;
+ }
+
+ Deque curr = new LinkedList<>();
+ Deque next = new LinkedList<>();
+ curr.addFirst(root);
+
+ while (!curr.isEmpty()) {
+
+ double count = curr.size();
+ double sum = 0;
+ while (!curr.isEmpty()) {
+ TreeNode node = curr.removeFirst();
+ sum += node.val;
+
+ if (node.left != null) {
+ next.addLast(node.left);
+ }
+ if (node.right != null) {
+ next.addLast(node.right);
+ }
+ }
+
+ result.add(sum / count);
+ curr = next;
+ next = new LinkedList<>();
+ }
+
+ return result;
+ }
+}
diff --git a/[0637][Average of Levels in Binary Tree]/src/TreeNode.java b/[0637][Average of Levels in Binary Tree]/src/TreeNode.java
new file mode 100644
index 0000000..5554849
--- /dev/null
+++ b/[0637][Average of Levels in Binary Tree]/src/TreeNode.java
@@ -0,0 +1,13 @@
+/**
+ * @author: wangjunchao(王俊超)
+ * @time: 2019-07-09 18:52
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
diff --git a/[0643][Maximum Average Subarray I]/[0643][Maximum Average Subarray I].iml b/[0643][Maximum Average Subarray I]/[0643][Maximum Average Subarray I].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0643][Maximum Average Subarray I]/[0643][Maximum Average Subarray I].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0643][Maximum Average Subarray I]/src/Solution.java b/[0643][Maximum Average Subarray I]/src/Solution.java
new file mode 100644
index 0000000..d0a7f8c
--- /dev/null
+++ b/[0643][Maximum Average Subarray I]/src/Solution.java
@@ -0,0 +1,28 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-05 16:23
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public double findMaxAverage(int[] nums, int k) {
+ if (nums == null || nums.length < 1 || k < 1) {
+ return 0;
+ }
+
+ double sum = 0;
+ for (int i = 0; i < nums.length && i < k; i++) { // 求nums[0, k-1]的和
+ sum += nums[i];
+ }
+
+ double max = sum;
+ for (int j = k; j < nums.length; j++) { // 求nums[j-k,j]中的大值
+ sum = sum + nums[j] - nums[j - k];
+ max = Math.max(max, sum);
+
+ }
+
+ return max / k;
+ }
+}
diff --git a/[0643][Maximum Average Subarray I]/src/SolutionTest.java b/[0643][Maximum Average Subarray I]/src/SolutionTest.java
new file mode 100644
index 0000000..f698ef1
--- /dev/null
+++ b/[0643][Maximum Average Subarray I]/src/SolutionTest.java
@@ -0,0 +1,25 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-05 16:29
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findMaxAverage() {
+ Solution s = new Solution();
+ Object[][] data = {
+// {new int[]{1, 12, -5, -6, 50, 3}, 4, 12.75},
+ {new int[]{7, 4, 5, 8, 8, 3, 9, 8, 7, 6}, 7, 7.0},
+ };
+
+ for (Object[] d : data) {
+ Object result = s.findMaxAverage((int[]) d[0], (Integer) d[1]);
+ Assert.assertEquals(d[2], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0645][Set Mismatch]/[0645][Set Mismatch].iml b/[0645][Set Mismatch]/[0645][Set Mismatch].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0645][Set Mismatch]/[0645][Set Mismatch].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0645][Set Mismatch]/src/Solution.java b/[0645][Set Mismatch]/src/Solution.java
new file mode 100644
index 0000000..006c5f8
--- /dev/null
+++ b/[0645][Set Mismatch]/src/Solution.java
@@ -0,0 +1,43 @@
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-05 17:13
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 通过求和找丢失的数
+ * m + x + y = total
+ * m + x + x = sum
+ * y - x = total -sum
+ *
+ * @param nums
+ * @return
+ */
+ public int[] findErrorNums(int[] nums) {
+ if (nums == null || nums.length < 1) {
+ return null;
+ }
+
+ Set map = new HashSet<>();
+ int num = 1;
+ int sum = 0;
+
+ for (int n : nums) {
+ if (map.contains(n)) {
+ num = n;
+ } else {
+ map.add(n);
+ }
+ sum += n;
+ }
+
+ return new int[]{num, (1 + nums.length) * nums.length / 2 - sum + num};
+
+ }
+
+}
diff --git a/[0645][Set Mismatch]/src/SolutionTest.java b/[0645][Set Mismatch]/src/SolutionTest.java
new file mode 100644
index 0000000..93087a5
--- /dev/null
+++ b/[0645][Set Mismatch]/src/SolutionTest.java
@@ -0,0 +1,25 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-05 17:19
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findErrorNums() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{1, 2, 2, 4}, new int[]{2, 3}},
+ {new int[]{1, 3, 2, 4, 4}, new int[]{4, 5}},
+ };
+
+ for (Object[] d : data) {
+ int[] result = s.findErrorNums((int[]) d[0]);
+ Assert.assertArrayEquals((int[]) d[1], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0653][Two Sum IV - Input is a BST]/[0653][Two Sum IV - Input is a BST].iml b/[0653][Two Sum IV - Input is a BST]/[0653][Two Sum IV - Input is a BST].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0653][Two Sum IV - Input is a BST]/[0653][Two Sum IV - Input is a BST].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0653][Two Sum IV - Input is a BST]/src/Solution.java b/[0653][Two Sum IV - Input is a BST]/src/Solution.java
new file mode 100644
index 0000000..05f7a25
--- /dev/null
+++ b/[0653][Two Sum IV - Input is a BST]/src/Solution.java
@@ -0,0 +1,69 @@
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-05 18:20
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树:
+ * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
+ * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
+ * 它的左、右子树也分别为二叉排序树。
+ *
+ * @param root
+ * @param k
+ * @return
+ */
+
+
+ public boolean findTarget(TreeNode root, int k) {
+ Set set = new HashSet<>();
+ return findTargetHelp(root, k, set);
+ }
+
+ private boolean findTargetHelp(TreeNode root, int k, Set set) {
+ if (root == null) {
+ return false;
+ }
+ if (set.contains(k - root.val)) {
+ return true;
+ }
+
+ set.add(root.val);
+
+ return findTargetHelp(root.left, k, set) || findTargetHelp(root.right, k, set);
+ }
+
+ public boolean findTarget2(TreeNode root, int k) {
+ if (root == null) {
+ return false;
+ }
+
+ Set set = new HashSet<>();
+ visitTree(root, set);
+
+ for (Integer n : set) {
+ if (n != k - n && set.contains(k - n)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public void visitTree(TreeNode root, Set set) {
+ if (root == null) {
+ return;
+ }
+
+ set.add(root.val);
+
+ visitTree(root.left, set);
+ visitTree(root.right, set);
+ }
+}
diff --git a/[0653][Two Sum IV - Input is a BST]/src/SolutionTest.java b/[0653][Two Sum IV - Input is a BST]/src/SolutionTest.java
new file mode 100644
index 0000000..792632a
--- /dev/null
+++ b/[0653][Two Sum IV - Input is a BST]/src/SolutionTest.java
@@ -0,0 +1,19 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-05 18:35
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findTarget() {
+ Solution s = new Solution();
+ TreeNode root = new TreeNode(1);
+
+ Assert.assertFalse(s.findTarget(root, 2));
+ }
+}
\ No newline at end of file
diff --git a/[0653][Two Sum IV - Input is a BST]/src/TreeNode.java b/[0653][Two Sum IV - Input is a BST]/src/TreeNode.java
new file mode 100644
index 0000000..5554849
--- /dev/null
+++ b/[0653][Two Sum IV - Input is a BST]/src/TreeNode.java
@@ -0,0 +1,13 @@
+/**
+ * @author: wangjunchao(王俊超)
+ * @time: 2019-07-09 18:52
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
diff --git a/[0657][Robot Return to Origin]/[0657][Robot Return to Origin].iml b/[0657][Robot Return to Origin]/[0657][Robot Return to Origin].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0657][Robot Return to Origin]/[0657][Robot Return to Origin].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0657][Robot Return to Origin]/src/Solution.java b/[0657][Robot Return to Origin]/src/Solution.java
new file mode 100644
index 0000000..e02ca96
--- /dev/null
+++ b/[0657][Robot Return to Origin]/src/Solution.java
@@ -0,0 +1,34 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-04 20:51
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public boolean judgeCircle(String moves) {
+ if (moves == null || moves.length() < 1) {
+ return true;
+ }
+
+ int[] pos = {0, 0};
+ for (int i = 0; i < moves.length(); i++) {
+ switch (moves.charAt(i)) {
+ case 'U':
+ pos[1]++;
+ break;
+ case 'D':
+ pos[1]--;
+ break;
+ case 'L':
+ pos[0]--;
+ break;
+ case 'R':
+ pos[0]++;
+ break;
+ }
+ }
+
+ return pos[0] == 0 && pos[1] == 0;
+ }
+}
diff --git a/[0661][Image Smoother]/[0661][Image Smoother].iml b/[0661][Image Smoother]/[0661][Image Smoother].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0661][Image Smoother]/[0661][Image Smoother].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0661][Image Smoother]/src/Solution.java b/[0661][Image Smoother]/src/Solution.java
new file mode 100644
index 0000000..a92e9d8
--- /dev/null
+++ b/[0661][Image Smoother]/src/Solution.java
@@ -0,0 +1,32 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-06 08:11
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+
+ public int[][] imageSmoother(int[][] M) {
+ int[][] result = new int[M.length][];
+ for (int i = 0; i < M.length; i++) {
+ result[i] = new int[M[i].length];
+ }
+
+ for (int i = 0; i < M.length; i++) {
+ for (int j = 0; j < M[0].length; j++) {
+ int count = 0;
+ int sum = 0;
+ for (int u = Math.max(0, i - 1); u <= i + 1 && u < M.length; u++) {
+ for (int v = Math.max(0, j - 1); v <= j + 1 && v < M[0].length; v++) {
+ count++;
+ sum += M[u][v];
+ }
+ }
+ result[i][j] = sum / count;
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/[0661][Image Smoother]/src/SolutionTest.java b/[0661][Image Smoother]/src/SolutionTest.java
new file mode 100644
index 0000000..311d241
--- /dev/null
+++ b/[0661][Image Smoother]/src/SolutionTest.java
@@ -0,0 +1,29 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-06 08:17
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void imageSmoother() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {
+ new int[][]{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}},
+ new int[][]{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
+ },
+ };
+
+ for (Object[] d : data) {
+ int[][] result = s.imageSmoother((int[][]) d[0]);
+ for (int i = 0; i < result.length; i++) {
+ Assert.assertArrayEquals(((int[][]) d[1])[i], result[i]);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml b/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0665][Non-decreasing Array]/src/Solution.java b/[0665][Non-decreasing Array]/src/Solution.java
new file mode 100644
index 0000000..db06448
--- /dev/null
+++ b/[0665][Non-decreasing Array]/src/Solution.java
@@ -0,0 +1,34 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-06 08:27
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+
+ /**
+ * 解题思路:
+ * 其实说的简单点,有2种情况:
+ * 升序序列中,突然有个数字小了,则nums[i]=nums[i-1]
+ * 升序序列中,突然有个数字大了,则nums[i-1]=nums[i]
+ *
+ * @param nums
+ * @return
+ */
+ public boolean checkPossibility(int[] nums) {
+ int fix = 0;
+ for (int i = 1; i < nums.length && fix <= 1; i++) {
+ if (nums[i] >= nums[i - 1]) {
+ continue;
+ }
+ fix++;
+ if (i - 2 >= 0 && (nums[i] < nums[i - 2])) { // 突然有个数字小了
+ nums[i] = nums[i - 1];
+ } else {// 突然有个数字大了
+ nums[i - 1] = nums[i];
+ }
+ }
+ return fix <= 1;
+ }
+}
diff --git a/[0665][Non-decreasing Array]/src/SolutionTest.java b/[0665][Non-decreasing Array]/src/SolutionTest.java
new file mode 100644
index 0000000..1667886
--- /dev/null
+++ b/[0665][Non-decreasing Array]/src/SolutionTest.java
@@ -0,0 +1,28 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-06 08:38
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void checkPossibility() {
+ Solution s = new Solution();
+ Object[][] data = {
+// {new int[]{1, 2, 3, 4, 6, 5, 7, 8, 9}, true},
+// {new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, true},
+// {new int[]{4, 2, 3}, true},
+// {new int[]{4, 2, 1}, false},
+ {new int[]{-1, 4, 2, 3}, false},
+ };
+
+ for (Object[] d : data) {
+ boolean result = s.checkPossibility((int[]) d[0]);
+ Assert.assertEquals(d[1], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml b/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0669][Trim a Binary Search Tree]/src/Solution.java b/[0669][Trim a Binary Search Tree]/src/Solution.java
new file mode 100644
index 0000000..c86b80d
--- /dev/null
+++ b/[0669][Trim a Binary Search Tree]/src/Solution.java
@@ -0,0 +1,33 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-06 08:58
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public TreeNode trimBST(TreeNode root, int L, int R) {
+ if (root == null) {
+ return null;
+ }
+
+ if (root.val == L) {
+ root.left = null;
+ root.right = trimBST(root.right, L, R);
+ return root;
+ } else if (root.val == R) {
+ root.left = trimBST(root.left, L, R);
+ root.right = null;
+ return root;
+ } else if (root.val < L) {
+ return trimBST(root.right, L, R);
+ } else if (root.val > R) {
+ return trimBST(root.left, L, R);
+ }
+
+ root.left = trimBST(root.left, L, R);
+ root.right = trimBST(root.right, L, R);
+
+ return root;
+ }
+}
diff --git a/[0669][Trim a Binary Search Tree]/src/TreeNode.java b/[0669][Trim a Binary Search Tree]/src/TreeNode.java
new file mode 100644
index 0000000..bff99a6
--- /dev/null
+++ b/[0669][Trim a Binary Search Tree]/src/TreeNode.java
@@ -0,0 +1,16 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 11:06
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
diff --git a/[0671][Second Minimum Node In a Binary Tree]/[0671][Second Minimum Node In a Binary Tree].iml b/[0671][Second Minimum Node In a Binary Tree]/[0671][Second Minimum Node In a Binary Tree].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0671][Second Minimum Node In a Binary Tree]/[0671][Second Minimum Node In a Binary Tree].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0671][Second Minimum Node In a Binary Tree]/src/Solution.java b/[0671][Second Minimum Node In a Binary Tree]/src/Solution.java
new file mode 100644
index 0000000..6562244
--- /dev/null
+++ b/[0671][Second Minimum Node In a Binary Tree]/src/Solution.java
@@ -0,0 +1,68 @@
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-07 08:58
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * @param root
+ * @return
+ */
+
+ public int findSecondMinimumValue(TreeNode root) {
+ if (root == null){
+ return -1;
+ }
+ return help(root, root.val);
+ }
+
+ private int help(TreeNode root, int min) {
+ if (root == null){
+ return -1;
+ }
+ if (root.val > min) {
+ return root.val;
+ }
+ int left = help(root.left, min);
+ int right = help(root.right, min);
+
+ if (left == -1) {
+ return right;
+ }
+ if (right == -1) {
+ return left;
+ }
+
+ return Math.min(left, right);
+ }
+
+ public int findSecondMinimumValue2(TreeNode root) {
+ SortedSet result = new TreeSet<>();
+ findSecondMinimumValue(root, result);
+
+ if (result.size() < 2) {
+ return -1;
+ }
+
+ result.remove(result.first());
+ return result.first();
+ }
+
+ /**
+ * @param root
+ * @param result 是一个从小到大的size=2的有序数组
+ */
+ private void findSecondMinimumValue(TreeNode root, SortedSet result) {
+ if (root == null) {
+ return;
+ }
+ result.add(root.val);
+ findSecondMinimumValue(root.left, result);
+ findSecondMinimumValue(root.right, result);
+ }
+}
diff --git a/[0671][Second Minimum Node In a Binary Tree]/src/SolutionTest.java b/[0671][Second Minimum Node In a Binary Tree]/src/SolutionTest.java
new file mode 100644
index 0000000..26dc827
--- /dev/null
+++ b/[0671][Second Minimum Node In a Binary Tree]/src/SolutionTest.java
@@ -0,0 +1,24 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-07 09:13
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findSecondMinimumValue() {
+ Solution s = new Solution();
+
+ TreeNode root = new TreeNode(2);
+ root.left = new TreeNode(2);
+ root.right = new TreeNode(5);
+ root.right.left = new TreeNode(5);
+ root.right.right = new TreeNode(7);
+
+ Assert.assertEquals(5, s.findSecondMinimumValue(root));
+ }
+}
\ No newline at end of file
diff --git a/[0671][Second Minimum Node In a Binary Tree]/src/TreeNode.java b/[0671][Second Minimum Node In a Binary Tree]/src/TreeNode.java
new file mode 100644
index 0000000..bff99a6
--- /dev/null
+++ b/[0671][Second Minimum Node In a Binary Tree]/src/TreeNode.java
@@ -0,0 +1,16 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 11:06
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
diff --git a/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml b/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java b/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java
new file mode 100644
index 0000000..5a15c9f
--- /dev/null
+++ b/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java
@@ -0,0 +1,30 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-07 10:03
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+
+ public int findLengthOfLCIS(int[] nums) {
+ if (nums == null) {
+ return 0;
+ } else if (nums.length < 2) {
+ return nums.length;
+ }
+
+ int prev = 0;
+ int max = 0;
+ for (int i = 1; i < nums.length; i++) {
+ while (i < nums.length && nums[i - 1] < nums[i]) {
+ i++;
+ }
+
+ max = Math.max(max, i - prev);
+ prev = i;
+ }
+
+ return max;
+ }
+}
diff --git a/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java b/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java
new file mode 100644
index 0000000..351e6f5
--- /dev/null
+++ b/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java
@@ -0,0 +1,27 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-07 10:07
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void findLengthOfLCIS() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new int[]{1, 3, 5, 4, 7}, 3},
+ {new int[]{2, 2, 2, 2, 2}, 1},
+ {new int[]{1, 2, 3, 4, 5}, 5},
+ {new int[]{2, 2, 3, 4, 5}, 4},
+ };
+
+ for (Object[] d : data) {
+ int result = s.findLengthOfLCIS((int[]) d[0]);
+ Assert.assertEquals(d[1], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml b/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0680][Valid Palindrome II]/src/Solution.java b/[0680][Valid Palindrome II]/src/Solution.java
new file mode 100644
index 0000000..9ae5eb2
--- /dev/null
+++ b/[0680][Valid Palindrome II]/src/Solution.java
@@ -0,0 +1,35 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-07-07 10:12
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public boolean validPalindrome(String s) {
+ int start = 0;
+ int end = s.length() - 1;
+ while (start < end) {
+ // 有一个不相等,可以抛弃start或者end字符,抛弃一个字符后,后面的都要是回文
+ if (s.charAt(start) != s.charAt(end)) {
+ return validPalindrome(s, start + 1, end) || validPalindrome(s, start, end - 1);
+ }
+ start++;
+ end--;
+ }
+
+ return true;
+ }
+
+ private boolean validPalindrome(String s, int start, int end) {
+ while (start < end) {
+ if (s.charAt(start) != s.charAt(end)) {
+ return false;
+ }
+ start++;
+ end--;
+ }
+
+ return true;
+ }
+}
diff --git a/[0682][Baseball Game]/[0682][Baseball Game].iml b/[0682][Baseball Game]/[0682][Baseball Game].iml
new file mode 100644
index 0000000..c70cbf1
--- /dev/null
+++ b/[0682][Baseball Game]/[0682][Baseball Game].iml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0682][Baseball Game]/src/Solution.java b/[0682][Baseball Game]/src/Solution.java
new file mode 100644
index 0000000..4af7f26
--- /dev/null
+++ b/[0682][Baseball Game]/src/Solution.java
@@ -0,0 +1,43 @@
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-07 12:46
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ public int calPoints(String[] ops) {
+
+ if (ops == null || ops.length < 1) {
+ return 0;
+ }
+ List round = new LinkedList<>();
+
+ for (String o : ops) {
+ int size = round.size();
+ switch (o) {
+ case "+":
+ round.add(round.get(size - 1) + round.get(size - 2));
+ break;
+ case "D":
+ round.add(2 * round.get(size - 1));
+ break;
+ case "C":
+ round.remove(size - 1);
+ break;
+ default:
+ round.add(Integer.parseInt(o));
+ }
+ }
+
+ int sum = 0;
+ for(Integer n : round) {
+ sum += n;
+ }
+
+ return sum;
+ }
+}
diff --git a/[0682][Baseball Game]/src/SolutionTest.java b/[0682][Baseball Game]/src/SolutionTest.java
new file mode 100644
index 0000000..1cf9c9d
--- /dev/null
+++ b/[0682][Baseball Game]/src/SolutionTest.java
@@ -0,0 +1,25 @@
+import org.junit.Assert;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-07-07 12:53
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class SolutionTest {
+
+ @org.junit.Test
+ public void calPoints() {
+ Solution s = new Solution();
+ Object[][] data = {
+ {new String[]{"5", "2", "C", "D", "+"}, 30},
+ {new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"}, 27},
+ };
+
+ for (Object[] d : data) {
+ int result = s.calPoints((String[]) d[0]);
+ Assert.assertEquals(d[1], result);
+ }
+ }
+}
\ No newline at end of file
diff --git a/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml b/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/[0783][Minimum Distance Between BST Nodes]/src/Solution.java b/[0783][Minimum Distance Between BST Nodes]/src/Solution.java
new file mode 100644
index 0000000..0230f1b
--- /dev/null
+++ b/[0783][Minimum Distance Between BST Nodes]/src/Solution.java
@@ -0,0 +1,50 @@
+import java.util.*;
+
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 10:09
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class Solution {
+ /**
+ * 解题思路
+ * root提个二叉搜索树
+ * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树:
+ * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
+ * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
+ * 它的左、右子树也分别为二叉排序树。
+ *
+ * @param root
+ * @return
+ */
+ public int minDiffInBST(TreeNode root) {
+ if (root == null) {
+ return 0;
+ }
+ Deque s = new LinkedList<>();
+ int min = Integer.MAX_VALUE;
+ int prev = 0;
+ boolean first = true;
+ while (root != null || !s.isEmpty()) {
+ while (root != null) {
+ s.push(root);//先访问再入栈
+ root = root.left;
+ }
+ root = s.pop();
+
+ if (first) {
+ first = false;
+ } else {
+ min = Math.min(min, root.val - prev);
+ }
+
+ prev = root.val;
+ root = root.right;//如果是null,出栈并处理右子树
+ }
+
+ return min;
+ }
+
+}
diff --git a/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java b/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java
new file mode 100644
index 0000000..bff99a6
--- /dev/null
+++ b/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java
@@ -0,0 +1,16 @@
+/**
+ * Author: 王俊超
+ * Time: 2020-06-29 11:06
+ * CSDN: http://blog.csdn.net/derrantcm
+ * Github: https://github.com/Wang-Jun-Chao
+ * Declaration: All Rights Reserved !!!
+ **/
+public class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+
+ TreeNode(int x) {
+ val = x;
+ }
+}
diff --git a/[0969][Pancake Sorting]/src/Solution.java b/[0969][Pancake Sorting]/src/Solution.java
index b16de77..4aafc53 100644
--- a/[0969][Pancake Sorting]/src/Solution.java
+++ b/[0969][Pancake Sorting]/src/Solution.java
@@ -3,6 +3,7 @@
/**
* https://leetcode.com/problems/pancake-sorting/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-09 18:29
**/
@@ -45,6 +46,7 @@ public class Solution {
* 完成把当前最大值放到最后的操作,即完成选择排序的选择最大值到最后的操作。
* 缩减未排序的序列,即可完成排序;
*
+ *
* @param nums nums元素的值是[1, 2, ..., nums.length]的一个排列
* @return
*/
diff --git a/[0979][Distribute Coins in Binary Tree]/src/Solution.java b/[0979][Distribute Coins in Binary Tree]/src/Solution.java
index 8819f14..fac903d 100644
--- a/[0979][Distribute Coins in Binary Tree]/src/Solution.java
+++ b/[0979][Distribute Coins in Binary Tree]/src/Solution.java
@@ -1,5 +1,6 @@
/**
* https://leetcode.com/problems/distribute-coins-in-binary-tree/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-09 18:52
**/
@@ -17,6 +18,7 @@ public class Solution {
*
* https://www.cnblogs.com/seyjs/p/10369614.html
*
+ *
* @param root
* @return
*/
@@ -32,6 +34,7 @@ public int distributeCoins(TreeNode root) {
/**
* 自底向上,对每一个节点,只能从父结点借,或者向父节点上交coin
+ *
* @param node
* @param parent
*/
diff --git a/[0979][Distribute Coins in Binary Tree]/src/TreeNode.java b/[0979][Distribute Coins in Binary Tree]/src/TreeNode.java
index 36ec099..5554849 100644
--- a/[0979][Distribute Coins in Binary Tree]/src/TreeNode.java
+++ b/[0979][Distribute Coins in Binary Tree]/src/TreeNode.java
@@ -3,7 +3,7 @@
* @time: 2019-07-09 18:52
**/
public class TreeNode {
- int val;
+ int val;
TreeNode left;
TreeNode right;
diff --git a/[1089][Duplicate Zeros]/src/Solution.java b/[1089][Duplicate Zeros]/src/Solution.java
index 9975463..701084f 100644
--- a/[1089][Duplicate Zeros]/src/Solution.java
+++ b/[1089][Duplicate Zeros]/src/Solution.java
@@ -6,7 +6,7 @@ public class Solution {
public void duplicateZeros(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == 0) {
- move(arr, i+1);
+ move(arr, i + 1);
arr[i + 1] = 0;
i++;
}
diff --git a/[1108][Defanging an IP Address]/src/Solution.java b/[1108][Defanging an IP Address]/src/Solution.java
index 96d080d..8667b05 100644
--- a/[1108][Defanging an IP Address]/src/Solution.java
+++ b/[1108][Defanging an IP Address]/src/Solution.java
@@ -1,5 +1,6 @@
/**
* https://leetcode.com/problems/defanging-an-ip-address/submissions/
+ *
* @author: wangjunchao(王俊超)
* @time: 2019-07-12 08:31
**/
@@ -21,11 +22,12 @@ public class Solution {
* Input: address = "255.100.50.0"
* Output: "255[.]100[.]50[.]0"
*
+ *
* @param address
* @return
*/
public String defangIPaddr(String address) {
- if (address ==null || address.length() < 1) {
+ if (address == null || address.length() < 1) {
return address;
}