Broken Code

给一个sorted array 0 0 0 1 1 1 1,然后找出第一个1的位置。

边界情况:array为空或者全0。

思路:二分查找。为了优化,可以先判断最后一个数是不是0。

 1 class Solution
 2 {
 3 public:
 4     int FindBrokenCode(vector<int>& nums)
 5     {
 6         if (!nums.size() || nums.back() == 0)
 7             return -1;
 8         int left = 0, right = nums.size() - 1;
 9         while (left <= right)
10         {
11             int mid = (left + right) >> 1;
12             if (!nums[mid])
13                 left = mid + 1;
14             else if (nums[mid] == 1 && (!mid || !nums[mid - 1]))
15                 return mid;
16             else right = mid;
17         }
18     }
19 };
时间: 2024-10-11 17:28:15

Broken Code的相关文章

git workflows

https://www.atlassian.com/git/tutorials/comparing-workflows Comparing Workflows The array of possible workflows can make it hard to know where to begin when implementing Git in the workplace. This page provides a starting point by surveying the most

How To Ask Questions The Smart Way

How To Ask Questions The Smart Way Eric Steven Raymond Thyrsus Enterprises <[email protected]> Rick Moen <[email protected]> Copyright ? 2001,2006,2014 Eric S. Raymond, Rick Moen Revision History Revision 3.10 21 May 2014 esr New section on St

使用任务Task 简化异步编程

使用任务简化异步编程 Igor Ostrovsky 下载代码示例 异步编程是实现与程序其余部分并发运行的较大开销操作的一组技术. 常出现异步编程的一个领域是有图形化 UI 的程序环境:当开销较大的操作完成时,冻结 UI 通常是不可接受的. 此外,异步操作对于需要并发处理多个客户端请求的服务器应用程序来说非常重要. 在实践过程中出现的异步操作的典型例子包括向服务器发送请求并等待响应.从硬盘读取数据以及运行拼写检查等开销较大的计算. 以一个含 UI 的应用程序为例. 该应用程序可以使用 Window

Git版本控制与工作流

基本概念 Git是什么? Git是分布式版本控制系统,与SVN类似的集中化版本控制系统相比,集中化版本控制系统虽然能够令多个团队成员一起协作开发,但有时如果中央服务器宕机的话,谁也无法在宕机期间提交更新和协同开发.甚至有时,中央服务器磁盘故障,恰巧又没有做备份或备份没及时,那就可能有丢失数据的风险. 但Git是分布式的版本控制系统,客户端不只是提取最新版本的快照,而且将整个代码仓库镜像复制下来.如果任何协同工作用的服务器发生故障了,也可以用任何一个代码仓库来恢复.而且在协作服务器宕机期间,你也可

Commit-and- Run Is a Crime

? Commit-and- Run Is a Crime Niclas Nilsson iT'S lATE in THE AFTERnoon. The team is churning out the last pieces of the new feature set for the iteration, and you can almost feel the rhythm in the room. John is in a bit of a hurry though. He's late f

转-git feature branch workflow

https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow Getting Started Setting up a repository git init git clone git config Saving changes git add git commit Inspecting a repository git status git log Viewing old commits

Avoid Whack-a- Mole Development

Avoid Whack-a- Mole Development Venkat Subramaniam Broomfield, Colorado, U.S. SoFTWARE PRojECT MAnAgERS face a lot of pressure to deliver fast. Time is of the essence. How can you get things done fast? Imagine you have two programmers on your team, B

What most young programmers need to learn

In the past 7.5 years I have supervised over a dozen programming interns at Ronimo and have seen hundreds of portfolios of students and graduates. In almost all of those I saw the same things that they needed to learn. One might expect that I think t

《ruby编程语言》笔记 1

赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods in Ruby are allowed to return more than one value, and parallel assignmentis helpful in conjunction with such methods. For example:# Define a method to