题目描述:

解析:之前做的按位与简单版,直接暴力两重循环,result记录数组长度,如果没有值就为Integer.MAX_VALUE
代码:
package January;
public class sixteen { public static void main(String[] args) { int result = sixteen.Solution.minimumSubarrayLength(new int[]{1, 2, 3}, 2); System.out.println(result); }
class Solution { public static int minimumSubarrayLength(int[] nums, int k) { int result = Integer.MAX_VALUE; for (int i = 0; i < nums.length; i++) { int value = nums[i]; for (int j = i; j < nums.length; j++) { value = value | nums[j]; if (value >= k) { result = result > j - i + 1 ? j - i + 1 : result; break; } }
} return result == Integer.MAX_VALUE ? -1 : result; } }
}
|