博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III
阅读量:5107 次
发布时间:2019-06-13

本文共 4807 字,大约阅读时间需要 16 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址: 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given an array A of 0s and 1s, we may change up to K values from 0 to 1.

Return the length of the longest (contiguous) subarray that contains only 1s.  

Example 1:

Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2Output: 6Explanation: [1,1,1,0,0,1,1,1,1,1,1]Bolded numbers were flipped from 0 to 1.  The longest subarray is underlined.

Example 2:

Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3Output: 10Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]Bolded numbers were flipped from 0 to 1.  The longest subarray is underlined. 

Note:

  1. 1 <= A.length <= 20000
  2. 0 <= K <= A.length
  3. A[i] is 0 or 1 

给定一个由若干 0 和 1 组成的数组 A,我们最多可以将 K 个值从 0 变成 1 。

返回仅包含 1 的最长(连续)子数组的长度。 

示例 1:

输入:A = [1,1,1,0,0,0,1,1,1,1,0], K = 2输出:6解释: [1,1,1,0,0,1,1,1,1,1,1]粗体数字从 0 翻转到 1,最长的子数组长度为 6。

示例 2:

输入:A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3输出:10解释:[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]粗体数字从 0 翻转到 1,最长的子数组长度为 10。 

提示:

  1. 1 <= A.length <= 20000
  2. 0 <= K <= A.length
  3. A[i] 为 0 或 1 

428ms
1 class Solution {     2     func longestOnes(_ A: [Int], _ K: Int) -> Int { 3         var left = 0, right = 0, res = 0, zeroCount = 0 4          5         while right < A.count { 6             if A[right] == 0 { zeroCount += 1 } 7             right += 1 8             while(zeroCount > K) { 9                 if A[left] == 0 { zeroCount -= 1 }10                 left += 111             }            12             res = max(right - left, res)13         }        14         return res15     }16 }

432ms

1 class Solution { 2     func longestOnes(_ A: [Int], _ K: Int) -> Int { 3         var i = 0, k = K 4         var result = 0 5         for j in 0..

460ms

1 class Queue
: CustomDebugStringConvertible, CustomStringConvertible { 2 private var storage: Array
3 4 private var head = 0 5 private var last = 0 6 7 init(_ capacity: Int) { 8 storage = Array() 9 storage.reserveCapacity(capacity)10 }11 12 func isEmpty() -> Bool {13 return head == last14 }15 16 func push(_ val: T) {17 storage.append(val)18 last += 119 }20 21 func pop() -> T {22 head += 123 24 return storage[head - 1]25 }26 27 func forEach(_ block: (T)->Void) {28 for i in head..
T {34 return storage[head]35 }36 37 func count() -> Int {38 return last - head39 }40 41 var description: String {42 return debugDescription43 }44 45 var debugDescription: String {46 var str = "["47 forEach { str += "\($0)," }48 str += "]"49 return str50 }51 }52 53 54 class Solution {55 func longestOnes(_ A: [Int], _ K: Int) -> Int {56 57 let queue = Queue
(A.count)58 var current = 059 var ans = 060 61 for i in A.indices {62 if A[i] > 0 {63 current += 164 } else if queue.count() < K {65 current += 166 queue.push(i)67 } else if K > 0{68 current = i - queue.pop()69 queue.push(i)70 }71 72 ans = max(ans, current)73 }74 75 return ans76 }77 }

Runtime: 476 ms

Memory Usage: 18.9 MB
1 class Solution { 2     func longestOnes(_ A: [Int], _ K: Int) -> Int { 3         var res:Int = 0 4         var zero:Int = 0 5         var left:Int = 0 6         for right in 0..
K)13 {14 if A[left] == 015 {16 zero -= 117 }18 left += 119 }20 res = max(res, right - left + 1)21 }22 return res 23 }24 }

Runtime: 576 ms

Memory Usage: 18.9 MB
1 class Solution { 2     func longestOnes(_ A: [Int], _ K: Int) -> Int { 3         var n:Int = A.count 4         var pre:[Int] = [Int](repeating:0,count:n) 5         for i in 0..
= 027 {28 val -= pre[i]29 }30 if val <= K31 {32 ans = mid33 lo = mid + 134 }35 else36 {37 hi = mid - 138 } 39 }40 fans = max(fans, ans - i)41 }42 return fans 43 }44 }

 

转载于:https://www.cnblogs.com/strengthen/p/10464806.html

你可能感兴趣的文章
java三角形代码_java判断是否是三角形的方法(附代码)
查看>>
java的c import_关于编译(javac),import,package的再理解
查看>>
js原生popup_js来实现popup的生成,带钟罩,可移动popup,点击body可自动消失
查看>>
idea写java webservice_WebService系列之Idea 创建Webservice server 和 WebService Client
查看>>
centos7用yum安装java_Centos7中yum方式安装java
查看>>
python 忽略异常_python如何忽略异常
查看>>
java对redis性能监控编码_Redis性能监控指标汇总
查看>>
性能测试工具 java ur_jmeter之自定义java请求性能测试
查看>>
java 成员域_java – Eclipse为域对象和带有’m’后缀的类成员生成getter/setter
查看>>
java jute_ZooKeeper源码阅读(六):JUTE
查看>>
java 创建书类_1、创建实体类(Customer.java、Orders.java)复制代码 代码如下:package wck.stu.vo.oneToMany_single...
查看>>
java短信验证怎么实现6,如何实现java手机短信验证功能
查看>>
php 5.2 xdebug,用PHP 5.5安装xdebug
查看>>
php多克,在PHP中使用Heredoc有什么好处?
查看>>
安卓setText导入什么Java包,java – 如何在recyclerview上添加SetOnClicklistener
查看>>
php 整数转日期,php数字转时间的方法
查看>>
mle matlab,MLE的Matlab程序
查看>>
php 视频资源互动共享系统,PHP教与学资源网站设计
查看>>
php buffer 切割,PHP 输出buffer控制 | 学步园
查看>>
php 名称搜索排序,php – 按名称弹性搜索排序
查看>>