Leetcode995 Minimum Number of K Consecutive Bit Flips

##题目描述
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.

Return the minimum number of K-bit flips required so that there is no 0 in the array. If it is not possible, return -1.

Example 1:

Input: A = [0,1,0], K = 1
Output: 2
Explanation: Flip A[0], then flip A[2].

Example 2:

Input: A = [1,1,0], K = 2
Output: -1
Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1].

Example 3:

Input: A = [0,0,0,1,0,1,1,0], K = 3
Output: 3
Explanation:
Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0]
Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0]
Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]

##思路
思路:先找到第一个0,找到第一个0以后,如果第一个0的位置i,i+k>当前数组的长度,则返回-1. 反之,在K循环里,对当前值进行按位异或(^=)且轮数加一。最后返回轮数。
##代码

class Solution {public int minKBitFlips(int[] A, int K) {int result=0;int i=0;while(i<A.length){if(A[i]==0){if(i+K>A.length)return -1;else{for(int j=0;j<K;j++){A[i+j]^=1;}result++;}}elsei++;}return result;}
}

Leetcode995 Minimum Number of K Consecutive Bit Flips解决方案相关推荐

  1. LeetCode-995. K连续位的最小翻转次数(Minimum Number of K Consecutive Bit Flips)

    K连续位的最小翻转次数 在仅包含 0 和 1 的数组 A 中,一次 K 位翻转包括选择一个长度为 K 的(连续)子数组,同时将子数组中的每个 0 更改为 1,而每个 1 更改为 0. 返回所需的 K ...

  2. LeetCode——1888. 使二进制字符串字符交替的最少反转次数(Minimum Number of Flips to Make the Binary ...)[中等]——分析及代码(Java)

    LeetCode--1888. 使二进制字符串字符交替的最少反转次数[Minimum Number of Flips to Make the Binary String Alternating][中等 ...

  3. LeetCode 202次周赛 1553. Minimum Number of Days to Eat N Oranges

    1553. Minimum Number of Days to Eat N Oranges 题目描述 思路分析 弱智动态规划 可行的动态规划 直觉上正确的思路 代码实现 证明正确性 复杂度分析 总结 ...

  4. 452 Minimum Number of Arrows to Burst Balloons

    452 Minimum Number of Arrows to Burst Balloons 文章目录 452 Minimum Number of Arrows to Burst Balloons 1 ...

  5. codeforce 804B Minimum number of steps

    cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each ...

  6. 871. Minimum Number of Refueling Stops

    题目: A car travels from a starting position to a destination which is target miles east of the starti ...

  7. LeetCode刷题:871. Minimum Number of Refueling Stops

    LeetCode刷题:871. Minimum Number of Refueling Stops 原题链接:https://leetcode.com/problems/minimum-number- ...

  8. LeetCode 871. Minimum Number of Refueling Stops 最少加油次数

    LeetCode 871. Minimum Number of Refueling Stops 本题是LeetCode 871题,最少加油次数. 题目描述 A car travels from a s ...

  9. LeetCode871.Minimum Number of Refueling Stops

    871.Minimum Number of Refueling Stops(加油站的最小数量) Description Difficulty: hard Example 1: Example 2: E ...

  10. 算法设计与分析:Minimum Number of Refueling Stops(Week 11)

    学号:16340008 题目:871. Minimum Number of Refueling Stops Question: A car travels from a starting positi ...

最新文章

  1. Ubuntu中的minicom
  2. struts2登录后返回登录前的页面
  3. 图像变换——等距变换,相似变换,仿射变换,投影变换
  4. 共享程序集和强命名程序集(3):强命名程序集的一些作用
  5. PHP获取毫秒时间戳,利用microtime()函数
  6. vue教程2 【Vue组件化编程】 组件的创建和使用 非单文件组件和单文件组件的区别
  7. 邮政银行贷款迟还4个小时就造成信用逾期,如何解决?
  8. linux生成文件清单,Linux 获取文件名称生成列表 txt - create_filelist
  9. 邮箱如何秘密发送多个人邮件_如何发送秘密消息
  10. 11 所允许的仓储单位类型没有针对仓储类型xxx定义
  11. 查看http的并发请求数及其TCP连接状态
  12. Unity2018新功能抢鲜 | 粒子系统改进
  13. 通过Atomsk、Ovito和lammps进行高熵合金的多晶体建模及渲染
  14. 贪吃蛇贪吃蛇代码--c语言版 visual c++6.0打开
  15. 清除此计算机中wps云盘,教大家Win10系统删除WPS网盘的方法,爱纯净官网
  16. 过去的过去,未来的未来
  17. konga 连接postgresql 12+启动异常:error: column r.consrc does not exist
  18. Java Class
  19. 斯德哥尔摩的照片七:城市漫步(下)
  20. 数据交换平台开发之二功能需求

热门文章

  1. Python 根据身份证号判断所属省份,生日与性别
  2. MacbookAir2011用U盘重装10.13.6High Sierra系统
  3. ObjectARX_AutoCAD创建矩形功能实现
  4. 产品经理必备原型工具Axure RP 8自定义元件库
  5. 项目过程管理(八)延期和需求变更
  6. Cannot access a disposed context instance. A common cause of this error is disposing a context insta
  7. 解决nohup.out文件过大的方法
  8. 论文翻译:2021_Performance optimizations on deep noise suppression models
  9. 用python来开发webgame服务端(1)
  10. StructedStreaming Kafka 排错(AbstractCoordinator: Marking the coordinator *dead for grou