【Lintcode】749. John’s Backyard Garden

it2024-07-14  46

题目地址:

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)

最新回复(0)