初次提交

This commit is contained in:
huangge1199 2021-02-04 14:41:57 +08:00
parent 180530595c
commit 58116c5ec6
101 changed files with 3815 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.idea/
/**/*.iml

19
CodeWar/pom.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Code</artifactId>
<groupId>com.code</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>CodeWar</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,18 @@
package com.code.war.entry;
/**
* @Author: hyy
* @Date: 2020-04-24 15:53
*/
public class Token {
private String text;
private String type;
public Token(String text, String type) {
this.text = text;
this.type = type;
System.out.println("text=" + text + ",type=" + type);
}
}

View File

@ -0,0 +1,56 @@
package com.code.war.study.t20200213;
import java.util.ArrayList;
import java.util.List;
/**
* 4 kyu
* Sum of Intervals
*
* @Author: hyy
* @Date: 2020-02-13 22:07
*/
public class Interval {
public static int sumIntervals(int[][] intervals) {
if (intervals == null || intervals.length == 0) {
return 0;
}
List<List<Integer>> list = new ArrayList<>();
for (int i = 0; i < intervals.length; i++) {
int start = intervals[i][0];
int end = intervals[i][1];
if (list.size() > 0) {
for (int j = list.size() - 1; j >= 0; j--) {
List<Integer> temp = list.get(j);
if (start >= temp.get(0) && end <= temp.get(1)) {
list.remove(j);
start = temp.get(0);
end = temp.get(1);
break;
} else if (start >= temp.get(0) && start <= temp.get(1) && end >= temp.get(1)) {
start = temp.get(0);
list.remove(j);
j = list.size();
} else if (start <= temp.get(0) && end <= temp.get(1) && end >= temp.get(0)) {
end = temp.get(1);
list.remove(j);
j = list.size();
} else if (start <= temp.get(0) && end >= temp.get(1)) {
list.remove(j);
}
}
}
List<Integer> temp = new ArrayList<>();
temp.add(start);
temp.add(end);
list.add(temp);
}
int sum = 0;
for (List<Integer> temp : list) {
sum += temp.get(1) - temp.get(0);
}
return sum;
}
}

View File

@ -0,0 +1,68 @@
package com.code.war.study.t20200214;
/**
* @Author: hyy
* @Date: 2020-02-14 09:32
*/
public class Runes {
public static int solveExpression(final String expression) {
int missingDigit = -1;
String[] nums = new String[]{"", "", ""};
String op = "";
int j = 0;
nums = new String[]{expression.substring(0, 1), "", ""};
for (int i = 1; i < expression.length(); i++) {
if ("1234567890?".contains(expression.substring(i, i + 1))) {
nums[j] += expression.substring(i, i + 1);
} else if ("+*".contains(expression.substring(i, i + 1))) {
op = expression.substring(i, i + 1);
j++;
} else if ("-".equals(expression.substring(i, i + 1))) {
if ("+-*=".contains(expression.substring(i - 1, i))) {
nums[j] += expression.substring(i, i + 1);
} else {
op = "-";
j++;
}
} else {
j++;
}
}
for (int num = 0; num < 10; num++) {
String temp = expression;
temp = temp.replaceAll("\\?", String.valueOf(num));
if(num==0&&(nums[0].indexOf("?")==0||nums[1].indexOf("?")==0||nums[2].indexOf("?")==0)){
continue;
}
int num1 = Integer.parseInt(nums[0].replaceAll("\\?", String.valueOf(num)));
int num2 = Integer.parseInt(nums[1].replaceAll("\\?", String.valueOf(num)));
int num3 = Integer.parseInt(nums[2].replaceAll("\\?", String.valueOf(num)));
int result = 0;
switch (op) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
default:
break;
}
if (result == num3) {
missingDigit = num;
break;
}
}
//Write code to determine the missing digit or unknown rune
//Expression will always be in the form
//(number)[opperator](number)=(number)
//Unknown digit will not be the same as any other digits used in expression
return missingDigit;
}
}

View File

@ -0,0 +1,63 @@
package com.code.war.study.t20200215;
import java.util.Arrays;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-02-15 17:33
*/
public class CaesarCipher {
public static List<String> movingShift(String s, int shift) {
int length = s.length() % 5 == 0 ? s.length() / 5 : s.length() / 5 + 1;
int count = 0;
List<String> result = Arrays.asList("", "", "", "", "");
for (int i = 0; i < s.length(); i++) {
int index = index = (int) s.charAt(i) + shift + i;
char ch = ' ';
if ((int) s.charAt(i) > 64 && (int) s.charAt(i) < 91) {
while (index > 90) {
index -= 26;
}
ch = (char) (index);
} else if ((int) s.charAt(i) > 96 && (int) s.charAt(i) < 123) {
while (index > 122) {
index -= 26;
}
ch = (char) (index);
} else {
ch = s.charAt(i);
}
result.set(i / length, result.get(i / length) + ch);
}
return result;
}
public static String demovingShift(List<String> s, int shift) {
String result = "";
int num = 0;
for (String str : s) {
for (int i = 0; i < str.length(); i++) {
int index = index = (int) str.charAt(i) - shift - num;
char ch = ' ';
if ((int) str.charAt(i) > 64 && (int) str.charAt(i) < 91) {
while (index < 65) {
index += 26;
}
ch = (char) (index);
} else if ((int) str.charAt(i) > 96 && (int) str.charAt(i) < 123) {
while (index < 97) {
index += 26;
}
ch = (char) (index);
} else {
ch = str.charAt(i);
}
result += ch;
num++;
}
}
return result;
}
}

View File

@ -0,0 +1,48 @@
package com.code.war.study.t20200217;
/**
* @Author: hyy
* @Date: 2020-02-17 20:40
*/
public class Emirps {
public static long[] findEmirp(long n) {
// your code
long[] emirps = new long[(int) n];
long[] temp = new long[(int) n];
int eIndex = 0;
int rIndex = 0;
int sum = 0;
for (long i = 2; i < n; i++) {
boolean isEmirps = true;
for (int j = 0; j < eIndex; j++) {
if (i % emirps[j] == 0) {
isEmirps = false;
break;
}
}
if (isEmirps) {
emirps[eIndex] = i;
eIndex++;
String str = String.valueOf(i);
int count = 0;
int start = 0;
int end = str.length() - 1;
while (start < end) {
if (str.charAt(start) == str.charAt(end)) {
count++;
}
start++;
end++;
}
if (count < str.length() / 2) {
temp[rIndex] = i;
rIndex++;
sum += i;
}
}
}
return new long[]{rIndex, rIndex > 0 ? temp[rIndex - 1] : 0, sum};
}
}

View File

@ -0,0 +1,26 @@
package com.code.war.study.t20200217;
/**
* Scramblies
*
* @Author: hyy
* @Date: 2020-02-17 09:55
*/
public class Scramblies {
public static boolean scramble(String str1, String str2) {
// your code
boolean result = true;
for (int i=0;i<str2.length();i++){
int index = str1.indexOf(str2.substring(i,i+1));
if(index>=0){
str1 = str1.substring(0,index)+str1.substring(index+1);
}else{
result=false;
break;
}
}
return result;
}
}

View File

@ -0,0 +1,36 @@
package com.code.war.study.t20200624;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-06-24 15:10
*/
public class Hamming {
public static long hamming(int n) {
List<Long> result = new ArrayList<>();
int num2 = 0;
int num3 = 0;
int num5 = 0;
int count = 1;
result.add(1L);
while (n > count) {
long temp = Math.min(Math.min(result.get(num2) * 2, result.get(num3) * 3), result.get(num5) * 5);
if (temp == result.get(num2) * 2) {
num2++;
}
if (temp == result.get(num3) * 3) {
num3++;
}
if (temp == result.get(num5) * 5) {
num5++;
}
count++;
result.add(temp);
}
return result.get(result.size() - 1);
}
}

View File

@ -0,0 +1,43 @@
package com.code.war.study.t20200624;
import java.util.*;
/**
* @Author: hyy
* @Date: 2020-06-24 14:16
*/
public class ObservedPin {
public static List<String> getPINs(String observed) {
Map<String,List<String>> map = new HashMap<>(10);
map.put("1", Arrays.asList("1","2","4"));
map.put("2",Arrays.asList("1","5","2","3"));
map.put("3", Arrays.asList("5","6","3"));
map.put("4",Arrays.asList("1","5","7","4"));
map.put("5",Arrays.asList("2","4","6","8","5"));
map.put("6",Arrays.asList("3","5","9","6"));
map.put("7", Arrays.asList("4","8", "7"));
map.put("8",Arrays.asList("5", "7", "8", "9", "0"));
map.put("9",Arrays.asList("6","8","9"));
map.put("0", Arrays.asList("0","8"));
List<String> result = new ArrayList<>();
for (int i = 0; i < observed.length(); i++) {
List<String> pin = map.get(observed.substring(i,i+1));
if(result.size()==0){
for (int j = 0; j < pin.size(); j++) {
result.add(pin.get(j));
}
}else{
List<String> temp = new ArrayList<>();
for (int j = 0; j < pin.size(); j++) {
for (int k = 0; k < result.size(); k++) {
temp.add(result.get(k)+pin.get(j));
}
}
result = temp;
}
}
return result;
}
}

19
LeetCode/pom.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Code</artifactId>
<groupId>com.code</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>LeetCode</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,15 @@
package com.code.leet.entiy;
/**
* @Author: hyy
* @Date: 2020-02-12 14:36
*/
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
}
}

View File

@ -0,0 +1,22 @@
package com.code.leet.entiy;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-02-13 19:19
*/
public interface NestedInteger {
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public boolean isInteger();
// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
public Integer getInteger();
// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return null if this NestedInteger holds a single integer
public List<NestedInteger> getList();
}

View File

@ -0,0 +1,13 @@
package com.code.leet.entiy;
/**
* @Author: hyy
* @Date: 2020-02-13 18:25
*/
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}

View File

