-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nibnait
authored and
nibnait
committed
May 7, 2020
1 parent
3697116
commit 9560901
Showing
9 changed files
with
306 additions
and
48 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/main/java/algorithm_practice/LeetCode/code300/M365_水壶问题.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/algorithm_practice/LeetCode/code800/E876_链表的中间结点.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/algorithm_practice/LeetCode/code900/M912_排序数组.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} | ||
|
||
|
||
|
||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/algorithm_practice/LeetCode/code900/M945_使数组唯一的最小增量.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.