【Problem Description】
有两个容量分别为 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
[Thinking] answer
1. BFS breadth-first traversal
- set to ensure that the situation does not repeat
- queue through all the cases
public boolean canMeasureWater_BFS(int x, int y, int z) {
if (z < 0 || z > x + y) {
return false;
}
Set<Integer> set = new HashSet<>();
Queue<Integer> q = new LinkedList<>();
//巧妙过度
q.offer(0);
//遍历所有情况 第一次 x,y入队列
while (!q.isEmpty()) {
int n = q.poll();
//巧妙限制条件!
if (n + x <= x + y && set.add(n + x)) {
q.offer(n + x);
}
if (n + y <= x + y && set.add(n + y)) {
q.offer(n + y);
}
if (n - x >= 0 && set.add(n - x)) {
q.offer(n - x);
}
if (n - y >= 0 && set.add(n - y)) {
q.offer(n - y);
}
if (set.contains(z)) {
return true;
}
}
return false;
}
2. Mathematical Methods greatest common divisor
public boolean canMeasureWater(int x, int y, int z) {
if (z == 0) return true;
if (x + y < z) return false;
int big = Math.max(x, y);
int small = x + y - big;
if (small == 0) return big == z;
//最大公约数
while (big % small != 0) {
int temp = small;
small = big % small;
big = temp;
}
return z % small == 0;
}
【to sum up】
1. want to clear all possible, to achieve dot columns, use containers, clever use of border
2. Queue operation
- Add to add one yuan cable if the queue is full, throw an exception IIIegaISlabEepeplian
- Remove to remove the head of the queue and returns the element if the queue is empty, an exception is thrown a NoSuchElementException
- element head of the queue element is returned if the queue is empty, an exception is thrown a NoSuchElementException
- offer and add an element returns true if the queue is full, false is returned
- Remove and return an element poll asked the head of the queue if the queue is empty or null
- Returns peek head of the queue If the queue element is empty, or null
- If you add an element to put the queue is full, then blocked
- take removes and returns the element head of the queue
3.Collections
Set and List have inherited Conllection, Map no
methods interface Collection :
- boolean add (Object o): An object is added to the reference in the set
- void clear (): Delete all the objects in the collection that no longer holds these objects references
- boolean isEmpty (): determines whether the set is empty
- boolean contains (Object o): determining whether a specific object in the collection holding references
- Iterartor iterator (): Returns an Iterator objects, can be used to traverse the elements in the collection
- boolean remove (Object o): Removes an object from a set of reference
- Returns the number of elements in the set: int size ()
- Object [] toArray (): returns an array, the array comprises all of the elements in the collection
- About: Iterator () and toArray () methods are used for all elements of the set, the former returns an Iterator objects, which returns a collection of all elements in the array contains.
Iterator interface declares the following methods : - hasNext (): determining whether a set of elements traversed completed, if not, returns true
- next (): returns the next element
- remove (): delete from the collection a next () method returns the element.
Set the add () method is how to determine whether the object has been stored in the collection?
boolean isExists=false;
Iterator iterator=set.iterator();
while(it.hasNext()) {
String oldStr=it.next();
if(newStr.equals(oldStr)){
isExists=true;
}