@ -0,0 +1,44 @@
package com.code.leet.study.t20200212;
import entiy.ListNode;
/**
* 2. 两数相加
*
* @Author: hyy
* @Date: 2020-02-12 14:35
*/
public class AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head, p = head = new ListNode(0);
int flag = 0;
int val;
while (l1 != null && l2 != null) {
val = (l1.val + l2.val + flag) % 10;
flag = (l1.val + l2.val + flag) / 10;
p.next = new ListNode(val);
p = p.next;
l1 = l1.next;
l2 = l2.next;
}
while (l1 != null) {
val = (l1.val + flag) % 10;
flag = (l1.val + flag) / 10;
p.next = new ListNode(val);
p = p.next;
l1 = l1.next;
}
while (l2 != null) {
val = (l2.val + flag) % 10;
flag = (l2.val + flag) / 10;
p.next = new ListNode(val);
p = p.next;
l2 = l2.next;
}
if (flag == 1) {
p.next = new ListNode(1);
}
return head.next;
}
}

View File

@ -0,0 +1,26 @@
package com.code.leet.study.t20200212;
import java.util.Arrays;
/**
* 4. 寻找两个有序数组的中位数
*
* @Author: hyy
* @Date: 2020-02-12 22:44
*/
public class FindMedianSortedArrays {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] result = Arrays.copyOf(nums1,nums1.length+nums2.length);
System.arraycopy(nums2,0,result,nums1.length,nums2.length);
Arrays.sort(result);
double num;
int length = result.length;
if(length%2==0){
num = (result[length/2-1]+result[length/2])/2.0;
}else{
num = result[length/2];
}
return num;
}
}

View File

@ -0,0 +1,35 @@
package com.code.leet.study.t20200212;
/**
* 3. 无重复字符的最长子串
*
* @Author: hyy
* @Date: 2020-02-12 17:41
*/
public class LengthOfLongestSubstring {
public static int lengthOfLongestSubstring(String s) {
if (s.length() <= 1) {
return s.length();
}
int length = 1;
int count = 1;
int start = 0;
for (int i = 1; i < s.length(); i++) {
int index = s.substring(start, i).indexOf(s.substring(i, i + 1));
if (index == -1) {
count++;
} else {
if (count > length) {
length = count;
}
count = i - index - start;
start = start + index + 1;
}
}
if (count > length) {
length = count;
}
return length;
}
}

View File

@ -0,0 +1,23 @@
package com.code.leet.study.t20200212;
/**
* 922. 按奇偶排序数组 II
*
* @Author: hyy
* @Date: 2020-02-12 11:07
*/
public class SortArrayByParityII {
public int[] sortArrayByParityII(int[] A) {
for (int i = 0; i < A.length - 1; i++) {
int j = i + 1;
while ((A[i] - i) % 2 == 1) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
j++;
}
}
return A;
}
}

View File

@ -0,0 +1,49 @@
package com.code.leet.study.t20200212;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 1. 两数之和
*
* @Author: hyy
* @Date: 2020-02-12 11:25
*/
public class TwoSum {
public static int[] twoSum(int[] nums, int target) {
int start = 0;
int end = nums.length - 1;
int[] result = new int[2];
if (nums.length == 2) {
result[0] = start;
result[1] = end;
} else {
List<Integer> list = new ArrayList();
for (int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
Arrays.sort(nums);
while (nums[start] + nums[end] != target) {
if (nums[start] + nums[end] > target) {
end--;
} else {
start++;
}
}
result[0] = list.indexOf(nums[start]);
list.remove(result[0]);
result[1] = list.indexOf(nums[end]);
if (result[1] >= result[0]) {
result[1]++;
}
if (result[0] > result[1]) {
int temp = result[0];
result[0] = result[1];
result[1] = temp;
}
}
return result;
}
}

View File

@ -0,0 +1,36 @@
package com.code.leet.study.t20200213;
/**
* 6. Z 字形变换
*
* @Author: hyy
* @Date: 2020-02-13 14:52
*/
public class Convert {
public String convert(String s, int numRows) {
if (numRows == 1 || s == null) {
return s;
}
String[] strs = new String[numRows];
for (int i = 0; i < strs.length; i++) {
strs[i] = "";
}
String str = "";
int forNum = 2 * numRows - 2;
for (int i = 0; i < s.length(); i++) {
int index = (i + 1) % forNum;
if (index > numRows) {
strs[forNum + 1 - index] += "" + s.charAt(i);
} else if (index == 0) {
strs[1] += "" + s.charAt(i);
} else {
strs[index - 1] += "" + s.charAt(i);
}
}
for (String temp : strs) {
str += temp;
}
return str;
}
}

View File

@ -0,0 +1,37 @@
package com.code.leet.study.t20200213;
import entiy.TreeNode;
/**
* @Author: hyy
* @Date: 2020-02-13 18:24
*/
public class FindSecondMinimumValue {
public int findSecondMinimumValue(TreeNode root) {
if(root.left==null){
return -1;
}
int min;
if (root.left.val < root.right.val) {
if(root.val==root.left.val){
min = root.right.val;
}else{
min = root.left.val;
}
} else if (root.left.val > root.right.val) {
if(root.val==root.right.val){
min = root.left.val;
}else{
min = root.right.val;
}
} else {
if (root.left.val == root.val) {
min = -1;
} else {
min = root.left.val;
}
}
return min;
}
}

View File

@ -0,0 +1,40 @@
package com.code.leet.study.t20200213;
/**
* @Author: hyy
* @Date: 2020-02-13 10:14
*/
public class LongestPalindrome {
public String longestPalindrome(String s) {
String result = "";
if (s.length() < 2) {
result = s;
} else {
String str = new StringBuilder(s).reverse().toString();
int start = 0;
int length = 1;
result = str.substring(start, 1);
String temp = str;
for (int i = 1; i < s.length(); i++) {
int index = temp.indexOf(s.substring(i, i + 1));
while (index >= 0) {
int j = 1;
while (i + j < s.length() && index + j < temp.length()) {
if (s.substring(i, i + j).equals(temp.substring(index, index + j))) {
j++;
if(i+index+j==s.length()&&j>length){
length=j;
result=s.substring(i,i+ j);
}
}else{
break;
}
}
index = temp.substring(index+1).indexOf(s.substring(i, i + 1));
}
}
}
return result;
}
}

View File

@ -0,0 +1,77 @@
package com.code.leet.study.t20200213;
/**
* @Author: hyy
* @Date: 2020-02-13 19:17
*/
import entiy.NestedInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
List<NestedInteger> nestedList;
List<Integer> list;
public NestedIterator(List<NestedInteger> nestedList) {
this.nestedList = nestedList;
NestedIterator i = new NestedIterator(nestedList);
list = new ArrayList<>();
while (i.hasNext()){
list.add(i.next());
}
}
@Override
public Integer next() {
if(nestedList.get(0).isInteger()){
int num = nestedList.get(0).getInteger();
nestedList.remove(0);
return num;
}else{
List<NestedInteger> firstList,temp = firstList = nestedList.get(0).getList();
while (!temp.get(0).isInteger()){
temp = temp.get(0).getList();
}
int num = temp.get(0).getInteger();
temp.remove(0);
nestedList.set(0,firstList.get(0));
return num;
}
}
@Override
public boolean hasNext() {
if(nestedList.size()>0){
return true;
}else{
return false;
}
}
}
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/

View File

@ -0,0 +1,9 @@
package com.code.leet.study.t20200306;
/**
* @Author: hyy
* @Date: 2020-03-06 20:27
*/
public class FindContinuousSequence {
}

View File

@ -0,0 +1,30 @@
package com.code.leet.study.t20200309;
/**
* @Author: hyy
* @Date: 2020-03-09 22:10
*/
public class NumArray {
private int[] nums;
public NumArray(int[] nums) {
this.nums = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
update(i, nums[i]);
}
}
public void update(int i, int val) {
this.nums[i] = val;
}
public int sumRange(int i, int j) {
int sum = 0;
for (; i <= j; i++) {
sum += this.nums[i];
}
return sum;
}
}

View File

@ -0,0 +1,26 @@
package com.code.leet.study.t20200309;
import com.code.leet.entiy.ListNode;
/**
* @Author: hyy
* @Date: 2020-03-09 20:37
*/
public class ReverseList {
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
ListNode p = head;
ListNode pPrev = null;
while (p != null) {
ListNode temp = p.next;
if (temp == null) {
newHead = p;
}
p.next = pPrev;
pPrev = p;
p = temp;
}
return newHead;
}
}

View File

@ -0,0 +1,25 @@
package com.code.leet.study.t20210204;
/**
*
*/
public class IsHappy {
public boolean isHappy(int n) {
boolean is = false;
int sum = 0;
do {
int num = n % 10;
sum += num * num;
n = n / 10;
} while (n >= 10);
sum += n * n;
if (sum >= 10) {
isHappy(sum);
} else {
is = sum == 1;
}
return is;
}
}

19
LintCode/pom.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Code</artifactId>
<groupId>com.code</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>LintCode</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,29 @@
package com.code.lint.entiy;
/**
* @Author: hyy
* @Date: 2020-01-25 23:45
*/
public class Interval {
int start, end;
Interval(int start, int end) {
this.start = start;
this.end = end;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
public void setStart(int start) {
this.start = start;
}
public void setEnd(int end) {
this.end = end;
}
}

View File

@ -0,0 +1,14 @@
package com.code.lint.entiy;
/**
* @Author: hyy
* @Date: 2020-01-26 01:37
*/
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
}
}

View File

@ -0,0 +1,12 @@
package com.code.lint.entiy;
/**
* @Author: hyy
* @Date: 2020-02-11 10:10
*/
public class SVNRepo {
public static boolean isBadVersion(int k) {
return false;
}
}

View File

@ -0,0 +1,15 @@
package com.code.lint.entiy;
/**
* @Author: hyy
* @Date: 2020-01-27 00:05
*/
public class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}

View File

@ -0,0 +1,40 @@
package com.code.lint.util;
import entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-27 16:08
*/
public class CreateTree {
public static TreeNode createTree(String str){
TreeNode root = null;
List<TreeNode> treeNodes = new ArrayList<>();
String[] val = str.split(",");
root = new TreeNode(Integer.parseInt(val[0]));
treeNodes.add(root);
int j=0;
for (int i=1;i<val.length;i++){
TreeNode temp;
if(val[i].equals("#")){
temp = null;
}else{
temp = new TreeNode(Integer.parseInt(val[i]));
treeNodes.add(temp);
}
if(i%2==0){
treeNodes.get(j).right = temp;
j++;
}else{
treeNodes.get(j).left = temp;
}
}
return root;
}
}

