知道逻辑是啥,不会写,哈哈哈
只能CV一下官方代码了,哈哈哈
-
-
- import java.util.Deque;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.Set;
-
- /**
- * @author xnl
- * @Description:
- * @date: 2022/6/28 22:52
- */
- public class Solution {
- public static void main(String[] args) {
- Solution solution = new Solution();
-
- }
-
- public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
- Deque<int[]> stack = new LinkedList<>();
- stack.push(new int[]{0 ,0});
- Set<Long> seen = new HashSet<>();
- while (!stack.isEmpty()){
- if (seen.contains(hash(stack.peek()))){
- stack.pop();
- continue;
- }
- seen.add(hash(stack.peek()));
-
- int[] state = stack.pop();
- // 代表第一个水壶和第二个水壶
- int remain_x = state[0], remain_y = state[1];
- // 第一个水壶达到了预期值,或者第二个水壶达到了预期值,或者第一个水壶加第二个水壶到达预期值
- if (remain_x == targetCapacity || remain_y == targetCapacity || remain_x + remain_y == targetCapacity){
- return true;
- }
-
- // 把第一个水壶灌满
- stack.push(new int[]{jug1Capacity, remain_y});
- // 把第二个水壶灌满
- stack.push(new int[]{remain_x, jug2Capacity});
- // 把第一个水壶倒空
- stack.push(new int[]{0, remain_y});
- // 把第二个个水壶倒空
- stack.push(new int[]{remain_x, 0});
- // 把第一个水壶灌进第二个水壶,直到满或者空
- stack.push(new int[]{remain_x - Math.min(remain_x, jug2Capacity - remain_y), remain_y + Math.min(remain_x, jug2Capacity - remain_y)});
- // 把第二个水壶灌进第一个水壶,直到满或者空
- stack.push(new int[]{remain_x + Math.min(remain_y, jug1Capacity - remain_x), remain_y - Math.min(remain_y, jug1Capacity - remain_x)});
- }
- return false;
- }
-
- private long hash(int[] state){
- return (long) state[0] * 1000001 + state[1];
- }
-
- /**
- * 贝祖定理
- * @param x
- * @param y
- * @param z
- * @return
- */
- private boolean canMeasureWater2(int x, int y, int z){
- if (x + y < z){
- return false;
- }
-
- if (x == 0 || y == 0 ){
- return z == 0 || x + y == z;
- }
- return z % gcd(x, y) == 0;
- }
-
- private int gcd(int x, int y){
- int remainder = x % y;
- while (remainder != 0){
- x = y;
- y = remainder;
- remainder = x % y;
- }
- return y;
- }
- }