This commit is contained in:
YuCheng Hu 2018-12-15 01:52:40 -05:00
parent 7ba067e016
commit 7f6177e160
No known key found for this signature in database
GPG Key ID: 1E5CBEF8B550FB7D
1 changed files with 50 additions and 92 deletions

View File

@ -3,22 +3,16 @@ package com.ossez.lang.tutorial.tests;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.management.ListenerNotFoundException;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.ossez.lang.tutorial.models.ListNode;
/** /**
* *
* @author YuCheng * @author YuCheng
@ -28,62 +22,6 @@ public class LintcodeTest {
private final static Logger logger = LoggerFactory.getLogger(LintcodeTest.class); private final static Logger logger = LoggerFactory.getLogger(LintcodeTest.class);
/**
* 35 https://www.lintcode.com/problem/reverse-linked-list/description
*/
@Test
public void test0035Reverse() {
// INIT LINKED LIST
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
// CHECK BEFORE
System.out.println(head.val);
System.out.println(head.next.val);
System.out.println(head.next.next.val);
// REVERSE
ListNode prev = null;
while (head != null) {
ListNode temp = head.next;
head.next = prev;
prev = head;
head = temp;
}
// CHECK AFTER
System.out.println(prev.val);
System.out.println(prev.next.val);
System.out.println(prev.next.next.val);
}
/**
* 1480 https://www.lintcode.com/problem/dot-product/description
*/
@Test
public void test0044minSubArray() {
List<Integer> nums = new ArrayList<Integer>();
nums.add(1);
nums.add(1);
int min_ending_here = 0;
int retStatus = 0;
for (int i = 0; i < nums.size(); i++) {
if (min_ending_here > 0) {
min_ending_here = nums.get(i);
} else {
min_ending_here += nums.get(i);
}
retStatus = Math.min(retStatus, min_ending_here);
}
System.out.println(retStatus);
}
/** /**
* 53 https://www.lintcode.com/problem/reverse-words-in-a-string/description * 53 https://www.lintcode.com/problem/reverse-words-in-a-string/description
*/ */
@ -156,36 +94,6 @@ public class LintcodeTest {
System.out.println("" + retStatus); System.out.println("" + retStatus);
} }
/**
* 411
*
* <p>
* <ul>
* <li>@see
* <a href="https://www.cwiki.us/display/ITCLASSIFICATION/Gray+Code">https://www.cwiki.us/display/ITCLASSIFICATION/Gray+Code</a>
* <li>@see<a href="https://www.lintcode.com/problem/gray-code/description">https://www.lintcode.com/problem/gray-code/description</a>
* </ul>
* </p>
*
*/
@Test
public void test0411GrayCode() {
int n = 2;
List<Integer> retArray = new ArrayList<>();
if (n == 0) {
retArray.add(0);
}
for (int i = 0; i < (2 << (n - 1)); i++) {
int g = i ^ (i / 2);
retArray.add(g);
}
System.out.println(retArray);
}
/** /**
* 1480 https://www.lintcode.com/problem/dot-product/description * 1480 https://www.lintcode.com/problem/dot-product/description
*/ */
@ -290,4 +198,54 @@ public class LintcodeTest {
System.out.println(strSet.size()); System.out.println(strSet.size());
} }
/**
* 1480 https://www.lintcode.com/problem/dot-product/description
*/
@Test
public void test1480dotProduct() {
int[] A = { 1, 1, -1 };
int[] B = { 2147483647, 1, 3 };
int retStatus = 0;
// LENGTH CHECK
if (A.length == 0 || B.length == 0 || A.length != B.length)
retStatus = -1;
// ADDED
if (retStatus != -1) {
for (int i = 0; i < A.length; i++) {
retStatus = retStatus + A[i] * B[i];
}
}
System.out.println(retStatus);
}
/**
* 1480 https://www.lintcode.com/problem/dot-product/description
*/
@Test
public void test0044minSubArray() {
List<Integer> nums = new ArrayList<Integer>();
nums.add(1);
nums.add(1);
int min_ending_here = 0;
int retStatus = 0;
for (int i = 0; i < nums.size(); i++) {
if (min_ending_here > 0) {
min_ending_here = nums.get(i);
} else {
min_ending_here += nums.get(i);
}
retStatus = Math.min(retStatus, min_ending_here);
}
System.out.println(retStatus);
}
} }