View File

@ -0,0 +1,9 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 19:28
*/
public class Aplusb {
}

View File

@ -0,0 +1,39 @@
package com.code.lint.y2020.m01.d25;
import entiy.Interval;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-25 23:42
*/
public class CanAttendMeetings {
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
public boolean canAttendMeetings(List<Interval> intervals) {
// Write your code here
for (int i = 0;i<intervals.size();i++){
for (int j=i+1;j<intervals.size();j++){
if(intervals.get(i).getStart()>=intervals.get(j).getStart()){
if(intervals.get(i).getStart()>=intervals.get(j).getEnd()){
Interval temp = intervals.get(i);
intervals.set(i, intervals.get(j));
}else{
return false;
}
}else{
if(intervals.get(i).getEnd()>intervals.get(j).getStart()){
return false;
}
}
}
}
return true;
}
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,23 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 21:58
*/
public class FirstUniqChar {
/**
* @param str: str: the given string
* @return: char: the first unique character in a given string
*/
public char firstUniqChar(String str) {
String temp;
for (int i = 0; i < str.length(); i++){
if(str.indexOf(str.charAt(i))==str.lastIndexOf(str.charAt(i))){
return str.charAt(i);
}
}
return '0';
}
}

View File

@ -0,0 +1,20 @@
package com.code.lint.y2020.m01.d25;
import java.util.regex.Pattern;
/**
* @Author: hyy
* @Date: 2020-01-25 21:59
*/
public class IsLegalIdentifier {
/**
* @param str: The identifier need to be judged.
* @return: Return if str is a legal identifier.
*/
public boolean isLegalIdentifier(String str) {
String pa = "^[a-zA-Z][a-z0-9A-Z_]+";
return Pattern.matches(pa,str);
}
}

View File

@ -0,0 +1,23 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 21:58
*/
public class IsUnique {
/**
* @param str: A string
* @return: a boolean
*/
public boolean isUnique(String str) {
// write your code here
for (int i = 1;i<str.length();i++){
if(str.indexOf(str.charAt(i))!=str.lastIndexOf(str.charAt(i))){
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,18 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 19:50
*/
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
// System.out.println(solution.reverseWords(" a b"));
// System.out.println(solution.firstUniqChar("aabc"));
// System.out.println(solution.firstUniqChar("abaccdeff"));
// System.out.println(solution.isLegalIdentifier("LintCode"));
// System.out.println(solution.mostFrequentlyAppearingLetters("ABCabcA"));
System.out.println(solution.count("WfaOIUZeTuQhIArgJuSgFufHBDoONlOVkKzXNwbDNXwD,EemZNuUovYHqKIaQBTZWUJinpNm,OX.DQPfHLNgedBUlGrHMgvoVw,sRicWxN.uNmULoHkMumuA mtemWcWPoUeZZdclZDYpbWY.OpAIBAVtJWfvTYzZtJowzcGizConWSUmZQHfnivsIedejNMtdiBTLfepfz,KTXTodw zNiIzFYSPuwPZLkhPkyvuxJinQHsPRfqDJGEECWhOiE.FCfexqGIpdlTTXgLvBxeUIuN.LPjQZCnH GJUlhCKDSZ"));
}
}

View File

@ -0,0 +1,33 @@
package com.code.lint.y2020.m01.d25;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: hyy
* @Date: 2020-01-25 21:59
*/
public class MostFrequentlyAppearingLetters {
/**
* @param str: the str
* @return: the sum that the letter appears the most
*/
public int mostFrequentlyAppearingLetters(String str) {
// Write your code here.
Map<Character, Integer> map = new HashMap<>();
int max = 1;
for (int i = 0 ;i<str.length();i++){
if(map.containsKey(str.charAt(i))){
map.put(str.charAt(i),map.get(str.charAt(i))+1);
if(map.get(str.charAt(i))>max){
max = map.get(str.charAt(i));
}
}else{
map.put(str.charAt(i),1);
}
}
return max;
}
}

View File

@ -0,0 +1,38 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 22:40
*/
public class MultiSort {
/**
* @param array: the input array
* @return: the sorted array
*/
public static int[][] multiSort(int[][] array) {
// Write your code here
for (int i = 0;i<array.length;i++){
for (int j=i+1;j<array.length;j++){
if(array[i][1]<array[j][1]){
int[] temp = array[i];
array[i] = array[j];
array[j] = temp;
}else if(array[i][1]==array[j][1]){
if(array[i][0]>array[j][0]){
int[] temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
return array;
}
public static void main(String[] args) {
int[][] array = new int[][]{{7,66},{4,77},{3,63},{5,81},{1,88},{9,86},{6,88},{2,82},{8,55},{10,95}};
array = multiSort(array);
System.out.println(array.toString());
}
}

View File

@ -0,0 +1,46 @@
package com.code.lint.y2020.m01.d25;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-25 19:12
*/
public class NarcissisticNumbers {
public static void main(String[] args) {
for (Integer i:getNarcissisticNumbers(3)
) {
System.out.println(i);
}
}
/**
* @param n: The number of digits
* @return: All narcissistic numbers with n digits
*/
public static List<Integer> getNarcissisticNumbers(int n) {
// write your code here
List<Integer> nums = new ArrayList<>();
int min = (int)Math.pow(10,n-1);
if(n==1){
min=0;
}
int max = (int)Math.pow(10,n);
for (int i = min; i < max; i++){
int temp = i;
int j = 0;
int sum = 0;
while(temp>10){
sum += Math.pow(temp % 10,n);
temp /= 10;
j++;
}
sum += Math.pow(temp % 10,n);
if(sum==i){
nums.add(i);
}
}
return nums;
}
}

View File

@ -0,0 +1,31 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 21:57
*/
public class ReverseWords {
/**
* @param s: A string
* @return: A string
*/
public String reverseWords(String s) {
// write your code here
if(s.equals("")) {
return s;
}
String[] strings = s.split(" ");
s = "";
for (String str:strings
) {
if(!str.equals("")){
s = str + " " + s;
}
}
if(!s.equals("")){
s = s.substring(0,s.length()-1);
}
return s;
}
}

View File

@ -0,0 +1,32 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 18:51
*/
public class RotateString {
public static void main(String[] args) {
String str="abcdefg";
int offset = 3;
rotateString(str.toCharArray(),offset);
}
/**
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
public static void rotateString(char[] str, int offset) {
if(str.length == 0 || offset == 0){
return;
}
offset = offset%str.length;//实际偏移量
for(int i = 0;i<offset; i++){
char temp = str[str.length-1];
for(int j = str.length-1;j>0;j--){
str[j] = str[j-1];
}
str[0] = temp;
}
}
}

View File

@ -0,0 +1,17 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 19:46
*/
public class RoundArea {
/**
* @param r: the given radius
* @return: the area of the given circle
*/
public double getArea(double r) {
// write your code here
return 3.14 * r * r;
}
}

View File

@ -0,0 +1,24 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 22:59
*/
public class SearchInsert {
/**
* @param A: an integer sorted array
* @param target: an integer to be inserted
* @return: An integer
*/
public int searchInsert(int[] A, int target) {
// write your code here
int i=0;
for(i=0;i<A.length;i++){
if(A[i]>=target) {
break;
}
}
return i;
}
}

View File

@ -0,0 +1,35 @@
package com.code.lint.y2020.m01.d25;
import java.util.regex.Pattern;
/**
* @Author: hyy
* @Date: 2020-01-25 19:50
*/
public class Solution {
/**
* @param s: the article
* @return: the number of letters that are illegal
*/
public int count(String s) {
// Write your code here.
int num = 0;
String[] sentence = s.split("\\.");
for (String str:sentence) {
if(!Pattern.matches("^[A-Z].*",str.trim())){
num++;
}
String[] words = str.split(" |,");
for(String word:words){
for (int i=1;i<word.length();i++){
if(Pattern.matches("[A-Z]",""+word.charAt(i))){
num++;
}
}
}
}
return num;
}
}

View File

@ -0,0 +1,18 @@
package com.code.lint.y2020.m01.d25;
/**
* @Author: hyy
* @Date: 2020-01-25 21:58
*/
public class StrStr {
/**
* @param source:
* @param target:
* @return: return the index
*/
public int strStr(String source, String target) {
return source.indexOf(target);
}
}

View File

@ -0,0 +1,38 @@
package com.code.lint.y2020.m01.d25;
/**
* 描述
*
* 给一个整数数组找到两个数使得他们的和等于一个给定的数 target
* 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标注意这里下标的范围是 0 n-1
*/
public class TwoSum{
public static void main(String[] args){
int[] numbers = {2, 7, 11, 15};
int[] result = twoSum(numbers,9);
System.out.print("["+result[0]+","+result[1]+"]");
}
/**
* @param numbers: An array of Integer
* @param target: target = numbers[index1] + numbers[index2]
* @return: [index1, index2] (index1 < index2)
*/
public static int[] twoSum(int[] numbers, int target) {
/**
* @param numbers: An array of Integer
* @param target: target = numbers[index1] + numbers[index2]
* @return: [index1, index2] (index1 < index2)
*/
int[] index = new int[2];
for(int i = 0; i < numbers.length; i++){
for(int j=i+1;j<numbers.length;j++){
if(numbers[i]+numbers[j]==target){
index[0]=i;
index[1]=j;
return index;
}
}
}
return index;
}
}

View File

@ -0,0 +1,44 @@
package com.code.lint.y2020.m01.d26;
import entiy.ListNode;
/**
*
* 466. 链表节点计数
*
* 计算链表中有多少个节点.
* 样例
*
* 样例 1:
* 输入: 1->3->5->null
* 输出: 3
*
* 样例解释:
* 返回链表中结点个数也就是链表的长度.
*
* 样例 2:
* 输入: null
* 输出: 0
*
* 样例解释:
* 空链表长度为0
*
* @Author: hyy
* @Date: 2020-01-26 01:52
*/
public class CountNodes {
/**
* @param head: the first node of linked list.
* @return: An integer
*/
public int countNodes(ListNode head) {
// write your code here
int num=0;
while (head!=null){
num++;
head = head.next;
}
return num;
}
}

View File

@ -0,0 +1,25 @@
package com.code.lint.y2020.m01.d26;
import entiy.ListNode;
/**
* @Author: hyy
* @Date: 2020-01-26 02:53
*/
public class CountNodesII {
/**
* @param head:
* @return: nothing
*/
public int countNodesII(ListNode head) {
int num = 0;
while (head!=null){
if(head.val>0&&head.val%2>0){
num++;
}
head = head.next;
}
return num;
}
}

View File

@ -0,0 +1,36 @@
package com.code.lint.y2020.m01.d26;
/**
* @Author: hyy
* @Date: 2020-01-26 00:04
*/
public class Deduplication {
/**
* @param nums: an array of integers
* @return: the number of unique integers
*/
public static int deduplication(int[] nums) {
// write your code here
int num = nums.length;
for (int i=0;i<num;i++){
for (int j=i+1;j<num;j++){
while(nums[i]==nums[j]&&j<num){
int temp = nums[j];
nums[j] = nums[num-1];
nums[num-1] = temp;
num--;
}
}
}
return num;
}
public static void main(String[] args) {
int[] nums = new int[]{1,3,1,4,4,2};
int n = deduplication(nums);
for(int i=0;i<n;i++){
System.out.println(nums[i]);
}
}
}

View File

@ -0,0 +1,48 @@
package com.code.lint.y2020.m01.d26;
import entiy.ListNode;
/**
* @Author: hyy
* @Date: 2020-01-26 02:33
*/
public class DeleteNode {
/**
* @param head: The first node of linked list
* @param n: the start index
* @param m: the end node
* @return: A ListNode
*/
public ListNode deleteNode(ListNode head, int n, int m) {
// Write your code here
int num=0;
ListNode temp = head;
while (head!=null){
num++;
if(num>n){
while (num<=m+1){
if(head.next!=null){
head = head.next;
temp = head;
}else{
temp = null;
}
num++;
}
}
if(num==n){
while (num<=m){
num++;
if(head.next!=null){
head.next = head.next.next;
} else {
break;
}
}
}
head=head.next;
}
return temp;
}
}

View File

@ -0,0 +1,35 @@
package com.code.lint.y2020.m01.d26;
/**
* @Author: hyy
* @Date: 2020-01-26 01:32
*/
public class Depress {
/**
* @param m: the limit
* @param k: the sum of choose
* @param arr: the array
* @return: yes or no
*/
public String depress(int m, int k, int[] arr) {
for (int i=0;i<arr.length;i++){
for (int j=0;j<arr.length;j++){
if(arr[i]<arr[j]){
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
int sum=0;
for (int i=0;i<k;i++){
sum+=arr[i];
}
if(sum<m){
return "yes";
}else{
return "no";
}
}
}

View File

@ -0,0 +1,59 @@
package com.code.lint.y2020.m01.d26;
import entiy.ListNode;
/**
*
* 219. 在排序链表中插入一个节点
* 中文
* English
*
* 在链表中插入一个节点
* 样例
*
* 样例 1
*
* 输入head = 1->4->6->8->null, val = 5
* 输出1->4->5->6->8->null
*
* 样例 2
*
* 输入head = 1->null, val = 2
* 输出1->2->null
*
* @Author: hyy
* @Date: 2020-01-26 01:58
*/
public class InsertNode {
/**
* @param head: The head of linked list.
* @param val: An integer.
* @return: The head of new linked list.
*/
public ListNode insertNode(ListNode head, int val) {
// write your code here
if(head==null){
ListNode newNode = new ListNode(val);
newNode.next = null;
return newNode;
}
if(val<=head.val){
ListNode newNode = new ListNode(val);
newNode.next = head;
return newNode;
}
ListNode temp = head;
while(val>head.val){
if(head.next!=null&&val>head.next.val){
head = head.next;
}else{
ListNode newNode = new ListNode(val);
newNode.next = head.next;
head.next = newNode;
break;
}
}
return temp;
}
}

View File

@ -0,0 +1,39 @@
package com.code.lint.y2020.m01.d26;
import entiy.ListNode;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-26 01:36
*/
public class MiddleNode {
/**
* @param head: the head of linked list.
* @return: a middle node of the linked list
*/
public ListNode middleNode(ListNode head) {
// write your code here
int num=0;
int index;
if(head==null){
return head;
}
List<ListNode> listNodes = new ArrayList<>();
ListNode next = head.next;
while (head!=null){
num++;
listNodes.add(head);
head = head.next;
}
if(num%2==0){
index = num/2-1;
}else {
index = num/2;
}
return listNodes.get(index);
}
}

View File

@ -0,0 +1,29 @@
package com.code.lint.y2020.m01.d26;
import entiy.ListNode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-26 02:25
*/
public class ReverseStore {
/**
* @param head: the given linked list
* @return: the array that store the values in reverse order
*/
public List<Integer> reverseStore(ListNode head) {
// write your code here
List<Integer> list = new ArrayList<>();
while (head!=null){
list.add(head.val);
head = head.next;
}
Collections.reverse(list);
return list;
}
}

View File

@ -0,0 +1,25 @@
package com.code.lint.y2020.m01.d26;
/**
* @Author: hyy
* @Date: 2020-01-26 00:39
*/
public class SortIntegers {
/**
* @param A: an integer array
* @return: nothing
*/
public void sortIntegers(int[] A) {
// write your code here
for (int i=0;i<A.length;i++){
for (int j=0;j<A.length;j++){
if(A[i]<A[j]){
int temp = A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
}
}

View File

@ -0,0 +1,51 @@
package com.code.lint.y2020.m01.d27;
import entiy.TreeNode;
import util.CreateTree;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-27 00:32
*/
public class BinaryTreePathSum {
/*
* @param root: the root of binary tree
* @param target: An integer
* @return: all valid paths
*/
static List<List<Integer>> result = new ArrayList<>();
static List<Integer> path = new ArrayList<>();
static int sum = 0;
public static List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
// write your code here
if(root==null){
return new ArrayList<>();
}
path.add(root.val);
sum += root.val;
if(root.left==null&&root.right==null){
if(sum==target){
result.add(new ArrayList(path));
}
}
if(root.left!=null){
binaryTreePathSum(root.left,target);
}
if(root.right!=null){
binaryTreePathSum(root.right,target);
}
sum -= root.val;
path.remove(path.size()-1);
return result;
}
public static void main(String[] args) {
TreeNode root = CreateTree.createTree("1,1,1,3,4,4,3,#,#,1,#,5,7");
binaryTreePathSum(root,6);
}
}

View File

@ -0,0 +1,79 @@
package com.code.lint.y2020.m01.d27;
import entiy.TreeNode;
import util.CreateTree;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-27 17:05
*/
public class BinaryTreePaths {
/**
* @param root: the root of the binary tree
* @return: all root-to-leaf paths
*/
// public static List<String> binaryTreePaths(TreeNode root) {
// // write your code here
// if(root==null){
// return new ArrayList<>();
// }
// List<String> strings = new ArrayList<>();
// List<TreeNode> treeNodes = new ArrayList<>();
// List<Integer> trees = new ArrayList<>();
// while(root!=null||treeNodes.size()>0){
// while(root!=null){
// trees.add(root.val);
// treeNodes.add(root);
// if(root.left==null&&root.right==null){
// if(trees.size()==0){
// continue;
// }
// String str = "";
// for(Integer num:trees){
// str += num + "->";
// }
// strings.add(str.substring(0,str.length()-2));
// }
// root = root.left;
// }
// if(treeNodes.size()>0){
// root = treeNodes.get(treeNodes.size()-1).right;
// treeNodes.remove(treeNodes.size()-1);
//// trees.remove(trees.size()-1);
// }
// }
// return strings;
// }
static List<String> path = new ArrayList<>();
static List<Integer> nums = new ArrayList<>();
public static List<String> binaryTreePaths(TreeNode root) {
if(root==null){
return new ArrayList<>();
}
nums.add(root.val);
if(root.left==null&&root.right==null){
String str = "";
for (Integer num:nums){
str += num + "->";
}
str = str.substring(0,str.length()-2);
path.add(str);
}
if(root.left!=null){
binaryTreePaths(root.left);
}
if(root.right!=null){
binaryTreePaths(root.right);
}
nums.remove(nums.size()-1);
return path;
}
public static void main(String[] args) {
TreeNode root = CreateTree.createTree("1,2,3,#,5");
binaryTreePaths(root);
}
}

View File

@ -0,0 +1,106 @@
package com.code.lint.y2020.m01.d27;
import java.util.ArrayList;
import java.util.List;
/**
* error
* @Author: hyy
* @Date: 2020-01-27 19:06
*
* 1751. 牛郎织女
* cat-only-iconCAT 专属题目
*
* 又到了七夕节牛郎织女相约一起去一个n*m大小的迷宫maze里玩耍然而没过多久他们就倒霉地走散了现在给定由.,*,S,T组成的矩阵maze
* 其中.表示空地,*表示障碍物,S表示牛郎的位置 ,T表示织女的位置牛郎和织女都会试图寻找对方不停地在矩阵中走动(他们可以每次向上下左右四
* 个方向移动一格或者站着不动但是不能走到迷宫外面或者障碍物)请问他们是否有可能重逢?如果有可能返回True否则返回False
*
* 样例
*
* 样例1:
*
* 输入:
* [
* "S..*",
* "*.**",
* "...T"
* ]
* 输出: true
* 说明:
* 织女选择不动
* 牛郎行动路线(0,0)->(0,1)->(1,1)->(2,1)->(2,2)->(2,3)
*
* 样例2:
*
* 输入:
* [
* "S..*",
* "***.",
* "...T"
* ]
* 输出: false
* 说明
* 这两个人无论如何和碰不上了
*
* 注意事项
*
* 2<=n,m<=1000
*/
public class FindHer {
/**
* @param maze: the maze
* @return: Can they reunion?
*/
public boolean findHer(String[] maze) {
// Write your code here
List<String> strs = new ArrayList();
int[][] num = new int[strs.size()][strs.get(0).length()];
int x1=0,y1=0,x2=0,y2=0;
for(int i = 0; i < maze.length; i++){
strs.add(maze[i]);
int index1 = maze[i].indexOf("S");
if(index1>0){
x1=i;
y1=index1;
}
int index2 = maze[i].indexOf("T");
if(index2>0){
x2=i;
y2=index1;
}
}
for (int i = 0; i < num.length;i++){
for(int j=0;j<num[0].length;j++){
num[i][j]=0;
}
}
return isFind(strs,x1,y1,x2,y2,num);
}
boolean isFind(List<String> strs,int x1,int y1,int x2,int y2,int[][] num){
if(Math.abs(x1-x2)+Math.abs(y1-y2)==1){
return true;
}
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;
if(x1-1>=0&&strs.get(x1-1).charAt(y1)=='.'&&num[x1-1][y1]==0){
num[x1-1][y1] = 1;
up = isFind(strs,x1-1,y1,x2,y2,num);
}
if(x1+1<strs.size()&&strs.get(x1+1).charAt(y1)=='.'&&num[x1+1][y1]==0){
num[x1+1][y1] = 1;
down = isFind(strs,x1+1,y1,x2,y2,num);
}
if(y1-1>=0&&strs.get(x1).charAt(y1-1)=='.'&&num[x1][y1-1]==0){
num[x1][y1-1] = 1;
left = isFind(strs,x1-1,y1,x2,y2,num);
}
if(y1+1<strs.get(0).length()&&strs.get(x1).charAt(y1+1)=='.'&&num[x1+1][y1]==0){
num[x1][y1+1] = 1;
right = isFind(strs,x1+1,y1,x2,y2,num);
}
return up||down||left||right;
}
}

View File

@ -0,0 +1,30 @@
package com.code.lint.y2020.m01.d27;
import entiy.TreeNode;
/**
* @Author: hyy
* @Date: 2020-01-27 19:22
*/
public class GetAns {
/**
* @param root: the root of the binary tree
* @return: the number of nodes
*/
int num = 0;
public int getAns(TreeNode root) {
// Write your code here
if(root==null){
return 0;
}
num++;
if(root.left!=null){
getAns(root.left);
}
if(root.right!=null){
getAns(root.right);
}
return num;
}
}

View File

@ -0,0 +1,69 @@
package com.code.lint.y2020.m01.d27;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-27 19:26
*/
public class GetWays {
/**
* @param a: the n numbers
* @param k: the number of integers you can choose
* @return: how many ways that the sum of the k integers is a prime number
*/
static List<List<Integer>> result = new ArrayList<>();
static LinkedList<Integer> sum = new LinkedList<>();
public static int getWays(int[] a, int k) {
// Write your code here
List<Integer> date = new ArrayList<>();
int count = 0;
for (int i=0;i<a.length;i++){
date.add(a[i]);
}
getData(date,sum,k);
for (List<Integer> nums:result){
boolean flag = true;
int sum=0;
for (int num:nums){
sum+=num;
}
for (int i=2;i<sum;i++){
if(sum%i==0){
flag = false;
break;
}
}
if(flag){
count++;
}
}
return count;
}
public static void getData(List<Integer> date,LinkedList<Integer> sum,int length){
if(length==0){
result.add((List<Integer>)sum.clone());
return;
}
for (int i=0;i<date.size();i++){
sum.addLast(date.get(i));
List<Integer> newDate = new ArrayList<>(date);
for (int j=i;j>=0;j--){
newDate.remove(j);
}
getData(newDate,sum,length-1);
sum.removeLast();
}
}
public static void main(String[] args) {
int[] a = new int[]{3,7,12,19};
int k = 3;
getWays(a,k);
}
}

View File

@ -0,0 +1,30 @@
package com.code.lint.y2020.m01.d27;
import entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-27 17:43
*/
public class InorderTraversal {
/**
* @param root: A Tree
* @return: Inorder in ArrayList which contains node values.
*/
List<Integer> trees = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
// write your code here
if(root==null){
return trees;
}
inorderTraversal(root.left);
trees.add(root.val);
inorderTraversal(root.right);
return trees;
}
}

View File

@ -0,0 +1,38 @@
package com.code.lint.y2020.m01.d27;
import entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-01-27 17:37
*/
public class InvertBinaryTree {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
public void invertBinaryTree(TreeNode root) {
// write your code here
List<TreeNode> treeNodes = new ArrayList<>();
if(root==null){
return;
}
while (root!=null||treeNodes.size()>0){
while(root!=null){
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
treeNodes.add(root);
root = root.left;
}
if(treeNodes.size()>0){
root = treeNodes.get(treeNodes.size()-1).right;
treeNodes.remove(treeNodes.size()-1);
}
}
}
}

View File

@ -0,0 +1,24 @@
package com.code.lint.y2020.m01.d27;
import entiy.TreeNode;
/**
* @Author: hyy
* @Date: 2020-01-27 00:04
*/
public class MaxDepth {
/**
* @param root: The root of binary tree.
* @return: An integer
*/
public int maxDepth(TreeNode root) {
// write your code here
if(root==null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return left >= right ? left + 1 : right + 1;
}
}

View File

@ -0,0 +1,65 @@
package com.code.lint.y2020.m01.d27;
import entiy.TreeNode;
import util.CreateTree;
import java.util.ArrayList;
import java.util.List;
/**
* error
* @Author: hyy
* @Date: 2020-01-27 15:48
*/
public class PreorderTraversal {
/**
* @param root: A Tree
* @return: Preorder in ArrayList which contains node values.
*/
// public static List<Integer> preorderTraversal(TreeNode root) {
// // write your code here
// List<TreeNode> treeNodes = new ArrayList<>();
// List<Integer> trees = new ArrayList<>();
// if(root==null){
// return new ArrayList<>();
// }
// while (root!=null||treeNodes.size()>0){
// while(root!=null){
// trees.add(root.val);
// treeNodes.add(root);
// root = root.left;
// }
// if(treeNodes.size()>0){
// root = treeNodes.get(treeNodes.size()-1).right;
// treeNodes.remove(treeNodes.size()-1);
// }
// }
// return trees;
// }
static List<Integer> tree = new ArrayList<>();
public static List<Integer> preorderTraversal(TreeNode root) {
// write your code here
if(root==null){
return new ArrayList<>();
}
tree.add(root.val);
if(root.left!=null){
preorderTraversal(root.left);
}
if(root.right!=null){
preorderTraversal(root.right);
}
return tree;
}
public static void main(String[] args) {
// TreeNode treeNode3 = new TreeNode(3);
// TreeNode treeNode2 = new TreeNode(2);
// TreeNode treeNode1 = new TreeNode(1);
// treeNode1.left = treeNode2;
// treeNode1.right = treeNode3;
TreeNode root = CreateTree.createTree("1,#,2,3");
preorderTraversal(root);
}
}

View File

@ -0,0 +1,134 @@
package com.code.lint.y2020.m01.d28;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 57. 三数之和
*
* 给出一个有n个整数的数组S在S中找到三个整数a, b, c找到所有使得a + b + c = 0的三元组
*
* @Author: hyy
* @Date: 2020-01-28 15:16
*/
public class ThreeSum {
/**
* @param numbers: Give an array numbers of n integer
* @return: Find all unique triplets in the array which gives the sum of zero.
*/
// static List<List<Integer>> result = new ArrayList<>();
// static LinkedList<Integer> sum = new LinkedList<>();
// public static List<List<Integer>> threeSum(int[] numbers) {
// // write your code here
// List<Integer> date = new ArrayList<>();
// for (int i=0;i<numbers.length;i++){
// for(int j=i+1;j<numbers.length;j++){
// if(numbers[i]>numbers[j]){
// int temp = numbers[i];
// numbers[i] = numbers[j];
// numbers[j] = temp;
// }
// }
// date.add(numbers[i]);
// }
// for (int i=2;i<date.size();i++){
// if(date.get(i).equals(date.get(i-2))){
// date.remove(i);
// }
// }
// getData(date,sum,3);
// for (int i=result.size()-1;i>=0;i--){
// List<Integer> nums = result.get(i);
// int sum = 0;
// for(int num:nums){
// sum += num;
// }
// if(sum!=0){
// result.remove(nums);
// }
// }
// for (int i=0;i<result.size();i++){
// for(int j=result.size()-1;j>i;j--){
// if(result.get(j).get(0).equals(result.get(i).get(0))&&result.get(j).get(1).equals(result.get(i).get(1))){
// result.remove(j);
// }
// }
// date.add(numbers[i]);
// }
// if(result.size()==1&&result.get(0).size()==0){
// result = null;
// }
// return result;
// }
// public static void getData(List<Integer> date, LinkedList<Integer> sum, int length){
// if(length==0){
// result.add((List<Integer>)sum.clone());
// return;
// }
// for (int i=0;i<date.size();i++){
// sum.addLast(date.get(i));
// List<Integer> newDate = new ArrayList<>(date);
// for (int j=i;j>=0;j--){
// newDate.remove(j);
// }
// getData(newDate,sum,length-1);
// sum.removeLast();
// }
// }
public static List<List<Integer>> threeSum(int[] numbers) {
List<List<Integer>> result = new ArrayList<>();
if (numbers == null || numbers.length < 3) {
return result;
}
Arrays.sort(numbers);
for (int i = 0; i < numbers.length - 2; i++) {
if (i > 0 && numbers[i] == numbers[i - 1]) {
continue;
}
int left = i + 1;
int right = numbers.length - 1;
int target = numbers[i];
sum(numbers, left, right, target, result);
}
return result;
}
public static void sum(int[] numbers, int left, int right, int target, List<List<Integer>> result) {
int oldLeft = 0;
int status = 1;
while (left < right) {
if (numbers[left] + numbers[right] + target == 0) {
List<Integer> list = new ArrayList<>();
if (status == 1) {
list.add(target);
list.add(numbers[left]);
list.add(numbers[right]);
result.add(list);
oldLeft = numbers[left];
status = 0;
} else if (oldLeft != numbers[left]) {
list.add(target);
list.add(numbers[left]);
list.add(numbers[right]);
result.add(list);
oldLeft = numbers[left];
}
left++;
right--;
status = 0;
} else if (numbers[left] + numbers[right] + target < 0) {
left++;
} else {
right--;
}
}
}
public static void main(String[] args) {
int[] nums = new int[]{0, 0, 0, 0, 92, 0, -3002, 0, 0, 0, -10, -19, 0, 65, 0, 0, 293, 0, 1, 1, 1, 9, 9, 9, 10, 11, 1001, 2001, -404, 201, 203, 201, 999, 345, 1, 1, 1, 1, 1, 1, 1, -2, 1, 1, 1, 1, 1, 1, 1, 1, -2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1200, 1, 1, 1, 1, 1, 2, 1202, 2, 2, -4, 2, 2, 2, 2, 4, 5, 6, 1, 1, -11, 1, 1, 1, 1, 1, 1, 1, 1, 101, 1, 1, 1, 1, 1, -1, 1, -6};
threeSum(nums);
}
}

View File

@ -0,0 +1,92 @@
package com.code.lint.y2020.m02.d10;
/**
* @Author: hyy
* @Date: 2020-02-10 23:42
*/
public class KthLargestElement {
/**
* @param n: An integer
* @param nums: An array
* @return: the Kth largest element
*/
// public static int kthLargestElement(int n, int[] nums) {
// // write your code here
// if(nums==null||nums.length==0){
// return -1;
// }
// return quickSort(nums,0,nums.length-1,n);
// }
//
// public int quickSort(int[] nums,int start,int end,int k){
// if(start==end){
// return nums[k];
// }
// int left = start;
// int right=end;
// int point=nums[(start+end)/2];
// while (left<=right){
// while (left<=right&&nums[left]<point){
// left++;
// }
// while (left<=right&&nums[right]>point){
// left--;
// }
// if(left<=right){
// int temp=nums[left];
// nums[left]=nums[right];
// nums[right]=temp;
// left++;
// right--;
// }
// }
// if(k<=right){
// return quickSort(nums,start,right,k);
// }
// if(start+k-1>=left){
// return quickSort(nums,left,end,k);
// }
// return nums[k];
// }
public int kthLargestElement(int n, int[] nums) {
int begin = 0, end = nums.length - 1;
int pos = 0;
while (begin <= end) {
pos = partion(nums, begin, end);
if (pos == n - 1) {
return nums[pos];
} else if (pos > n - 1) {
end = pos - 1;
} else {
begin = pos + 1;
}
}
return -1;
}
private static int partion(int[] data, int begin, int end) {
int temp = data[begin];
while (begin < end) {
while (begin < end && data[end] <= temp) {
end--;
}
swap(data, begin, end);
while (begin < end && data[begin] > temp) {
begin++;
}
swap(data, begin, end);
}
return begin;
}
public static void swap(int[] arr, int i, int j) {
if (arr == null || i >= arr.length || j >= arr.length || i < 0 || j < 0) {
return;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

View File

@ -0,0 +1,59 @@
package com.code.lint.y2020.m02.d10;
/**
* @Author: hyy
* @Date: 2020-02-10 21:05
*/
public class KthSmallestElement {
/**
* @param k: An integer
* @param nums: An array
* @return: the Kth largest element
*/
public static int kthSmallest(int k, int[] nums) {
int begin = 0, end = nums.length - 1;
int pos = 0;
while (begin <= end) {
pos = partion(nums, begin, end);
if (pos == k - 1) {
return nums[pos];
} else if (pos > k - 1) {
end = pos - 1;
} else {
begin = pos + 1;
}
}
return -1;
}
private static int partion(int[] data, int begin, int end) {
int temp = data[begin];
while (begin < end) {
while (begin < end && data[end] >= temp) {
end--;
}
swap(data, begin, end);
while (begin < end && data[begin] < temp) {
begin++;
}
swap(data, begin, end);
}
return begin;
}
public static void swap(int[] arr, int i, int j) {
if (arr == null || i >= arr.length || j >= arr.length || i < 0 || j < 0) {
return;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] nums = new int[]{9, 3, 2, 4, 8};
System.out.println(kthSmallest(1, nums));
}
}

View File

@ -0,0 +1,37 @@
package com.code.lint.y2020.m02.d10;
/**
* @Author: hyy
* @Date: 2020-02-10 20:35
*/
public class PartitionArray {
/**
* @param nums: The integer array you should partition
* @param k: An integer
* @return: The index after partition
*/
public static int partitionArray(int[] nums, int k) {
// write your code here
int max = nums.length;
if (max > 0) {
for (int i = 0; i < max; i++) {
while (nums[i] >= k) {
int temp = nums[i];
nums[i] = nums[max - 1];
nums[max - 1] = temp;
max--;
if(i==max){
break;
}
}
}
}
return max;
}
public static void main(String[] args) {
int[] nums = new int[]{3,2,1};
System.out.println(partitionArray(nums,2));
}
}

View File

@ -0,0 +1,27 @@
package com.code.lint.y2020.m02.d10;
/**
* 整数排序 II
* @Author: hyy
* @Date: 2020-02-10 20:11
*/
public class SortColors2 {
/**
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
public void sortColors2(int[] colors, int k) {
// write your code here
for (int i = 0; i < colors.length; i++) {
for (int j = i + 1; j < colors.length; j++) {
if (colors[i] > colors[j]) {
int temp = colors[i];
colors[i] = colors[j];
colors[j] = temp;
}
}
}
}
}

View File

@ -0,0 +1,46 @@
package com.code.lint.y2020.m02.d10;
/**
* @Author: hyy
* @Date: 2020-02-10 20:18
*/
public class SortIntegers2 {
/**
* @param A: an integer array
* @return: nothing
*/
public static void sortIntegers2(int[] A) {
// write your code here
quickSort(A,0,A.length-1);
}
public static void quickSort(int[] nums,int start,int end){
if(start<end){
int index = getIndex(nums,start,end);
quickSort(nums,start,index-1);
quickSort(nums,index+1,end);
}
}
public static int getIndex(int[] nums,int start,int end){
int temp = nums[start];
while (start<end){
while (start<end&&nums[end]>=temp){
end--;
}
nums[start]=nums[end];
while(start<end&&nums[start]<=temp){
start++;
}
nums[end]=nums[start];
}
nums[start]=temp;
return start;
}
public static void main(String[] args) {
int[] nums = new int[]{3,2,1,4,5};
sortIntegers2(nums);
}
}

View File

@ -0,0 +1,50 @@
package com.code.lint.y2020.m02.d11;
import java.util.Arrays;
/**
* @Author: hyy
* @Date: 2020-02-11 17:21
*/
public class DoingHomework {
/**
* @param cost: the cost
* @param val: the val
* @return: the all cost
*/
public static long doingHomework(int[] cost, int[] val) {
// Write your code here.
long sums = 0;
long sum = 0;
int index = 0;
Arrays.sort(val);
for (int i = 0; i < val.length; i++) {
val[i] -= sum;
if (val[i] < cost[index]) {
continue;
}
int j = 0;
for (j = index; j < cost.length; j++) {
if (val[i] >= cost[j]) {
sums += cost[j] * (val.length - i);
sum += cost[j];
val[i] -= cost[j];
} else {
index = j;
break;
}
}
if (j == cost.length) {
break;
}
}
return sums;
}
public static void main(String[] args) {
int[] cost = new int[]{3, 3, 3};
int[] val = new int[]{7, 15, 11, 13, 1, 6, 11, 15, 11, 1, 8, 9, 13, 9, 1};
System.out.println(doingHomework(cost, val));
}
}

View File

@ -0,0 +1,42 @@
package com.code.lint.y2020.m02.d11;
/**
* @Author: hyy
* @Date: 2020-02-11 09:40
*/
import entiy.SVNRepo;
/**
* public class SVNRepo {
* public static boolean isBadVersion(int k);
* }
* you can use SVNRepo.isBadVersion(k) to judge whether
* the kth code version is bad or not.
*/
public class FindFirstBadVersion {
/**
* @param n: An integer
* @return: An integer which is the first bad version.
*/
public int findFirstBadVersion(int n) {
// write your code here
int start = 1;
int end = n;
int index = (start + end) / 2;
while (start < index && index < end) {
if (SVNRepo.isBadVersion(index) == true) {
end = index;
} else {
start = index;
}
index = (start + end) / 2;
}
if (SVNRepo.isBadVersion(start) == true) {
index = start;
} else {
index = end;
}
return index;
}
}

View File

@ -0,0 +1,23 @@
package com.code.lint.y2020.m02.d11;
/**
* @Author: hyy
* @Date: 2020-02-11 11:16
*/
public class FindMin {
/**
* @param nums: a rotated sorted array
* @return: the minimum number in the array
*/
public int findMin(int[] nums) {
// write your code here
int min = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
return min;
}
}

View File

@ -0,0 +1,49 @@
package com.code.lint.y2020.m02.d11;
/**
* 75. 寻找峰值
*
* 你给出一个整数数组(size为n)其具有以下特点
*
* 相邻位置的数字是不同的
* A[0] < A[1] 并且 A[n - 2] > A[n - 1]
*
* 假定P是峰值的位置则满足A[P] > A[P-1]且A[P] > A[P+1]返回数组中任意一个峰值的位置
*
* @Author: hyy
* @Date: 2020-02-11 11:25
*/
public class FindPeak {
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
public static int findPeak(int[] A) {
// write your code here
int start = 1;
int end = A.length - 2;
int index = (start + end) / 2;
while (start < index && index < end) {
if (index > 0 && A[index - 1] < A[index] && A[index] > A[index + 1]) {
break;
} else if (A[index - 1] > A[index]) {
end = index - 1;
} else {
start = index + 1;
}
index = (start + end) / 2;
}
if (A[start] > A[start - 1] && A[start] > A[start + 1]) {
index = start;
} else if (A[end] > A[end +-1] && A[end] > A[end + 1]) {
index = end;
}
return index;
}
public static void main(String[] args) {
int[] nums = new int[]{100,102,104,7,9,11,4,3};
System.out.println(findPeak(nums));
}
}

View File

@ -0,0 +1,53 @@
package com.code.lint.y2020.m02.d11;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-02-11 14:58
*/
public class GetDistance {
/**
* @param n: The total number of stones.
* @param m: The total number of stones you can remove.
* @param target: The distance from the end to the starting point.
* @param nums: The array that the distance from the i rock to the starting point is nums[i].
* @return: Return the maximum value of the shortest jump distance.
*/
public static int getDistance(int n, int m, int target, List<Integer> d) {
// Write your code here.
List<Integer> nums = new ArrayList<>(d);
nums.add(target);
int start = 0;
int end = target;
while (start <= end) {
int index = (start + end) / 2;
int count = 0;
for (int i = 0, x = 0; i <= n; ) {
i++;
if (nums.get(i) - nums.get(x) < index) {
count++;
} else {
x = i;
}
}
if (count <= m) {
start = index + 1;
} else {
end = index - 1;
}
}
return start - 1;
}
public static void main(String[] args) {
int n = 100;
int m = 40;
int target = 53428902;
List<Integer> d = Arrays.asList(137872, 312786, 640276, 718243, 995859, 1188568, 1229359, 1549474, 1843642, 1931010, 2242465, 2430010, 2549796, 2902294, 3396266, 3521509, 3961579, 4297275, 4613587, 4614842, 5074008, 5094591, 5327198, 5860009, 5945922, 6341191, 6655012, 6816932, 7084191, 7109821, 7640178, 7936610, 8026301, 8054873, 8545942, 8989726, 9224735, 9244360, 9331817, 9406546, 9898145, 10239978, 10764311, 10962958, 10972250, 11128108, 11319843, 11515655, 11818594, 12283648, 12403800, 12631814, 12885894, 13202229, 13229659, 13362406, 13446983, 13639755, 13783223, 14210368, 14292516, 14787853, 14808906, 15269292, 15393700, 15607344, 15858474, 16279493, 16281697, 16551566, 16646986, 17129598, 17270469, 17599294, 18119162, 18371807, 18492487, 18611563, 18843930, 18927526, 19164215, 19465972, 19637549, 19973483, 20262894, 20600381, 20736572, 21174736, 21475457, 21824861, 21986321, 22213204, 22705607, 22708998, 23140278, 23212977, 23378634, 23677390, 23708887, 23728739);
System.out.println(getDistance(n, m, target, d));
}
}

View File

@ -0,0 +1,43 @@
package com.code.lint.y2020.m02.d11;
/**
* @Author: hyy
* @Date: 2020-02-11 02:33
*/
public class IntervalStatistics {
/**
* @param arr: the 01 array
* @param k: the limit
* @return: the sum of the interval
*/
public static long intervalStatistics(int[] arr, int k) {
// Write your code here.
long count = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]!=0){
continue;
}
int num = 0;
for (int j = i; j < arr.length; j++) {
if(arr[j]!=0){
if (arr[j] == 1) {
num++;
}
continue;
}
if (num <= k) {
count++;
} else {
break;
}
}
}
return count;
}
public static void main(String[] args) {
int[] nums = new int[]{0, 0, 1, 0, 1, 1, 0};
System.out.println(intervalStatistics(nums, 1));
}
}

View File

@ -0,0 +1,40 @@
package com.code.lint.y2020.m02.d11;
import java.util.Arrays;
/**
* @Author: hyy
* @Date: 2020-02-11 23:36
*/
/**
* 945 任务计划
*/
public class LeastInterval {
/**
* @param tasks: the given char array representing tasks CPU need to do
* @param n: the non-negative cooling interval
* @return: the least number of intervals the CPU will take to finish all the given tasks
*/
public static int leastInterval(char[] tasks, int n) {
// write your code here
int[] ch = new int[26];
for(char c : tasks){
ch[c-'A']++;
}
Arrays.sort(ch);
int i = 25;
while (i>=0 && ch[i]==ch[25]){
i--;
}
return Math.max(tasks.length, (ch[25] - 1) * (n + 1) + 25 - i);
}
public static void main(String[] args) {
String str = "BFJJCHICEGCEJFGJBIBBCBGAJHCGDEHEHAHIAJCGBGHGH";
char[] tasks = str.toCharArray();
// char[] tasks = new char[]{'A', 'A', 'A', 'B', 'B', 'B'};
int n = 15;
System.out.println(leastInterval(tasks, n));
}
}

View File

@ -0,0 +1,43 @@
package com.code.lint.y2020.m02.d11;
/**
* @Author: hyy
* @Date: 2020-02-11 22:06
*/
public class LengthOfLongestSubstring {
/**
* @param s: a string
* @return: an integer
*/
public static int lengthOfLongestSubstring(String s) {
// write your code here
if (s.length() < 2) {
return s.length();
}
int count = 1;
int num = 1;
int start = 0;
for (int i = 1; i < s.length(); i++) {
int index = s.substring(start, i).indexOf(s.substring(i, i + 1));
if (index == -1) {
num++;
} else {
if (num > count) {
count = num;
}
start += index + 1;
num = num - index;
}
}
if (num > count) {
count = num;
}
return count;
}
public static void main(String[] args) {
String str = "bpfbhmipx";
System.out.println(lengthOfLongestSubstring(str));
}
}

View File

@ -0,0 +1,73 @@
package com.code.lint.y2020.m02.d11;
import java.util.Stack;
/**
* 12. 带最小值操作的栈
*
* 实现一个栈, 支持以下操作:
*
* push(val) val 压入栈
* pop() 将栈顶元素弹出, 并返回这个弹出的元素
* min() 返回栈中元素的最小值
*
* 要求 O(1) 开销.
*
* @Author: hyy
* @Date: 2020-02-11 21:46
*/
public class MinStack {
public MinStack() {
// do intialization if necessary
}
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> minstack = new Stack<Integer>();
/**
* @param number: An integer
* @return: nothing
*/
public void push(int number) {
// write your code here
stack.push(number);
int top;
if (!minstack.empty()) {
top = minstack.peek();
} else {
top = number;
}
if (number < top) {
minstack.push(number);
} else {
minstack.push(top);
}
}
/**
* @return: An integer
*/
public int pop() {
// write your code here
int data = 0;
if(!stack.empty()){
data = stack.peek();
stack.pop();
minstack.pop();
}
return data;
}
/**
* @return: An integer
*/
public int min() {
// write your code here
int data = 0;
if(!minstack.empty()){
data = minstack.peek();
}
return data;
}
}

View File

@ -0,0 +1,88 @@
package com.code.lint.y2020.m02.d11;
/**
* 1671. 玩游戏
* <p>
* N 个人在玩游戏每局游戏有一个裁判和 N-1 个平民玩家给出一个数组 A, A[i] 代表玩家 i 至少需要成为平民 A[i]
* 返回最少进行游戏的次数
*
* @Author: hyy
* @Date: 2020-02-11 13:38
*/
public class PlayGames {
/**
* @param A:
* @return: nothing
*/
// public static long playGames(int[] A) {
// // Write your code here
// long count = 0;
// int num = 0;
// int max = 0;
// boolean bl = true;
// do {
// count = getCount(A, count);
// num = 0;
// max = 0;
// for (int i = 0; i < A.length; i++) {
// if (A[i] > count) {
// num++;
// if (A[i] - count > max) {
// max = (int) (A[i] - count);
// }
// } else {
// bl = false;
// }
// A[i] = (int) (A[i] - count < 0 ? 0 : A[i] - count);
// }
// } while (num > 0 && bl);
// return count + max;
// }
//
// public static long getCount(int[] A, long count) {
// int sum = 0;
// for (int i = 0; i < A.length; i++) {
// sum += A[i];
// }
// if (sum % (A.length - 1) == 0) {
// count += sum / (A.length - 1);
// } else {
// count += sum / (A.length - 1) + 1;
// }
// return count;
// }
public static long playGames(int[] A) {
long max = 0;
for (int num : A) {
if (num > max) {
max = num;
}
}
long start = 0;
long end = max * 2;
while (start < end) {
long index = (start + end) / 2;
long count = 0;
for (int num : A) {
if (index - num > 0) {
count += (index - num);
}
}
if (index > count) {
start = index + 1;
} else {
end = index;
}
}
if (start > max) {
max = start;
}
return max;
}
public static void main(String[] args) {
int[] nums = new int[]{39, 22, 89, 54, 91, 5, 30};
System.out.println(playGames(nums));
}
}

View File

@ -0,0 +1,54 @@
package com.code.lint.y2020.m02.d11;
/**
* 63. 搜索旋转排序数组 II
*
* 跟进搜索旋转排序数组假如有重复元素又将如何
*
* 是否会影响运行时间复杂度
*
* 如何影响
*
* 为何会影响
*
* 写出一个函数判断给定的目标值是否出现在数组中
*
* @Author: hyy
* @Date: 2020-02-11 11:21
*/
public class Search {
/**
* @param A: an integer rotated sorted array
* @param target: an integer to be searched
* @return: an integer
*/
public int search(int[] A, int target) {
// write your code here
int index = -1;
for (int i = 0; i < A.length; i++) {
if (A[i] == target) {
index = i;
break;
}
}
return index;
}
/**
* @param A: an integer ratated sorted array and duplicates are allowed
* @param target: An integer
* @return: a boolean
*/
public boolean search1(int[] A, int target) {
// write your code here
boolean isIn = false;
for (int i = 0; i < A.length; i++) {
if (A[i] == target) {
isIn = true;
break;
}
}
return isIn;
}
}

View File

@ -0,0 +1,37 @@
package com.code.lint.y2020.m02.d11;
/**
* @Author: hyy
* @Date: 2020-02-11 13:14
*/
public class SearchRange {
/**
* @param A: an integer sorted array
* @param target: an integer to be inserted
* @return: a list of length 2, [index1, index2]
*/
public int[] searchRange(int[] A, int target) {
// write your code here
int[] result = new int[]{-1, -1};
int start = 0;
int end = A.length;
while (start <= end) {
if (A[start] > target || A[end] < target) {
break;
} else {
if(A[start] == target&&A[end]==target){
result = new int[]{start,end};
break;
}
if (A[start] < target) {
start++;
}
if(A[end]>target){
end--;
}
}
}
return result;
}
}

View File

@ -0,0 +1,39 @@
package com.code.lint.y2020.m02.d11;
import java.util.Stack;
/**
* 229. 栈排序
*
* 请设计一种方法将一个栈进行升序排列 最大的数在最上面
*
* 你可以使用另外一个栈来辅助操作但不可将这些数复制到另外一个数据结构中 数组
*
* @Author: hyy
* @Date: 2020-02-11 22:46
*/
public class StackSorting {
/*
* @param stk: an integer stack
* @return: void
*/
public void stackSorting(Stack<Integer> stk) {
// write your code here
Stack<Integer> temp = new Stack<>();
while (!stk.isEmpty()) {
if(temp.isEmpty()){
temp.push(stk.pop());
}else{
int data = stk.pop();
while(!temp.isEmpty()&&temp.peek()<data){
stk.push(temp.pop());
}
temp.push(data);
}
}
while (!temp.isEmpty()){
stk.push(temp.pop());
}
}
}

View File

@ -0,0 +1,36 @@
package com.code.lint.y2020.m02.d11;
/**
* @Author: hyy
* @Date: 2020-02-11 22:33
*/
public class TwoSum {
/**
* @param nums: an array of Integer
* @param target: target = nums[index1] + nums[index2]
* @return: [index1 + 1, index2 + 1] (index1 < index2)
*/
public static int[] twoSum(int[] nums, int target) {
// write your code here
int[] result = new int[2];
int start = 0;
int end = nums.length - 1;
while (nums[start] + nums[end] != target) {
if (nums[start] + nums[end] > target) {
end--;
} else {
start++;
}
}
result[0] = start + 1;
result[1] = end + 1;
return result;
}
public static void main(String[] args) {
int[] nums = new int[]{2, 7, 11, 15};
System.out.println(twoSum(nums, 9));
}
}

View File

@ -0,0 +1,45 @@
package com.code.lint.y2020.m04.d026;
import java.util.HashMap;
import java.util.Map;
/**
* 1673. 存钱
* @Author: hyy
* @Date: 2020-04-26 14:38
*/
public class GetAns {
/**
* @param op: the type of information
* @param name: the name of user
* @param w: the money need to save or take
* @return: output the remaining money of the user.if the operation is illegal,output -1
*/
public int[] getAns(int[] op, String[] name, int[] w) {
// Write your code here
Map<String, Integer> map = new HashMap<>();
int[] result = new int[op.length];
for (int i = 0; i < op.length; i++) {
if (op[i] == 0) {
if (map.containsKey(name[i])) {
map.put(name[i], map.get(name[i]) + w[i]);
} else {
map.put(name[i], w[i]);
}
result[i] = map.get(name[i]);
} else {
if (map.containsKey(name[i])) {
if (map.get(name[i]) >= w[i]) {
map.put(name[i], map.get(name[i]) - w[i]);
result[i] = map.get(name[i]);
} else {
result[i] = -1;
}
} else {
result[i] = -1;
}
}
}
return result;
}
}

View File

@ -0,0 +1,37 @@
package com.code.lint.y2020.m04.d026;
/**
* 1479. 能否到达终点
*
* @Author: hyy
* @Date: 2020-04-26 14:54
*/
public class ReachEndpoint {
/**
* @param map: the map
* @return: can you reach the endpoint
*/
public static boolean reachEndpoint(int[][] map) {
// Write your code here
return reach(0, 0, map);
}
private static boolean reach(int x, int y, int[][] map) {
boolean result = false;
if (map[x][y] == 9) {
return true;
}
if (x + 1 < map[0].length && map[x + 1][y] > 0) {
result = result || reach(x + 1, y, map);
}
if (y + 1 < map.length && map[x][y + 1] > 0) {
result = result || reach(x, y + 1, map);
}
return result;
}
public static void main(String[] args) {
int[][] map = new int[][]{{1, 1, 1}, {1, 1, 1}, {1, 1, 9}};
System.out.println(reachEndpoint(map));
}
}

View File

@ -0,0 +1,60 @@
package com.code.lint.y2020.m04.d29;
import entiy.ListNode;
import entiy.TreeNode;
import util.CreateTree;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-04-29 11:09
*/
public class BinaryTreeToLists {
/**
* @param root the root of binary tree
* @return a lists of linked list
*/
public static List<ListNode> binaryTreeToLists(TreeNode root) {
// Write your code here
List<ListNode> lists = new ArrayList<>();
if(root==null){
return lists;
}
List<TreeNode> treeNodes = new ArrayList<>();
treeNodes.add(root);
while (treeNodes.size()!=0){
ListNode head = new ListNode(treeNodes.get(0).val);
ListNode p = head;
for (int i=1;i<treeNodes.size();i++){
TreeNode treeNode = treeNodes.get(i);
p.next = new ListNode(treeNode.val);
p = p.next;
}
p.next = null;
lists.add(head);
treeNodes = level(treeNodes);
}
return lists;
}
public static List<TreeNode> level(List<TreeNode> treeNodes){
List<TreeNode> treeNodeList = new ArrayList<>();
for (TreeNode treeNode:treeNodes) {
if(treeNode.left!=null){
treeNodeList.add(treeNode.left);
}
if(treeNode.right!=null){
treeNodeList.add(treeNode.right);
}
}
return treeNodeList;
}
public static void main(String[] args) {
TreeNode treeNode = CreateTree.createTree("1,2,3,4");
binaryTreeToLists(treeNode);
}
}

View File

@ -0,0 +1,49 @@
package com.code.lint.y2020.m04.d29;
import entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: hyy
* @Date: 2020-04-29 09:39
*/
public class LevelOrder {
/**
* @param root: A Tree
* @return: Level order a list of lists of integer
*/
public List<List<Integer>> levelOrder(TreeNode root) {
// write your code here
List<List<Integer>> lists = new ArrayList<>();
if(root==null){
return lists;
}
List<TreeNode> treeNodes = new ArrayList<>();
treeNodes.add(root);
while (treeNodes.size()!=0){
List<Integer> list = new ArrayList<>();
for (TreeNode treeNode:treeNodes) {
list.add(treeNode.val);
}
lists.add(list);
treeNodes = level(treeNodes);
}
return lists;
}
public List<TreeNode> level(List<TreeNode> treeNodes){
List<TreeNode> treeNodeList = new ArrayList<>();
for (TreeNode treeNode:treeNodes) {
if(treeNode.left!=null){
treeNodeList.add(treeNode.left);
}
if(treeNode.right!=null){
treeNodeList.add(treeNode.right);
}
}
return treeNodeList;
}
}

View File

@ -0,0 +1,30 @@
package com.code.lint.y2020.m04.d29;
/**
* @Author: hyy
* @Date: 2020-04-29 13:21
*/
public class ReachNumber {
/**
* @param target: the destination
* @return: the minimum number of steps
*/
public int reachNumber(int target) {
// Write your code here
int result = 1;
int sum = 0;
if(target>0){
while (sum < target || (sum - target) % 2 == 1) {
sum += result;
result++;
}
}else{
while (sum > target || (target - sum) % 2 == 1) {
sum -= result;
result++;
}
}
return result - 1;
}
}

View File

@ -0,0 +1,48 @@
package com.code.lint.y2020.m05.d29;
import entiy.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @Author: hyy
* @Date: 2020-05-29 10:41
*/
public class LevelOrder {
/**
* @param root: A Tree
* @return: Level order a list of lists of integer
*/
public List<List<Integer>> levelOrder(TreeNode root) {
// write your code here
List<List<Integer>> all = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if(root!=null){
queue.offer(root);
}
int num = 1;
while (!queue.isEmpty()) {
List<Integer> treeNum = new ArrayList<>();
int tempNum = 0;
for (int i = 0; i < num; i++) {
TreeNode temp = queue.poll();
treeNum.add(temp.val);
if (temp.left != null) {
queue.offer(temp.left);
tempNum++;
}
if (temp.right != null) {
queue.offer(temp.right);
tempNum++;
}
}
num = tempNum;
all.add(treeNum);
}
return all;
}
}

View File

@ -0,0 +1,20 @@
package com.code.lint.y2020.m06.d24;
/**
* @Author: hyy
* @Date: 2020-06-24 09:44
*/
public class TrailingZeros {
/**
* @param n: An integer
* @return: An integer, denote the number of trailing zeros in n!
*/
public long trailingZeros(long n) {
long count = 0;
for(int i = 1; Math.pow(5,i) <= n; i++) {
count += n / (long)Math.pow(5,i);
}
return count;
}
}

19
LuoGu/pom.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Code</artifactId>
<groupId>com.code</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>LuoGu</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,18 @@
package com.code.luogu.study.T20200304;
import java.util.Scanner;
/**
* @Author: hyy
* @Date: 2020-03-04 23:38
*/
public class P1001 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = in.nextInt();
int num2 = in.nextInt();
int sum = num1 + num2;
System.out.println(sum);
}
}

View File

@ -0,0 +1,28 @@
package com.code.luogu.study.T20200305;
import java.util.Scanner;
/**
* @Author: hyy
* @Date: 2020-03-05 01:31
*/
public class P1085 {
public static void main(String[] args) {
int greet = 0;
int max = 8;
int[][] hours = new int[7][2];
for (int i = 0; i < 7; i++) {
Scanner in = new Scanner(System.in);
hours[i][0] = in.nextInt();
hours[i][1] = in.nextInt();
}
for (int i = 0; i < 7; i++) {
if (hours[i][0] + hours[i][1] > max) {
max = hours[i][0] + hours[i][1];
greet = i + 1;
}
}
System.out.println(greet);
}
}

View File

@ -0,0 +1,18 @@
package com.code.luogu.study.T20200305;
import java.util.Scanner;
/**
* @Author: hyy
* @Date: 2020-03-05 00:07
*/
public class P1412 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = in.nextInt();
int num2 = in.nextInt();
int num = (num1 * 10 + num2) / 19;
System.out.println(num);
}
}

View File

@ -0,0 +1,25 @@
package com.code.luogu.study.T20200305;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* @Author: hyy
* @Date: 2020-03-05 01:21
*/
public class P1422 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int battery = in.nextInt();
float fee = 0;
if(battery<=150){
fee = 0.4463f*battery;
}else if(battery<=400){
fee = 0.4463f*150+(battery-150)*0.4663f;
}else{
fee=0.4463f*150+0.4663f*250+(battery-400)*0.5663f;
}
System.out.println(new DecimalFormat("0.0").format(fee));
}
}

View File

@ -0,0 +1,20 @@
package com.code.luogu.study.T20200305;
import java.util.Scanner;
/**
* @Author: hyy
* @Date: 2020-03-05 00:17
*/
public class P1425 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int startH = in.nextInt();
int startM = in.nextInt();
int endH = in.nextInt();
int endM = in.nextInt();
int sub = endH * 60 + endM - startH * 60 - startM;
System.out.println(sub / 60 + " " + sub % 60);
}
}

Some files were not shown because too many files have changed in this diff Show More