Skip to content

Commit

Permalink
每日一题
Browse files Browse the repository at this point in the history
  • Loading branch information
nibnait authored and nibnait committed May 7, 2020
1 parent 3697116 commit 9560901
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package algorithm_practice.LeetCode.code300;

import junit.framework.TestCase;
import org.junit.Test;

/*
有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?
如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水。
你允许:
装满任意一个水壶
清空任意一个水壶
从一个水壶向另外一个水壶倒水,直到装满或者倒空
示例 1: (From the famous "Die Hard" example)
输入: x = 3, y = 5, z = 4
输出: True
示例 2:
输入: x = 2, y = 6, z = 5
输出: False
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/water-and-jug-problem
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Created by nibnait on 2020-03-21
*/
public class M365_水壶问题 extends TestCase {

@Test
public void testCase() {
int x = 3, y = 5, z = 6;
System.out.println(canMeasureWater(x, y, z));

int x1 = 2, y1 = 6, z2 = 5;
System.out.println(canMeasureWater(x1, y1, z2));


}

public boolean canMeasureWater(int x, int y, int z) {

if (x == z || y == z || z == 0) {
return true;
}

if (z > x + y) {
return false;
}

int gcd;
if (x == 0 || y == 0) {
gcd = x + y;
} else {
gcd = getGcd(x, y);
}

return z % gcd == 0;
}

private int getGcd(int x, int y) {
if (x % y == 0) {
return y;
}
return getGcd(y, x % y);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package algorithm_practice.LeetCode.code800;

import common.datastruct.ListNode;
import common.util.ConstructListNode;
import common.util.SysOut;
import junit.framework.TestCase;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/*
给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
 
示例 1:
输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2:
输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
 
提示:
给定链表的结点数介于 1 和 100 之间。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/middle-of-the-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Created by nibnait on 2020-03-24
*/
public class E876_链表的中间结点 extends TestCase {

@Test
public void testCase() {

int[] nums = new int[]{1, 2, 3, 4, 5};
SysOut.printList(middleNode(ConstructListNode.construct(nums)));

int[] nums1 = new int[]{1, 2, 3, 4, 5, 6};
SysOut.printList(middleNode(ConstructListNode.construct(nums1)));

}

/**
* 本题考查的是双指针诶~~
* @param head
* @return
*/

public ListNode middleNode(ListNode head) {

if (head == null) {
return null;
}

List<ListNode> nodeList = new ArrayList<>();

while (head != null) {
nodeList.add(head);
head = head.next;
}

return nodeList.get(nodeList.size()/2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package algorithm_practice.LeetCode.code900;

import junit.framework.TestCase;
import org.junit.Test;

/*
给你一个整数数组 nums,请你将该数组升序排列。
 
示例 1:
输入:nums = [5,2,3,1]
输出:[1,2,3,5]
示例 2:
输入:nums = [5,1,1,2,0,0]
输出:[0,0,1,1,2,5]
 
提示:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sort-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Created by nibnait on 2020-03-31
*/
public class M912_排序数组 extends TestCase {

@Test
public void testCase() {

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package algorithm_practice.LeetCode.code900;

import junit.framework.TestCase;
import org.junit.Test;

import java.util.Arrays;

/*
给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1。
返回使 A 中的每个值都是唯一的最少操作次数。
示例 1:
输入:[1,2,2]
输出:1
解释:经过一次 move 操作,数组将变为 [1, 2, 3]。
示例 2:
输入:[3,2,1,2,1,7]
输出:6
解释:经过 6 次 move 操作,数组将变为 [3, 4, 1, 2, 5, 7]。
可以看出 5 次或 5 次以下的 move 操作是不能让数组的每个值唯一的。
提示:
0 <= A.length <= 40000
0 <= A[i] < 40000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-increment-to-make-array-unique
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Created by nibnait on 2020-03-22
*/
public class M945_使数组唯的最小增量 extends TestCase {

@Test
public void testCase() {

int[] A = new int[]{1, 2, 2};
System.out.println(minIncrementForUnique(A));

int[] A1 = new int[]{3, 2, 1, 2, 1, 7};
System.out.println(minIncrementForUnique(A1));


}

/**
* 1. 先排序
* 2. 如果(前一项A[i-1] == 后一项A[i])
* 则将A[i-1]加到A[i]+1
* A[i] = A[i-1] +1
*
* A[i-1] > A[i] 也是同理
*/
public int minIncrementForUnique(int[] A) {
Arrays.sort(A);

int move = 0;
for (int i = 1; i < A.length; i++) {
if (A[i - 1] >= A[i]) {
move += A[i - 1] - A[i] + 1;
A[i] = A[i - 1] + 1;
}
}

return move;
}
}
1 change: 1 addition & 0 deletions src/main/java/algorithm_practice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [M016_最接近的三数之和](./LeetCode/code000/M016_最接近的三数之和.java)
- [M015_三数之和](./LeetCode/code000/M015_三数之和.java)
- [E674_最长连续递增序列](./LeetCode/code600/E674_最长连续递增序列.java)
- [E876_链表的中间结点](./LeetCode/code800/E876_链表的中间结点.java)

## 滑动窗口
- [M567_字符串的排列](./LeetCode/code500/M567_字符串的排列.java)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/algorithm_practice/a_Sorting/f_Heap.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static int[] Heap_Sort(int[] a) {

while (N > 0) {
SwapUtil.swap(a, 0, N--); //将堆的最大元素a[0]和a[N]交换,
sink(a, 0, N); //调整堆, 知道堆空
sink(a, 0, N); //调整堆, 直到堆空
SysOut.printArray(a);
}

Expand Down
44 changes: 1 addition & 43 deletions src/main/test/Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Functions;
import com.google.common.collect.Lists;
import common.datastruct.TreeNode;
import junit.framework.TestCase;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.Arrays;

/**
* Created by nibnait on 2020-01-08
Expand All @@ -17,43 +10,8 @@ public class Main extends TestCase {

@Test
public void testCase() {
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(null);
queue.add(null);
queue.add(null);
queue.add(null);

System.out.println(queue.isEmpty());

}

public String compressString(String S) {
Stack<Character> stack = new Stack<Character>();

StringBuilder sb = new StringBuilder();
for (int i = 0; i < S.length(); i++) {
char c = S.charAt(i);
if (stack.isEmpty()) {
stack.push(c);
} else {
Character peek = stack.peek();
if (peek.equals(c)) {
stack.push(c);
} else {
sb.append(peek).append(stack.size());
stack.clear();

stack.push(c);
}
}
}

if (!stack.isEmpty()) {
sb.append(stack.peek()).append(stack.size());
}

String result = sb.toString();
return result.length() > S.length() ? S : result;
}

}
10 changes: 6 additions & 4 deletions src/main/test/localtest/advanceTest/反射.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class 反射 extends TestCase {

@Data
class Person {
private int age;
public int age;

public String phone;

String getName() {
return "xxx";
Expand Down Expand Up @@ -50,10 +52,10 @@ public void testCase() throws IllegalAccessException {
for (int i = 0; i < fields.length; i++) {
String name = fields[i].getName();
System.out.println(name);
int age = fields[i].getInt(person);
String age =String.valueOf(fields[i].get(person));
System.out.println(age);
fields[i].setInt(person, 2);
fields[i].setAccessible(true);
// fields[i].setInt(person, 2);
// fields[i].setAccessible(true);
}
}

Expand Down
Loading

0 comments on commit 9560901

Please sign in to comment.