diff --git a/.gitignore b/.gitignore
index 259b906..413eb6c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,7 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin/
+.idea/*
+.classpath
+.project
+*.xml
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..e208459
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..8fd8bd1
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..455c569
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,519 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+ DEFINITION_ORDER
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1512935398459
+
+
+ 1512935398459
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ file://$PROJECT_DIR$/src/Issues/Q388.java
+ 9
+
+
+
+ file://$PROJECT_DIR$/src/Issues/Q388.java
+ 17
+
+
+
+
+ file://$PROJECT_DIR$/src/Issues/Q346.java
+ 29
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1.8
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/LeetCode.iml b/LeetCode.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/LeetCode.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/EntryPoint/Starter.java b/src/EntryPoint/Starter.java
index d986b9e..d719182 100644
--- a/src/EntryPoint/Starter.java
+++ b/src/EntryPoint/Starter.java
@@ -1,10 +1,22 @@
package EntryPoint;
+
+import Issues.*;
+
+
public class Starter {
public static void main(String[] args) {
+
+
+ IIssueTemplate q = new Q346();
+
+ q.go();
+
+
System.out.println("Hello world");
+
}
}
diff --git a/src/Issues/IIssueTemplate.java b/src/Issues/IIssueTemplate.java
new file mode 100644
index 0000000..87054d5
--- /dev/null
+++ b/src/Issues/IIssueTemplate.java
@@ -0,0 +1,5 @@
+package Issues;
+
+public interface IIssueTemplate {
+ void go();
+}
diff --git a/src/Issues/Q117.java b/src/Issues/Q117.java
new file mode 100644
index 0000000..78debc5
--- /dev/null
+++ b/src/Issues/Q117.java
@@ -0,0 +1,96 @@
+package Issues;
+
+public class Q117 implements IIssueTemplate {
+
+ class TreeLinkNode {
+ int val;
+ TreeLinkNode left, right, next;
+ TreeLinkNode(int x) { val = x; }
+ }
+
+ public void connect(TreeLinkNode root) {
+ if(root == null){
+ return ;
+ }
+
+ TreeLinkNode start = root;
+
+ while(start != null){
+ TreeLinkNode node = start;
+
+ while(node != null){
+ if(node.left == null && node.right == null){
+ node = node.next;
+ continue;
+ }
+ TreeLinkNode ptr = null;
+ if(node.left != null && node.right != null){
+ node.left.next = node.right;
+ ptr = node.right;
+ }else if(node.left != null){
+ ptr = node.left;
+ }else{
+ ptr = node.right;
+ }
+ TreeLinkNode tmp = node.next;
+ while(tmp != null && tmp.left == null && tmp.right == null){
+ tmp = tmp.next;
+ }
+ if(tmp != null){
+ if(tmp.left != null){
+ ptr.next = tmp.left;
+ }else{
+ ptr.next = tmp.right;
+ }
+
+ }
+ node = tmp;
+ }
+
+ while(start != null){
+ if(start.left != null){
+ start = start.left;
+ break;
+ }else if(start.right != null){
+ start = start.right;
+ break;
+ }else{
+ start = start.next;
+ }
+ }
+ }
+
+
+
+
+ }
+
+
+
+
+
+ @Override
+ public void go() {
+ TreeLinkNode t1 = new TreeLinkNode(1);
+ TreeLinkNode t2 = new TreeLinkNode(2);
+ TreeLinkNode t3 = new TreeLinkNode(3);
+ TreeLinkNode t4 = new TreeLinkNode(4);
+ TreeLinkNode t5 = new TreeLinkNode(5);
+
+ t1.left = t2;
+ t1.right = t3;
+ t2.left = t4;
+ t2.right = t5;
+
+ connect(t1);
+
+
+ System.out.println("hi");
+
+
+
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/src/Issues/Q146.java b/src/Issues/Q146.java
new file mode 100644
index 0000000..f58d1b5
--- /dev/null
+++ b/src/Issues/Q146.java
@@ -0,0 +1,92 @@
+package Issues;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Q146 implements IIssueTemplate {
+
+ int _cap;
+ int size;
+ Map map;
+ Node head;
+ Node tail;
+
+ class Node{
+ public int key;
+ public int value;
+ public Node pre;
+ public Node next;
+ public Node(){}
+ public Node(int k, int v){
+ key = k;
+ value = v;
+ }
+
+ };
+
+ public Q146(int capacity) {
+
+ _cap = capacity;
+ size = 0;
+ map = new HashMap();
+ head = new Node();
+ tail = new Node();
+ head.next = tail;
+ tail.pre = head;
+ }
+
+ public int get(int key) {
+ if(map.containsKey(key)){
+ Node node = map.get(key);
+ takeOut(node);
+ insertHead(node);
+ return node.value;
+ }
+ return -1;
+ }
+
+ public void put(int key, int value) {
+ if(_cap == 0){
+ return;
+ }
+
+ if(map.containsKey(key)){
+ Node node = map.get(key);
+ node.value = value;
+ takeOut(node);
+ insertHead(node);
+ }else{
+ Node newNode = new Node(key, value);
+ map.put(key, newNode);
+ if(size < _cap){
+ size++;
+ }else{
+ Node last = tail.pre;
+ takeOut(last);
+ map.remove(last.key);
+ }
+ insertHead(newNode);
+ }
+ }
+
+ private void takeOut(Node node){
+ node.pre.next = node.next;
+ node.next.pre = node.pre;
+ }
+
+ private void insertHead(Node node){
+ node.next = head.next;
+ node.next.pre = node;
+ node.pre = head;
+ head.next = node;
+ }
+ @Override
+ public void go() {
+ put(2,1);
+ get(2);
+ put(3,2);
+ get(2);
+ get(3);
+ }
+
+}
diff --git a/src/Issues/Q160.java b/src/Issues/Q160.java
new file mode 100644
index 0000000..4ecf589
--- /dev/null
+++ b/src/Issues/Q160.java
@@ -0,0 +1,88 @@
+package Issues;
+
+import Utils.InputFactory;
+import model.ListNode;
+
+public class Q160 implements IIssueTemplate {
+
+ //3ms
+ public ListNode getIntersectionNode_II(ListNode headA, ListNode headB) {
+
+ int l1 = getLength(headA);
+ int l2 = getLength(headB);
+
+ while(l1 > l2){
+ headA = headA.next;
+ l1--;
+ }
+ while(l1 < l2){
+ headB = headB.next;
+ l2--;
+ }
+
+ while(headA != headB){
+ headA = headA.next;
+ headB = headB.next;
+ }
+
+ return headA;
+ }
+
+ private int getLength(ListNode node){
+ if(node == null){
+ return 0;
+ }
+
+ return getLength(node.next)+1;
+ }
+// 2ms
+public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
+
+ if(headA== null || headB==null){
+ return null;
+ }
+
+ ListNode last = headA;
+
+ while(last.next != null){
+ last = last.next;
+ }
+
+ last.next = headA;
+
+ ListNode fast = headB;
+ ListNode slow = headB;
+ while(fast.next != null && fast.next.next != null){
+
+ fast = fast.next.next;
+ slow = slow.next;
+
+ if(fast == slow){
+ break;
+ }
+ }
+
+ if(fast.next == null || fast.next.next == null){
+ last.next = null;
+ return null;
+ }
+ fast = headB;
+ while(fast != slow){
+ fast = fast.next;
+ slow = slow.next;
+ }
+
+ last.next = null;
+ return fast;
+ }
+
+ @Override
+ public void go() {
+ ListNode a = InputFactory.getListNode(new int[]{1});
+ ListNode b = InputFactory.getListNode(new int[]{2,4,6,8,10,12,14,16,18,20,22});
+
+ getIntersectionNode(a,b);
+
+ }
+
+}
diff --git a/src/Issues/Q189.java b/src/Issues/Q189.java
new file mode 100644
index 0000000..674d3d3
--- /dev/null
+++ b/src/Issues/Q189.java
@@ -0,0 +1,36 @@
+package Issues;
+
+public class Q189 implements IIssueTemplate {
+
+ public void rotate(int[] nums, int k) {
+
+ int len = nums.length;
+ if(len == 0 || k%len == 0){
+ return;
+ }
+
+ reverse(nums, 0, len-1);
+ reverse(nums, 0, k-1);
+ reverse(nums, k, len-1);
+
+ }
+
+ private void reverse(int[] nums, int start, int end){
+
+ while(start < end){
+ int tmp = nums[start];
+ nums[start] = nums[end];
+ nums[end] = tmp;
+ start++;
+ end--;
+ }
+
+ }
+
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/src/Issues/Q190.java b/src/Issues/Q190.java
new file mode 100644
index 0000000..f35305e
--- /dev/null
+++ b/src/Issues/Q190.java
@@ -0,0 +1,32 @@
+package Issues;
+
+public class Q190 implements IIssueTemplate {
+ /*
+ * -2147483648 -> 10000000 00000000 00000000 00000000
+ * 2147483647 -> 01111111 11111111 11111111 11111111
+ *
+ * the loop should go 32 times
+ * make sure do shift bit first, then assign, the order does matter!!!!!
+ */
+
+ public int reverseBits(int n) {
+
+ int result = 0;
+
+ for(int i =1 ;i <=32 ; i++){
+ result <<= 1;
+ result |= (n & 1);
+ n >>>= 1;
+ System.out.println(Integer.toBinaryString(n));
+ System.out.println(" "+Integer.toBinaryString(result));
+ }
+ return result;
+ }
+ @Override
+ public void go() {
+
+ System.out.println(reverseBits(-2147483648));
+
+ }
+
+}
diff --git a/src/Issues/Q210.java b/src/Issues/Q210.java
new file mode 100644
index 0000000..46cfff2
--- /dev/null
+++ b/src/Issues/Q210.java
@@ -0,0 +1,87 @@
+package Issues;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class Q210 implements IIssueTemplate {
+
+ public int[] findOrder(int numCourses, int[][] prerequisites) {
+ int[] parent = new int[numCourses];
+
+ List> adjList = new ArrayList();
+ for(int i = 0; i < numCourses; i++){
+ parent[i] = -1;
+ adjList.add(new ArrayList());
+ }
+
+ for(int i = 0; i < prerequisites.length; i++){
+ adjList.get(prerequisites[i][0]).add(prerequisites[i][1]);
+ }
+
+
+ List res = new ArrayList(numCourses);
+// Set visited = new HashSet();
+
+ for(int i = 0; i < numCourses; i++){
+ if(adjList.get(i).size() == 0){
+ parent[i] = -2;
+ res.add(i);
+ process(adjList, i, parent, res);
+ }
+ }
+ if(cycle){
+ return new int[0];
+ }
+ int[] result = new int[numCourses];
+
+ for(int i = 0; i < numCourses ;i++){
+ result[i] = res.get(i);
+ }
+
+ return result;
+
+
+ }
+ boolean cycle = false;
+ void process(List> adjList, int index, int[] parent, List res){
+
+ if(cycle){
+ return;
+ }
+
+ List neighbors = adjList.get(index);
+
+ for(int to : neighbors){
+ if(parent[to] == -1){
+ parent[to] = index;
+// visited.add(to);
+ res.add(to);
+ process(adjList, to, parent, res);
+ }
+ else if(parent[to] != index){
+ cycle = true;
+ return;
+ }
+ }
+
+
+ }
+
+
+
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ int [][] a = new int[][]{{1,0}};
+
+ int[] res = findOrder(2, a);
+
+ System.out.println("HI");
+ }
+
+}
diff --git a/src/Issues/Q218.java b/src/Issues/Q218.java
new file mode 100644
index 0000000..18315bf
--- /dev/null
+++ b/src/Issues/Q218.java
@@ -0,0 +1,49 @@
+package Issues;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+
+public class Q218 implements IIssueTemplate{
+
+ public List getSkyline(int[][] buildings) {
+ List result = new ArrayList<>();
+ List height = new ArrayList<>();
+ for(int[] b:buildings) {
+ height.add(new int[]{b[0], -b[2]});
+ height.add(new int[]{b[1], b[2]});
+ }
+ Collections.sort(height, (a, b) -> {
+ if(a[0] != b[0])
+ return a[0] - b[0];
+ return a[1] - b[1];
+ });
+ PriorityQueue pq = new PriorityQueue(Collections.reverseOrder());
+ pq.offer(0);
+ int prev = 0;
+ for(int[] h:height) {
+ if(h[1] < 0) {
+ pq.offer(-h[1]);
+ } else {
+ pq.remove(h[1]);
+ }
+ int cur = pq.peek();
+ if(prev != cur) {
+ result.add(new int[]{h[0], cur});
+ prev = cur;
+ }
+ }
+ return result;
+ }
+ @Override
+ public void go() {
+ int[][] input = new int[][]{{5,6,6},{5,7,5},{5,8,4}};
+
+ Listres = getSkyline(input);
+ System.out.println(res);
+
+ }
+
+}
diff --git a/src/Issues/Q281.java b/src/Issues/Q281.java
new file mode 100644
index 0000000..0687d19
--- /dev/null
+++ b/src/Issues/Q281.java
@@ -0,0 +1,44 @@
+package Issues;
+
+import java.util.Iterator;
+import java.util.List;
+
+public class Q281 implements IIssueTemplate{
+
+
+ Iterator it;
+ Iterator it1;
+ Iterator it2;
+ public void ZigzagIterator(List v1, List v2) {
+ it1 = v1.iterator();
+ it2 = v2.iterator();
+
+ if(it1.hasNext()){
+ it = it1;
+ }else{
+ it = it2;
+ }
+
+ }
+
+ public int next() {
+
+ int res = it.next();
+
+ if(it == it1 && it2.hasNext()){
+ it = it2;
+ }else if(it == it2 && it1.hasNext()){
+ it = it1;
+ }
+ return res;
+ }
+
+ public boolean hasNext() {
+ return it.hasNext();
+ }
+
+ @Override
+ public void go() {
+
+ }
+}
diff --git a/src/Issues/Q301.java b/src/Issues/Q301.java
new file mode 100644
index 0000000..425031c
--- /dev/null
+++ b/src/Issues/Q301.java
@@ -0,0 +1,104 @@
+package Issues;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+//BFS is faster , see Leetcode
+
+
+public class Q301 implements IIssueTemplate {
+
+
+ public List removeInvalidParentheses(String s) {
+ int start = 0;
+ int end = s.length()-1;
+ for(; start < s.length(); start++){
+ if(s.charAt(start) != ')'){
+ break;
+ }
+ }
+ for(; end >= 0; end--){
+ if(s.charAt(end) != '('){
+ break;
+ }
+ }
+
+ s = s.substring(start, end+1);
+
+ List res = new ArrayList();
+ StringBuilder sb = new StringBuilder();
+ process(s, 0, sb, res);
+
+ return res;
+
+
+ }
+
+ void process(String ori, int index, StringBuilder sb, List res){
+ if(index == ori.length()){
+ String str = sb.toString();
+ if(verify(str)){
+ if(res.isEmpty()){
+ res.add(str);
+ }else if(res.get(0).length() == str.length() && !res.contains(str)){
+ res.add(str);
+ }else if(res.get(0).length() < str.length()){
+ res.clear();
+ res.add(str);
+ }
+ }
+ return;
+ }
+ if(ori.charAt(index) != '(' && ori.charAt(index) != ')'){
+ sb.append(ori.charAt(index));
+ process(ori, index+1, sb, res);
+ }
+ else{
+ sb.append(ori.charAt(index));
+ process(ori, index+1, sb, res);
+ sb.deleteCharAt(sb.length()-1);
+
+ process(ori, index+1, sb, res);
+ }
+
+
+
+ }
+
+ boolean verify(String str){
+ LinkedList stk = new LinkedList();
+
+ for(int i = 0; i < str.length(); i++){
+ if(str.charAt(i) != '(' && str.charAt(i) != ')'){
+ continue;
+ }
+
+ if(str.charAt(i) == '('){
+ stk.push('(');
+ }else{
+ if(stk.size() > 0){
+ stk.pop();
+ }else{
+ return false;
+ }
+ }
+
+
+ }
+
+ if(stk.size() > 0){
+ return false;
+ }
+
+ return true;
+ }
+
+
+ @Override
+ public void go() {
+ List res = removeInvalidParentheses(")(f");
+
+ }
+
+}
diff --git a/src/Issues/Q346.java b/src/Issues/Q346.java
new file mode 100644
index 0000000..62730b3
--- /dev/null
+++ b/src/Issues/Q346.java
@@ -0,0 +1,43 @@
+package Issues;
+
+import java.util.LinkedList;
+
+public class Q346 implements IIssueTemplate{
+ @Override
+ public void go() {
+
+ Q346 q = new Q346();
+ q.MovingAverage(3);
+ q.next(1);
+ q.next(10);
+ double a = q.next(3);
+ q.next(5);
+ }
+
+ LinkedList list;
+ int count = 0;
+ double sum = 0;
+ int size = 0;
+
+ /** Initialize your data structure here. */
+ public void MovingAverage(int size) {
+
+ list = new LinkedList();
+ this.size = size;
+ }
+
+ public double next(int val) {
+ if(count < size){
+ count++;
+ sum += val;
+ list.offer(val);
+ return sum / count;
+ }
+
+ int out = list.poll();
+ sum -= out;
+ sum += val;
+ list.offer(val);
+ return sum/size;
+ }
+}
diff --git a/src/Issues/Q347.java b/src/Issues/Q347.java
new file mode 100644
index 0000000..88c24fa
--- /dev/null
+++ b/src/Issues/Q347.java
@@ -0,0 +1,72 @@
+package Issues;
+
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.AbstractMap;
+import java.util.ArrayList;
+
+public class Q347 implements IIssueTemplate {
+
+ public List topKFrequent(int[] nums, int k) {
+
+ Map map = new HashMap();
+
+
+
+ TreeMap> sortedMap = new TreeMap>(new Comparator(){
+ @Override
+ public int compare(Integer a, Integer b){
+ return b-a;
+ }
+
+
+ });
+
+ sortedMap.put(1, new ArrayList());
+
+ for(int num : nums){
+ if(map.keySet().contains(num)){
+ int count = map.get(num);
+ map.put(num, count+1);
+
+ int index = sortedMap.get(count).indexOf(num);
+ sortedMap.get(count).remove(index);
+
+ if(sortedMap.keySet().contains(count+1)){
+ sortedMap.get(count+1).add(num);
+ }else{
+ List list = new ArrayList();
+ list.add(num);
+ sortedMap.put(count+1, list);
+ }
+
+ }else{
+ map.put(num, 1);
+ sortedMap.get(1).add(num);
+ }
+ }
+
+
+ List result = new ArrayList();
+ for(Map.Entry> entry : sortedMap.entrySet() ){
+ if(k == 0){
+ break;
+ }
+ for(int val : entry.getValue()){
+ result.add(val);
+ }
+ k--;
+ }
+
+ return result;
+ }
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/src/Issues/Q351.java b/src/Issues/Q351.java
new file mode 100644
index 0000000..1827740
--- /dev/null
+++ b/src/Issues/Q351.java
@@ -0,0 +1,102 @@
+package Issues;
+
+/**
+ * Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤
+ * n ≤ 9, count the total number of unlock patterns of the Android lock screen,
+ * which consist of minimum of m keys and maximum n keys.
+ *
+ * Rules for a valid pattern: Each pattern must connect at least m keys and at
+ * most n keys. All the keys must be distinct. If the line connecting two
+ * consecutive keys in the pattern passes through any other keys, the other keys
+ * must have previously selected in the pattern. No jumps through non selected
+ * key is allowed. The order of keys used matters.
+ *
+ * Explanation: | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | Invalid move: 4 - 1 -
+ * 3 - 6 Line 1 - 3 passes through key 2 which had not been selected in the
+ * pattern.
+ *
+ * Invalid move: 4 - 1 - 9 - 2 Line 1 - 9 passes through key 5 which had not
+ * been selected in the pattern.
+ *
+ * Valid move: 2 - 4 - 1 - 3 - 6 Line 1 - 3 is valid because it passes through
+ * key 2, which had been selected in the pattern
+ *
+ * Valid move: 6 - 5 - 4 - 1 - 9 - 2 Line 1 - 9 is valid because it passes
+ * through key 5, which had been selected in the pattern.
+ *
+ * Example: Given m = 1, n = 1, return 9.
+ *
+ */
+
+public class Q351 {
+ public int numberOfPatterns(int m, int n) {
+
+ boolean[] visited = new boolean[10];
+ for (int i = 0; i < 10; i++) {
+ visited[i] = false;
+ }
+
+ int[][] mapping = new int[10][10];
+
+ mapping[1][3] = 2;
+ mapping[1][7] = 4;
+ mapping[1][9] = 5;
+ mapping[2][8] = 5;
+ mapping[3][1] = 2;
+ mapping[3][7] = 5;
+ mapping[3][9] = 6;
+ mapping[4][6] = 5;
+ mapping[6][4] = 5;
+ mapping[7][1] = 4;
+ mapping[7][3] = 5;
+ mapping[7][9] = 8;
+ mapping[8][2] = 5;
+ mapping[9][1] = 5;
+ mapping[9][3] = 6;
+ mapping[9][7] = 8;
+
+ // 1
+ visited[1] = true;
+
+ int count1 = process(1, m, n, visited, mapping, 1);
+ // 2
+ visited[1] = false;
+ visited[2] = true;
+
+ int count2 = process(1, m, n, visited, mapping, 2);
+
+ // 5
+ visited[2] = false;
+ visited[5] = true;
+
+ int count5 = process(1, m, n, visited, mapping, 5);
+
+ return count1 * 4 + count2 * 4 + count5;
+
+ }
+
+ int process(int level, int m, int n, boolean[] visited, int[][] mapping, int current) {
+ if (level == n) {
+ return 1;
+ }
+
+ int count = 0;
+
+ for (int i = 1; i <= 9; i++) {
+ if (!visited[i]) {
+ if (mapping[current][i] == 0 || visited[mapping[current][i]]) {
+ visited[i] = true;
+ count += process(level + 1, m, n, visited, mapping, i);
+ visited[i] = false;
+ }
+ }
+ }
+
+ if (level >= m) {
+ count += 1;
+ }
+
+ return count;
+
+ }
+}
diff --git a/src/Issues/Q366.java b/src/Issues/Q366.java
new file mode 100644
index 0000000..81f0152
--- /dev/null
+++ b/src/Issues/Q366.java
@@ -0,0 +1,66 @@
+package Issues;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import Utils.InputFactory;
+import model.TreeNode;
+
+public class Q366 implements IIssueTemplate {
+
+ public List> findLeaves(TreeNode root) {
+
+ ArrayList> result = new ArrayList>();
+ if(root == null){
+ return result;
+ }
+
+ process(root, result);
+ return result;
+
+ }
+
+ int process(TreeNode node, ArrayList> result){
+
+ if(node.left == null && node.right == null){
+ if(result.isEmpty()){
+ ArrayList tmp = new ArrayList();
+ tmp.add(node.val);
+ result.add(tmp);
+
+ }else{
+ result.get(0).add(node.val);
+ }
+
+ return 0;
+ }
+ int left = node.left == null ? -1 : process(node.left, result)+1;
+ int right = node.right == null ? -1 : process(node.right, result)+1;
+
+ int depth = Math.max(left, right);
+ if(result.size() -1 < depth){
+ ArrayList tmp = new ArrayList();
+ tmp.add(node.val);
+ result.add(tmp);
+ }else{
+ result.get(depth).add(node.val);
+ }
+
+ return depth;
+
+
+
+ }
+
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ String input = "1,2,3,4,null,7,10,5,6,8,9";
+ TreeNode root = InputFactory.getTreeFromString(input);
+
+ List> result = findLeaves(root);
+
+ }
+
+}
diff --git a/src/Issues/Q370.java b/src/Issues/Q370.java
new file mode 100644
index 0000000..ff31750
--- /dev/null
+++ b/src/Issues/Q370.java
@@ -0,0 +1,32 @@
+package Issues;
+
+public class Q370 implements IIssueTemplate {
+
+ public int[] getModifiedArray(int length, int[][] updates) {
+
+ int[] result = new int[length];
+
+ for(int i = 0; i < updates.length; i++){
+ result[updates[i][0]] += updates[i][2];
+
+ if(updates[i][1] < length-1){
+ result[updates[i][1]+1] += updates[i][2];
+ }
+ }
+ int sum = 0;
+ for(int i = 0; i < length; i++){
+ sum+=result[i];
+ result[i] = sum;
+ }
+
+ return result;
+
+
+ }
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/src/Issues/Q380.java b/src/Issues/Q380.java
new file mode 100644
index 0000000..18d67fe
--- /dev/null
+++ b/src/Issues/Q380.java
@@ -0,0 +1,66 @@
+package Issues;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+public class Q380 implements IIssueTemplate {
+
+ public class RandomizedSet {
+
+
+ List list;
+ Random rand;
+ Map map;
+ /** Initialize your data structure here. */
+ public RandomizedSet() {
+ list = new ArrayList();
+ rand = new Random();
+ map = new HashMap();
+ }
+
+ /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
+ public boolean insert(int val) {
+ if(map.containsKey(val)){
+ return false;
+ }
+
+ list.add(val);
+ map.put(val, list.size()-1);
+ return true;
+ }
+
+ /** Removes a value from the set. Returns true if the set contained the specified element. */
+ public boolean remove(int val) {
+ if(map.containsKey(val)){
+
+ int index = map.get(val);
+
+ int tmp = list.get(list.size()-1);
+ map.put(tmp, index);
+ map.remove(val);
+ list.set(index, tmp);
+ list.remove(list.size()-1);
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /** Get a random element from the set. */
+ public int getRandom() {
+ int r = rand.nextInt(list.size());
+
+ return list.get(r);
+ }
+ }
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/src/Issues/Q388.java b/src/Issues/Q388.java
new file mode 100644
index 0000000..ea8c5b6
--- /dev/null
+++ b/src/Issues/Q388.java
@@ -0,0 +1,40 @@
+package Issues;
+
+import java.util.ArrayList;
+
+public class Q388 implements IIssueTemplate {
+
+
+ public int lengthLongestPath(String input) {
+
+ String[] inputArray = input.split("\n");
+ int[] tracker = new int[inputArray.length+1];
+
+ int res = 0;
+
+
+ for(String str : inputArray){
+ String[] elementArray = str.split("\t");
+ int len = elementArray.length;
+ String last = elementArray[len-1];
+
+ if(last.contains(".")){//this is a file
+
+ res = Math.max(res, tracker[len-1]+last.length()+len-1);
+
+ }else{ // dir
+ tracker[len] = tracker[len-1]+last.length();
+
+ }
+ }
+
+ return res;
+ }
+
+ @Override
+ public void go() {
+ String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
+
+ System.out.println(lengthLongestPath(input));
+ }
+}
diff --git a/src/Issues/Q400.java b/src/Issues/Q400.java
new file mode 100644
index 0000000..20d0507
--- /dev/null
+++ b/src/Issues/Q400.java
@@ -0,0 +1,52 @@
+package Issues;
+
+public class Q400 implements IIssueTemplate {
+
+public int findNthDigit(int n) {
+
+ int base = 1;
+ int length = 1;
+ // this is the key point long !!!!!
+ long count = 9;
+// int tmp = 0;
+
+ while( count*length < n){
+ n -= count*length;
+ base *= 10;
+ count *=10;
+ length++;
+ }
+
+ int number = base + (n-1)/length;
+ String str = Integer.toString(number);
+
+ return Character.getNumericValue(str.charAt((n-1)%length));
+
+
+ }
+
+/*
+ public int findNthDigit(int n) {
+ int len = 1;
+ long count = 9;
+ int start = 1;
+
+ while (n > len * count) {
+ n -= len * count;
+ len += 1;
+ count *= 10;
+ start *= 10;
+ }
+
+ start += (n - 1) / len;
+ String s = Integer.toString(start);
+ return Character.getNumericValue(s.charAt((n - 1) % len));
+ }
+*/
+ @Override
+ public void go() {
+ System.out.println(findNthDigit(1000000000));
+
+ }
+
+}
diff --git a/src/Issues/Q406.java b/src/Issues/Q406.java
new file mode 100644
index 0000000..1b5b6d1
--- /dev/null
+++ b/src/Issues/Q406.java
@@ -0,0 +1,42 @@
+package Issues;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+
+public class Q406 implements IIssueTemplate{
+
+ public int[][] reconstructQueue(int[][] people) {
+ int len = people.length;
+ Arrays.sort(people, new Comparator(){
+
+ public int compare(int[] first, int[] second){
+ if(first[0] != second[0]){
+ return second[0]-first[0];
+ }else{
+ return first[1]-second[1];
+ }
+ }
+
+ });
+
+
+ ArrayList result = new ArrayList();
+
+
+ for(int i = 0; i < len; i++){
+ result.add(people[i][1], people[i]);
+ }
+
+ return result.toArray(new int[len][]);
+ }
+
+ @Override
+ public void go() {
+
+ int[][] input = new int[][]{{7,0}, {4,4}, {7,1}, {5,0}, {6,1}, {5,2}};
+
+ int[][] result = reconstructQueue(input);
+ System.out.println("1");
+ }
+}
diff --git a/src/Issues/Q418.java b/src/Issues/Q418.java
new file mode 100644
index 0000000..249194a
--- /dev/null
+++ b/src/Issues/Q418.java
@@ -0,0 +1,71 @@
+package Issues;
+
+public class Q418 {
+ public int wordsTyping(String[] sentence, int rows, int cols) {
+
+ return smartWay(sentence, rows, cols);
+
+ }
+
+ int smartWay(String[] sentence, int rows, int cols){
+
+ int[] counts = new int[sentence.length];
+ int[] goTo = new int[sentence.length];
+
+ for(int i = 0; i < sentence.length; i++){
+ int times = 0;
+ int index = i;
+ int remain = cols;
+ while(remain >= sentence[index].length()){
+ remain -= sentence[index].length() +1;
+ if(index == sentence.length-1){
+ index = 0;
+ times++;
+ }else{
+ index ++;
+ }
+ }
+
+ counts[i] = times;
+ goTo[i] = index;
+ }
+
+ int index = 0;
+ int result = 0;
+ for(int row = 0; row < rows; row++){
+ result += counts[index];
+ index = goTo[index];
+ }
+
+ return result;
+ }
+
+ int bruteForce(String[] sentence, int rows, int cols){
+ int[] sizes = new int[sentence.length];
+ for(int i = 0; i < sentence.length; i++){
+ sizes[i] = sentence[i].length();
+ }
+
+ int result = 0;
+
+ int range = sentence.length;
+
+ int index = 0;
+
+ for(int i = 0 ;i < rows; i++){
+ int remain = cols;
+
+
+ while(remain >= sizes[index]){
+ remain -= sizes[index] +1;
+ if(index == range-1){
+ result ++;
+ }
+
+ index = (index+1)%range;
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/src/Issues/Q43.java b/src/Issues/Q43.java
new file mode 100644
index 0000000..a92f73b
--- /dev/null
+++ b/src/Issues/Q43.java
@@ -0,0 +1,77 @@
+package Issues;
+
+public class Q43 implements IIssueTemplate {
+
+ public String multiply(String num1, String num2) {
+ int l1 = num1.length();
+ int l2 = num2.length();
+
+ if(l1 < l2){
+ String tmp = num1;
+ num1 = num2;
+ num2 = tmp;
+ }
+
+ int[] array = new int[l1+l2];
+
+ int startIndex = l1+l2-1;
+ int lastIndex = 0;
+
+ for(int i = num2.length()-1; i >=0; i--){
+ lastIndex = process(num1, num2.charAt(i), startIndex, array);
+ startIndex--;
+ }
+
+ StringBuilder sb = new StringBuilder();
+
+
+ for(; lastIndex <= l1+l2-1; lastIndex++){
+ if(array[lastIndex] != 0)break;
+
+ }
+
+ for(int i = lastIndex ; i <= l1+l2-1; i++){
+ sb.append((char)(array[i]+48));
+ }
+
+
+ return sb.length() == 0 ? "0": sb.toString();
+
+ }
+
+
+
+ int process(String num1, char chr, int startIndex, int[] array){
+
+ int carrier = 0;
+
+ for(int i = num1.length()-1; i >=0; i--){
+ int a = num1.charAt(i)-48;
+ int b = chr-48;
+
+ int result = a*b+array[startIndex]+carrier;
+ array[startIndex] = result%10;
+ carrier = result/10;
+ startIndex--;
+
+ }
+ if(carrier != 0){
+ array[startIndex] = carrier;
+
+ return startIndex;
+ }
+
+ return startIndex+1;
+
+ }
+ @Override
+ public void go() {
+
+ String s1 = "1";
+ String s2 = "1";
+ String result = multiply(s1,s2);
+
+
+ }
+
+}
diff --git a/src/Issues/Q445.java b/src/Issues/Q445.java
new file mode 100644
index 0000000..838ed1c
--- /dev/null
+++ b/src/Issues/Q445.java
@@ -0,0 +1,79 @@
+package Issues;
+
+import model.ListNode;
+
+public class Q445 implements IIssueTemplate {
+ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
+
+ if(l1 == null && l2 == null){
+ return null;
+ }else if(l1 == null){
+ return l2;
+ }else if(l2 == null){
+ return l1;
+ }
+ int len1 = getLength(l1);
+ int len2 = getLength(l2);
+
+ if(len2 > len1){
+ ListNode tmp = l1;
+ l1 = l2;
+ l2 = tmp;
+ }
+
+
+ int diff = Math.abs(len1 -len2);
+ ListNode head = new ListNode(-1);
+
+ int carrier = process(l1, l2, diff, head);
+ if(carrier != 0){
+ ListNode tmp = new ListNode(1);
+ tmp.next = head.next;
+ head.next = tmp;
+ }
+ return head.next;
+
+ }
+
+ int process(ListNode l1, ListNode l2, int diff, ListNode head){
+ if(diff > 0){
+ int carrier = process(l1.next, l2, diff-1, head);
+
+ ListNode tmp = new ListNode((l1.val+carrier)%10);
+ tmp.next = head.next;
+ head.next = tmp;
+ return (l1.val+carrier)/10;
+ }else{
+ if(l1 == null && l2 == null){
+ return 0;
+ }
+
+ int carrier = process(l1.next, l2.next, 0, head);
+
+ ListNode tmp = new ListNode((l1.val + l2.val + carrier)%10);
+ tmp.next = head.next;
+ head.next = tmp;
+ return (l1.val + l2.val + carrier)/10;
+ }
+
+ }
+
+ int getLength(ListNode node){
+ ListNode tmp = node;
+ int res = 0;
+
+ while(tmp != null){
+ res++;
+ tmp = tmp.next;
+
+ }
+ return res;
+ }
+
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/src/Issues/Q475.java b/src/Issues/Q475.java
new file mode 100644
index 0000000..11f755f
--- /dev/null
+++ b/src/Issues/Q475.java
@@ -0,0 +1,82 @@
+package Issues;
+
+import java.util.Arrays;
+
+public class Q475 implements IIssueTemplate {
+
+
+ /*
+ * binary search solution
+ *
+ */
+/*
+ public int findRadius(int[] houses, int[] heaters) {
+
+ int record = -1; // can not be negative
+ int end = heaters.length-1;
+ Arrays.sort(heaters);
+ for(int house : houses){
+ record = Math.max(record, getNearest(house, heaters, 0, end));
+ }
+
+ return record;
+
+ }
+
+ int getNearest(int target, int[] heaters, int start, int end){
+
+ if(end < start){
+ return Integer.MAX_VALUE;
+ }
+
+ int mid = start+ (end-start)/2;
+
+ if(heaters[mid] == target){
+ return 0;
+ }
+ else if(heaters[mid] > target){
+ return Math.min(heaters[mid]-target, getNearest(target, heaters, start, mid-1));
+ }else{
+ return Math.min(target- heaters[mid], getNearest(target, heaters, mid+1, end));
+ }
+ }
+ */
+
+ /**
+ * two pointer solution , beat 95% , 18 ms
+ */
+ public int findRadius(int[] houses, int[] heaters) {
+ Arrays.sort(houses);
+ Arrays.sort(heaters);
+ int record = 0;
+ int start = 0;
+ int len = heaters.length;
+
+ for(int house : houses){
+ for(; start < len; start++){
+ if(heaters[start] >= house){
+ break;
+ }
+ }
+ if(start == 0){
+ record = Math.max(record, heaters[0]-house);
+ }else if(start == len){
+ record = Math.max(record, house - heaters[start-1]);
+ }else{
+ record = Math.max(record, Math.min(house-heaters[start-1], heaters[start]-house));
+ }
+ }
+
+
+ return record;
+ }
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ int result = findRadius(new int[]{282475249,622650073,984943658,144108930,470211272,101027544,457850878,458777923},
+ new int[]{823564440,115438165,784484492,74243042,114807987,137522503,441282327,16531729,823378840,143542612});
+ System.out.println(result);
+ }
+
+}
diff --git a/src/Issues/Q484.java b/src/Issues/Q484.java
new file mode 100644
index 0000000..9fc41b0
--- /dev/null
+++ b/src/Issues/Q484.java
@@ -0,0 +1,83 @@
+package Issues;
+
+import java.util.LinkedList;
+
+public class Q484 implements IIssueTemplate {
+
+ int[] result;
+ boolean terminate = false;
+
+ public int[] findPermutation(String s) {
+ int n = s.length()+1;
+ if(n == 1){
+ return new int[]{1};
+ }
+
+ boolean[] flags = new boolean[n+1];
+
+ LinkedList res = new LinkedList();
+
+ for(int i = 1; i <= n; i++){
+ res.offer(i);
+ flags[i] = true;
+ process(res, s, 0, s.length(), flags);
+ res.poll();
+ flags[i] = false;
+ }
+
+ return result;
+
+
+ }
+
+ void process(LinkedList res, String s, int level, int end, boolean[] flags){
+ if(terminate){
+ return;
+ }
+
+ if(level == end){
+ // terminate
+ terminate = true;
+ result = new int[res.size()];
+ for(int i = 0 ; i < res.size(); i++){
+ result[i] = res.get(i);
+ }
+ return;
+ }
+
+ if(s.charAt(level) == 'I'){
+ int top = res.peek();
+ for(int i = 1; i < flags.length; i++){
+ if(!flags[i] && i >top){
+ flags[i] = true;
+ res.offer(i);
+ process(res, s, level+1, end, flags);
+ res.poll();
+ flags[i] = false;
+ }
+ }
+ }else{
+ int top = res.peek();
+ for(int i = 1; i < flags.length; i++){
+ if(!flags[i] && i map = new HashMap();
+ map.put(nums[0],1);
+ map.put(-nums[0], 1);
+ for(int i = 1; i< nums.length; i++){
+ int tmp = nums[i];
+ Map tmpM = new HashMap();
+ for( int key : map.keySet()){
+ tmpM.put(key+tmp, map.get(key)+tmpM.getOrDefault(key+tmp,0));
+ tmpM.put(key-tmp, map.get(key)+tmpM.getOrDefault(key-tmp,0));
+ }
+ map = tmpM;
+ }
+
+ return map.get(S) == null ? 0 : map.get(S);
+
+ }
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+ int[] input = {1,1,1,1,1};
+ int res = findTargetSumWays(input, 3);
+ }
+
+}
diff --git a/src/Issues/Q529.java b/src/Issues/Q529.java
new file mode 100644
index 0000000..1f22683
--- /dev/null
+++ b/src/Issues/Q529.java
@@ -0,0 +1,119 @@
+package Issues;
+
+import java.util.LinkedList;
+
+public class Q529 implements IIssueTemplate {
+
+ char[][] self;
+ int rows = 0;
+ int cols = 0;
+
+
+ public char[][] updateBoard(char[][] board, int[] click) {
+ self = board;
+ rows = board.length-1;
+ cols = board[0].length-1;
+
+
+ LinkedList coords = new LinkedList();
+
+ if(board[click[0]][click[1]] == 'M'){
+ board[click[0]][click[1]] = 'X';
+ return board;
+ }else{
+ coords.add(new int[]{click[0], click[1]});
+ process(board, coords);
+ }
+
+ return board;
+
+ }
+
+ private void process(char[][]board, LinkedList coords){
+
+ while(!coords.isEmpty()){
+ int[] tmp = coords.poll();
+ if(board[tmp[0]][tmp[1]] == 'E'){
+ int count = countMine(tmp[0], tmp[1]);
+ if(count == 0){
+ board[tmp[0]][tmp[1]] = 'B';
+ // check neighbores
+ if(tmp[0]>0 && board[tmp[0]-1][tmp[1]] == 'E'){
+ coords.offer(new int[]{tmp[0]-1, tmp[1]});
+ }
+ if(tmp[0] < rows && board[tmp[0]+1][tmp[1]] == 'E'){
+ coords.offer(new int[]{tmp[0]+1, tmp[1]});
+ }
+
+ if(tmp[1] >0 && board[tmp[0]][tmp[1]-1] == 'E'){
+ coords.offer(new int[]{tmp[0],tmp[1]-1});
+ }
+
+ if(tmp[1] < cols && board[tmp[0]][tmp[1]+1] == 'E'){
+ coords.offer(new int[]{tmp[0], tmp[1]+1});
+ }
+
+ }else{
+ board[tmp[0]][tmp[1]] = (char)(count+48);
+ }
+ }
+ }
+ }
+
+
+ private int countMine(int row, int col){
+ int res = 0;
+
+ if(row >0 && col > 0 && self[row-1][col-1] == 'M'){
+ res ++;
+ }
+ if(row >0 && self[row-1][col] == 'M'){
+ res ++;
+ }
+ if(row > 0 && col < cols && self[row-1][col+1] == 'M'){
+ res ++;
+ }
+
+ if(col > 0 && self[row][col-1] == 'M'){
+ res++;
+ }
+
+ if(col < cols && self[row][col+1] == 'M'){
+ res ++;
+ }
+
+ if(row < rows && col > 0 && self[row+1][col-1] == 'M'){
+ res++;
+ }
+ if(row < rows && self[row+1][col] == 'M'){
+ res++;
+ }
+ if(row < rows && col < cols && self[row+1][col+1] == 'M'){
+ res++;
+ }
+ return res;
+
+ }
+
+
+ @Override
+ public void go() {
+
+// ["EEEEEEEE","EEEEEEEM","EEMEEEEE","MEEEEEEE","EEEEEEEE","EEEEEEEE","EEEEEEEE","EEMMEEEE"]
+
+
+ // TODO Auto-generated method stub
+ char[][] board = new char[][]{"EEEEEEEE".toCharArray(),
+ "EEEEEEEM".toCharArray(),
+ "EEMEEEEE".toCharArray(),
+ "MEEEEEEE".toCharArray(),
+ "EEEEEEEE".toCharArray(),
+ "EEEEEEEE".toCharArray(),
+ "EEEEEEEE".toCharArray(),
+ "EEMMEEEE".toCharArray()};
+
+ char[][] result = updateBoard(board,new int[]{0,0});
+
+ }
+
+}
diff --git a/src/Issues/Q68.java b/src/Issues/Q68.java
new file mode 100644
index 0000000..7c1dfd8
--- /dev/null
+++ b/src/Issues/Q68.java
@@ -0,0 +1,78 @@
+package Issues;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+public class Q68 implements IIssueTemplate {
+
+ public List fullJustify(String[] words, int maxWidth) {
+
+
+ List res = new ArrayList();
+ StringBuilder sb = new StringBuilder();
+
+ LinkedList q = new LinkedList();
+ int index = 0;
+ while(index < words.length){
+ sb.setLength(0);
+
+ q.offer(words[index]);
+ int count = 1;
+ int length = words[index].length();
+ index++;
+
+ while(index < words.length && (length +words[index].length() +1 <= maxWidth)){
+
+ length += words[index].length()+1;
+ count ++;
+ q.offer(words[index]);
+ index++;
+ }
+
+ if(index == words.length){
+ while(!q.isEmpty()){
+ sb.append(q.poll());
+ if(!q.isEmpty()){
+ sb.append(' ');
+ }
+ }
+ }else{
+ int spaces = maxWidth-length + count-1;
+ int spacePer= count == 1 ? 0 : spaces/(count-1);
+ int remain = count == 1 ? 0 : spaces%(count-1);
+
+ while(!q.isEmpty()){
+ sb.append(q.poll());
+ if(!q.isEmpty()){
+ for(int i = 1; i <= spacePer; i++){
+ sb.append(' ');
+ }
+ if(remain > 0){
+ sb.append(' ');
+ remain--;
+ }
+ }
+
+ }
+ }
+
+
+ while(sb.length() < maxWidth){
+ sb.append(' ');
+ }
+ res.add(sb.toString());
+
+ }
+
+ return res;
+ }
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+ String[] words = {"What","must","be","shall","be."};
+ fullJustify(words, 12);
+
+ }
+
+}
diff --git a/src/Issues/Q681.java b/src/Issues/Q681.java
new file mode 100644
index 0000000..b63647f
--- /dev/null
+++ b/src/Issues/Q681.java
@@ -0,0 +1,78 @@
+package Issues;
+
+import java.util.Arrays;
+
+public class Q681 implements IIssueTemplate {
+
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ nextClosestTime("19:34");
+ }
+
+ public String nextClosestTime(String time) {
+ int[] array = new int[4];
+ array[0] = time.charAt(0)-'0';
+ array[1] = time.charAt(1)-'0';
+ array[2] = time.charAt(3)-'0';
+ array[3] = time.charAt(4)-'0';
+
+ Arrays.sort(array);
+
+ StringBuilder sb = new StringBuilder(time);
+
+ int index = findIt(array, sb.charAt(4) - '0');
+ if(index < 3){
+ sb.setCharAt(4, (char)(array[index+1]+'0'));
+ return sb.toString();
+ }else{
+ sb.setCharAt(4, (char)(array[0]+'0'));
+ }
+
+ index = findIt(array, sb.charAt(3)-'0');
+ if(index < 3 && array[index+1] <= 5){
+ sb.setCharAt(3, (char)(array[index+1]+'0'));
+ return sb.toString();
+ }else{
+ sb.setCharAt(3, (char)(array[0]+'0'));
+ }
+
+ index = findIt(array, sb.charAt(1)-'0');
+ if(index < 3 ){
+
+ if(sb.charAt(0) == '2' && array[index+1] > 3){
+ sb.setCharAt(1, (char)(array[0]+'0'));
+ }else{
+ sb.setCharAt(1, (char)(array[index+1]+'0'));
+ return sb.toString();
+ }
+
+ }else{
+ sb.setCharAt(1, (char)(array[0]+'0'));
+ }
+
+ index = findIt(array, sb.charAt(0)-'0');
+ if(index < 3 && array[index+1] <=2){
+ sb.setCharAt(0, (char)(array[index+1]+'0'));
+ return sb.toString();
+ }else{
+ sb.setCharAt(0, (char)(array[0]+'0'));
+ }
+
+ return sb.toString();
+
+ }
+
+ private int findIt(int[] array, int num){
+
+ for(int i = 3; i >=0; i--){
+ if(array[i] == num){
+ return i;
+ }
+ }
+
+ return -1;
+
+ }
+}
diff --git a/src/Issues/Q683.java b/src/Issues/Q683.java
new file mode 100644
index 0000000..0ebea86
--- /dev/null
+++ b/src/Issues/Q683.java
@@ -0,0 +1,89 @@
+package Issues;
+
+public class Q683 implements IIssueTemplate {
+ @Override
+ public void go() {
+
+ }
+
+ public int kEmptySlots(int[] flowers, int k) {
+
+ return betterThought(flowers, k);
+ }
+
+ int betterThought(int[] flowers, int k){
+
+ int[] spots = new int[flowers.length];
+
+ for(int i = 0; i < flowers.length; i++){
+ spots[flowers[i]-1] = i+1;
+ }
+
+ int start = 0;
+ int end = start+k+1;
+ int record = Integer.MAX_VALUE;
+
+
+ for(int i = start; end < spots.length; i++){
+
+ if( i == end){
+ record = Math.min(record, Math.max(spots[start], spots[end]));
+
+ start = i;
+ end = start+k+1;
+ continue;
+ }
+
+ if(spots[i] < spots[start] || spots[i] < spots[end] ){
+
+ start = i;
+ end = start+k+1;
+ }
+
+
+ }
+
+ return (record == Integer.MAX_VALUE)?-1:record;
+
+
+
+ }
+
+
+
+
+ int originalThought(int[] flowers, int k){
+ int[] array = new int[flowers.length+1];
+
+ boolean[] isBloom = new boolean[flowers.length+1];
+
+ for(int i = 0; i < flowers.length; i++){
+
+ int pos = flowers[i];
+
+ isBloom[pos] = true;
+
+ int left = pos -k-1;
+
+ //left
+ if(left >= 1 && isBloom[left] && array[left] == array[pos]){
+ return i+1;
+ }
+
+ array[pos]++;
+ //right
+ int right = pos +k+1;
+ if(right < array.length && isBloom[right] && array[pos] == array[right]){
+ return i+1;
+ }
+
+
+ for(int j = pos+1; j < array.length; j++){
+ array[j] ++;
+ }
+
+ }
+
+ return -1;
+ }
+}
diff --git a/src/Issues/Q7.java b/src/Issues/Q7.java
new file mode 100644
index 0000000..4cf2596
--- /dev/null
+++ b/src/Issues/Q7.java
@@ -0,0 +1,38 @@
+package Issues;
+
+public class Q7 implements IIssueTemplate {
+
+ public int reverse(int x) {
+
+ // in case overflow
+ long result = 0;
+
+ while(x != 0){
+ // first make space
+ result *= 10;
+ // second assign value
+ result += x%10;
+
+ // third move source
+ x /= 10;
+
+ // check overflow
+ if(result > Integer.MAX_VALUE || result < Integer.MIN_VALUE){
+ return 0;
+ }
+
+
+ }
+
+ return (int)result;
+
+ }
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ reverse(1534236469);
+
+ }
+
+}
diff --git a/src/Issues/Q71.java b/src/Issues/Q71.java
new file mode 100644
index 0000000..eba6e12
--- /dev/null
+++ b/src/Issues/Q71.java
@@ -0,0 +1,48 @@
+package Issues;
+
+import java.util.LinkedList;
+
+public class Q71 implements IIssueTemplate {
+
+ public String simplifyPath(String path) {
+LinkedList stk = new LinkedList();
+
+ String[] str = path.split("/");
+ for(String s : str){
+ if(s.equals(".") || s.equals("")){
+ continue;
+ }else if(s.equals("..") ){
+ if(stk.isEmpty()){
+ continue;
+ }else{
+ stk.pop();
+ }
+ }
+ else{
+ stk.push(s);
+ }
+ }
+
+ if(stk.isEmpty()){
+ return "/";
+ }
+
+
+ StringBuilder sb = new StringBuilder();
+
+ for(int i = stk.size()-1; i >=0; i--){
+ sb.append("/").append(stk.get(i));
+ }
+
+ return sb.toString();
+
+
+ }
+ @Override
+ public void go() {
+
+ String res = simplifyPath("/...");
+
+ }
+
+}
diff --git a/src/Issues/Q76.java b/src/Issues/Q76.java
new file mode 100644
index 0000000..2c1d16b
--- /dev/null
+++ b/src/Issues/Q76.java
@@ -0,0 +1,71 @@
+package Issues;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Q76 implements IIssueTemplate {
+
+ public String minWindow(String s, String t) {
+ int[] map = new int[128];
+
+ for(int i = 0 ; i < t.length(); i++){
+ map[t.charAt(i)]++;
+ }
+
+ int start = 0;
+ int end = 0;
+
+ int head = 0;
+ int tail = 0;
+
+ int len = s.length()+1;
+
+ int count = t.length();
+
+ while(end < s.length()){
+
+ int val = s.charAt(end);
+
+ if(map[val] > 0){
+ count--;
+ }
+
+ end++;
+ map[val] -- ;
+
+ while(count == 0){
+ if(len > (end-start)){
+ len = end-start;
+ head = start;
+ tail = end;
+ }
+ int pre = s.charAt(start);
+ start++;
+ map[pre]++;
+ if(map[pre] >0){
+ count++;
+ }
+ }
+ }
+
+ if(len == s.length()+1){
+ return "";
+ }
+
+ return s.substring(head, tail);
+
+ }
+
+
+ @Override
+ public void go() {
+ // TODO Auto-generated method stub
+
+ String s = "bdab";
+ String t = "ab";
+
+ String res = minWindow(s, t);
+
+ }
+
+}
diff --git a/src/Utils/InputFactory.java b/src/Utils/InputFactory.java
new file mode 100644
index 0000000..271dfaa
--- /dev/null
+++ b/src/Utils/InputFactory.java
@@ -0,0 +1,59 @@
+package Utils;
+
+import java.util.LinkedList;
+
+import model.ListNode;
+import model.TreeNode;
+
+public class InputFactory {
+ public static ListNode getListNode(int[] array){
+ ListNode head = new ListNode(-1);
+ ListNode cur = head;
+ for(int a : array){
+ cur.next = new ListNode(a);
+ cur = cur.next;
+ }
+
+ return head.next;
+ }
+
+ public static TreeNode getTreeFromString(String input){
+ String[] array = input.split(",");
+ if(array.length == 0){
+ return null;
+ }
+
+ LinkedList queue = new LinkedList();
+ TreeNode root = new TreeNode(Integer.valueOf(array[0]));
+ queue.offer(root);
+
+ int i = 1;
+ while(i < array.length){
+ TreeNode node = queue.poll();
+ if(!array[i].equals("null")){
+ TreeNode left = new TreeNode(Integer.valueOf(array[i]));
+ node.left = left;
+ queue.offer(left);
+ }
+ i++;
+ if(i >= array.length){
+ break;
+ }
+
+ if(!array[i].equals("null")){
+ TreeNode right = new TreeNode(Integer.valueOf(array[i]));
+ node.right = right;
+ queue.offer(right);
+ }
+ i++;
+
+
+ }
+
+ return root;
+
+
+
+ }
+
+}
diff --git a/src/model/ListNode.java b/src/model/ListNode.java
new file mode 100644
index 0000000..632a8a9
--- /dev/null
+++ b/src/model/ListNode.java
@@ -0,0 +1,10 @@
+package model;
+
+public class ListNode {
+ public int val;
+ public ListNode next;
+ public ListNode(int x) {
+ val = x;
+ next = null;
+ }
+}
diff --git a/src/model/TreeNode.java b/src/model/TreeNode.java
new file mode 100644
index 0000000..0a94c8b
--- /dev/null
+++ b/src/model/TreeNode.java
@@ -0,0 +1,13 @@
+package model;
+
+public class TreeNode {
+ public int val;
+ public TreeNode left;
+ public TreeNode right;
+
+ public TreeNode(int value){
+ val = value;
+ left = null;
+ right= null;
+ }
+}