题目地址:
https://www.lintcode.com/problem/johns-backyard-garden/description
给定一个正整数 x x x,问是否存在两个正整数 a a a和 b b b使得 3 a + 7 b = x 3a+7b=x 3a+7b=x。
从 0 0 0到 x / 7 x/7 x/7来枚举 b b b即可。代码如下:
public class Solution {
/**
* @param x: the wall's height
* @return: YES or NO
*/
public String isBuild(int x) {
// write you code here
for (int i = 0; i * 7 <= x; i++) {
if ((x - i * 7) % 3 == 0) {
return "YES";
}
}
return "NO";
}
}
时间复杂度 O ( x ) O(x) O(x),空间 O ( 1 ) O(1) O(1)。