Add changes for building

This commit is contained in:
YuCheng Hu
2021-04-22 07:42:25 -04:00
parent 7796a7a2ec
commit de9342faec
25 changed files with 89 additions and 213 deletions
@@ -1,16 +0,0 @@
package com.ossez.lang.tutorial.models;
/**
*
* @author YuCheng
*
*/
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
next = null;
}
}
@@ -1,16 +0,0 @@
package com.ossez.lang.tutorial.models;
/**
*
* @author YuCheng
*
*/
public class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}
@@ -2,17 +2,11 @@ package com.ossez.lang.tutorial.tests;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.management.ListenerNotFoundException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -1,49 +0,0 @@
package com.ossez.lang.tutorial.tests;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import junit.framework.TestCase;
/**
* Object of VariableOssez
*
* @author YuCheng
*
*/
class OssezVariable {
int s1, s2;
static int s3;
OssezVariable(int x, int y, int z) {
s1 = x;
s2 = y;
s3 = z;
}
}
/**
*
* @author YuCheng
*
*/
public class VariableTest extends TestCase {
private final static Logger logger = LoggerFactory.getLogger(VariableTest.class);
/**
* Do RetsServerConnection Test
*/
@Test
public void testStaticVariableChange() {
OssezVariable objA = new OssezVariable(1, 2, 3);
logger.debug("s1/s2/s3 - [{}]/[{}]/[{}]", objA.s1, objA.s2, OssezVariable.s3);
OssezVariable objB = new OssezVariable(4, 5, 6);
logger.debug("s1/s2/s3 - [{}]/[{}]/[{}]", objA.s1, objA.s2, OssezVariable.s3);
logger.debug("s1/s2/s3 - [{}]/[{}]/[{}]", objB.s1, objB.s2, OssezVariable.s3);
}
}
@@ -1,131 +0,0 @@
package com.ossez.lang.tutorial.tests.lintcode;
import java.util.ArrayList;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ossez.lang.tutorial.models.TreeNode;
/**
* <p>
* 7
* <ul>
* <li>@see <a href=
* "https://www.cwiki.us/display/ITCLASSIFICATION/Serialize+and+Deserialize+Binary+Tree">https://www.cwiki.us/display/ITCLASSIFICATION/Serialize+and+Deserialize+Binary+Tree</a>
* <li>@see<a href=
* "https://www.lintcode.com/problem/serialize-and-deserialize-binary-tree">https://www.lintcode.com/problem/serialize-and-deserialize-binary-tree</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode0007SerializeAndDeserializeTest {
private final static Logger logger = LoggerFactory.getLogger(LintCode0007SerializeAndDeserializeTest.class);
/**
*
*/
@Test
public void testMain() {
logger.debug("BEGIN");
String data = "{3,9,20,#,#,15,7}";
System.out.println(serialize(deserialize(data)));
}
/**
* Deserialize from array to tree
*
* @param data
* @return
*/
private TreeNode deserialize(String data) {
// NULL CHECK
if (data.equals("{}")) {
return null;
}
ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();
data = data.replace("{", "");
data = data.replace("}", "");
String[] vals = data.split(",");
// INSERT ROOT
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
treeList.add(root);
int index = 0;
boolean isLeftChild = true;
for (int i = 1; i < vals.length; i++) {
if (!vals[i].equals("#")) {
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
if (isLeftChild) {
treeList.get(index).left = node;
} else {
treeList.get(index).right = node;
}
treeList.add(node);
}
// LEVEL
if (!isLeftChild) {
index++;
}
// MOVE TO RIGHT OR NEXT LEVEL
isLeftChild = !isLeftChild;
}
return root;
}
/**
*
* @param root
* @return
*/
public String serialize(TreeNode root) {
// write your code here
if (root == null) {
return "{}";
}
ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
queue.add(root);
for (int i = 0; i < queue.size(); i++) {
TreeNode node = queue.get(i);
if (node == null) {
continue;
}
queue.add(node.left);
queue.add(node.right);
}
while (queue.get(queue.size() - 1) == null) {
queue.remove(queue.size() - 1);
}
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(queue.get(0).val);
for (int i = 1; i < queue.size(); i++) {
if (queue.get(i) == null) {
sb.append(",#");
} else {
sb.append(",");
sb.append(queue.get(i).val);
}
}
sb.append("}");
return sb.toString();
}
}
@@ -1,124 +0,0 @@
package com.ossez.lang.tutorial.tests.lintcode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ossez.lang.tutorial.models.TreeNode;
/**
* <p>
* 69
* <ul>
* <li>@see <a href=
* "https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Tree+Level+Order+Traversal">https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Tree+Level+Order+Traversal</a>
* <li>@see<a href=
* "https://www.lintcode.com/problem/binary-tree-level-order-traversal">https://www.lintcode.com/problem/binary-tree-level-order-traversal</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode0069LevelOrderTest {
private final static Logger logger = LoggerFactory.getLogger(LintCode0069LevelOrderTest.class);
/**
*
*/
@Test
public void testMain() {
logger.debug("BEGIN");
String data = "{3,9,20,#,#,15,7}";
TreeNode tn = deserialize(data);
System.out.println(levelOrder(tn));
}
/**
* Deserialize from array to tree
*
* @param data
* @return
*/
private TreeNode deserialize(String data) {
// NULL CHECK
if (data.equals("{}")) {
return null;
}
ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();
data = data.replace("{", "");
data = data.replace("}", "");
String[] vals = data.split(",");
// INSERT ROOT
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
treeList.add(root);
int index = 0;
boolean isLeftChild = true;
for (int i = 1; i < vals.length; i++) {
if (!vals[i].equals("#")) {
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
if (isLeftChild) {
treeList.get(index).left = node;
} else {
treeList.get(index).right = node;
}
treeList.add(node);
}
// LEVEL
if (!isLeftChild) {
index++;
}
// MOVE TO RIGHT OR NEXT LEVEL
isLeftChild = !isLeftChild;
}
return root;
}
private List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
List<List<Integer>> rs = new ArrayList<List<Integer>>();
// NULL CHECK
if (root == null) {
return rs;
}
queue.offer(root);
while (!queue.isEmpty()) {
int length = queue.size();
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < length; i++) {
TreeNode curTN = queue.poll();
list.add(curTN.val);
if (curTN.left != null) {
queue.offer(curTN.left);
}
if (curTN.right != null) {
queue.offer(curTN.right);
}
}
rs.add(list);
}
return rs;
}
}
@@ -1,63 +0,0 @@
package com.ossez.lang.tutorial.tests.lintcode;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ossez.lang.tutorial.models.ListNode;
/**
* <p>
* 102
* <ul>
* <li>@see <a href=
* "https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle">https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle</a>
* <li>@see<a href= "https://www.lintcode.com/problem/linked-list-cycle/">https://www.lintcode.com/problem/linked-list-cycle/</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode0102HasCycleTest {
private final static Logger logger = LoggerFactory.getLogger(LintCode0102HasCycleTest.class);
/**
*
*/
@Test
public void testMain() {
logger.debug("BEGIN");
// INIT LINKED LIST
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
// CREATE A LOOP
head.next.next.next.next = head.next.next.next;
boolean retResult = false;
// LIKED LIST MAY NULL:
if (!(head == null || head.next == null)) {
ListNode s = head;
ListNode f = head.next;
while (f.next != null && f.next.next != null) {
s = s.next;
f = f.next.next;
if (f == s) {
retResult = true;
break;
}
}
}
System.out.println(retResult);
}
}
@@ -1,89 +0,0 @@
package com.ossez.lang.tutorial.tests.lintcode;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ossez.lang.tutorial.models.ListNode;
/**
* <p>
* 102
* <ul>
* <li>@see <a href=
* "https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle">https://www.cwiki.us/display/ITCLASSIFICATION/Linked+List+Cycle</a>
* <li>@see<a href= "https://www.lintcode.com/problem/linked-list-cycle/">https://www.lintcode.com/problem/linked-list-cycle/</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode0165MergeTwoListsTest {
private final static Logger logger = LoggerFactory.getLogger(LintCode0165MergeTwoListsTest.class);
/**
*
*/
@Test
public void testMain() {
logger.debug("BEGIN");
// INIT LINKED LIST l1
ListNode l1 = new ListNode(1);
l1.next = new ListNode(3);
l1.next.next = new ListNode(8);
l1.next.next.next = new ListNode(11);
l1.next.next.next.next = new ListNode(15);
// INIT LINKED LIST l2
ListNode l2 = new ListNode(2);
// RETURN RESULT
ListNode retResult = new ListNode(0);
// NULL CHECK
// if (l1 == null && l2 == null) {
// retResult = null;
// }
//
// if (l1 == null) {
// retResult = l2;
// }
//
// if (l2 == null) {
// retResult = l1;
// }
// MERGE
retResult = new ListNode(0);
ListNode tmpNode = new ListNode(0);
retResult = tmpNode;
while (l1 != null & l2 != null) {
if (l1.val <= l2.val) {
tmpNode.next = l1;
l1 = l1.next;
} else {
tmpNode.next = l2;
l2 = l2.next;
}
tmpNode = tmpNode.next;
}
if (l1 == null) {
tmpNode.next = l2;
}
if (l2 == null) {
tmpNode.next = l1;
}
retResult = retResult.next;
System.out.println(retResult.val);
System.out.println(retResult.next.val);
System.out.println(retResult.next.next.val);
}
}
@@ -1,78 +0,0 @@
package com.ossez.lang.tutorial.tests.lintcode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <p>
* 425
* <ul>
* <li>@see <a href=
* "https://www.cwiki.us/display/ITCLASSIFICATION/Letter+Combinations+of+a+Phone+Number">https://www.cwiki.us/display/ITCLASSIFICATION/Letter+Combinations+of+a+Phone+Number</a>
* <li>@see<a href=
* "https://www.lintcode.com/problem/letter-combinations-of-a-phone-number/description">https://www.lintcode.com/problem/letter-combinations-of-a-phone-number/description</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode0425LetterCombinationsTest {
private final static Logger logger = LoggerFactory.getLogger(LintCode0425LetterCombinationsTest.class);
/**
*
*/
@Test
public void testMain() {
logger.debug("LetterCombinationsTest");
String digits = "23";
HashMap<String, String> phoneKeyMap = new HashMap<String, String>();
phoneKeyMap.put("0", "");
phoneKeyMap.put("1", "");
phoneKeyMap.put("2", "abc");
phoneKeyMap.put("3", "def");
phoneKeyMap.put("4", "ghi");
phoneKeyMap.put("5", "jkl");
phoneKeyMap.put("6", "mno");
phoneKeyMap.put("7", "pqrs");
phoneKeyMap.put("8", "tuv");
phoneKeyMap.put("9", "wxyz");
List<String> retStatus = new ArrayList<>();
if (digits != null && digits.length() != 0) {
phoneRecursive(digits, retStatus, phoneKeyMap, "", 0);
}
System.out.println(retStatus);
}
/**
* phoneRecursive
*
* @param digits
* @param retStatus
* @param phoneKeyMap
* @param comb
* @param index
*/
private void phoneRecursive(String digits, List<String> retStatus, HashMap<String, String> phoneKeyMap, String comb, int index) {
if (index == digits.length()) {
retStatus.add(comb);
return;
}
char pos = digits.charAt(index);
for (char c : ((String) phoneKeyMap.get(String.valueOf(pos))).toCharArray()) {
phoneRecursive(digits, retStatus, phoneKeyMap, comb + c, index + 1);
}
}
}
@@ -1,94 +0,0 @@
package com.ossez.lang.tutorial.tests.lintcode;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <p>
* 433
* <ul>
* <li>@see <a href=
* "https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands">https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands</a>
* <li>@see<a href="https://www.lintcode.com/problem/number-of-islands/">https://www.lintcode.com/problem/number-of-islands/</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode0433NumIslandsTest {
private final static Logger logger = LoggerFactory.getLogger(LintCode0433NumIslandsTest.class);
/**
*
*/
@Test
public void testMain() {
logger.debug("BEGIN");
// INIT GRID
boolean[][] grid = { { true, true, false, false, false }, { false, true, false, false, true }, { false, false, false, true, true },
{ false, false, false, false, false }, { false, false, false, false, true }
};
// NULL CHECK
if (grid.length == 0 || grid[0].length == 0) {
System.out.println("NULL");
// return 0;
}
// GET SIZE
int n = grid.length;
int m = grid[0].length;
// ARRAY FOR VISITED LOG
boolean[][] visited = new boolean[n][m];
int count = 0;
// LOOP FOR GRID
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] && !visited[i][j]) {
numIslandsDFS(grid, visited, i, j);
count++;
}
}
}
System.out.println(count);
}
/**
*
* @param grid
* @param visited
* @param x
* @param y
*/
public void numIslandsDFS(boolean[][] grid, boolean[][] visited, int x, int y) {
if (x < 0 || x >= grid.length) {
return;
}
if (y < 0 || y >= grid[0].length) {
return;
}
if (grid[x][y] != true || visited[x][y]) {
return;
}
visited[x][y] = true;
// Recursive call
numIslandsDFS(grid, visited, x - 1, y);
numIslandsDFS(grid, visited, x + 1, y);
numIslandsDFS(grid, visited, x, y - 1);
numIslandsDFS(grid, visited, x, y + 1);
}
}
@@ -1,35 +0,0 @@
package com.ossez.lang.tutorial.tests.lintcode;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <p>
* 1480
* <ul>
* <li>@see
* <a href= "https://www.cwiki.us/display/ITCLASSIFICATION/Dot+Product">https://www.cwiki.us/display/ITCLASSIFICATION/Dot+Product</a>
* <li>@see<a href= "https://www.lintcode.com/problem/dot-product/">https://www.lintcode.com/problem/dot-product/</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode1480DotProductTest {
private final static Logger logger = LoggerFactory.getLogger(LintCode1480DotProductTest.class);
/**
*
*/
@Test
public void testMain() {
logger.debug("BEGIN");
int t = 87;
int[] dur = { 20, 25, 19, 37 };
// Write your code here
}
}