Broken Code
给一个sorted array 0 0 0 1 1 1 1,然后找出第一个1的位置。
边界情况:array为空或者全0。
思路:二分查找。为了优化,可以先判断最后一个数是不是0。
class Solution
{
public:
int FindBrokenCode(vector<int>& nums)
{
if (!nums.size() || nums.back() == )
return -;
int left = , right = nums.size() - ;
while (left <= right)
{
int mid = (left + right) >> ;
if (!nums[mid])
left = mid + ;
else if (nums[mid] == && (!mid || !nums[mid - ]))
return mid;
else right = mid;
}
}
};
Broken Code的更多相关文章
- Code Complete阅读笔记(二)
2015-03-06 328 Unusual Data Types ——You can carry this technique to extremes,putting all the ...
- What I Learned as a Junior Developer Writing Tests for Legacy Code(转载)
I go to the gym and lift weights because I like the feeling of getting stronger and better. Two mont ...
- Git版本控制与工作流
基本概念 Git是什么? Git是分布式版本控制系统,与SVN类似的集中化版本控制系统相比,集中化版本控制系统虽然能够令多个团队成员一起协作开发,但有时如果中央服务器宕机的话,谁也无法在宕机期间提交更 ...
- 《Continuous Integration》读书笔记
Trigger a Build whenever a change occurs. it can help us reduce assumptions on a projecvt by rebuild ...
- How To Ask Questions The Smart Way
How To Ask Questions The Smart Way Eric Steven Raymond Thyrsus Enterprises <esr@thyrsus.com> R ...
- Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
1, http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c 2, Introduction ...
- Git工作流指南:Gitflow工作流 Comparing Workflows
Comparing Workflows The array of possible workflows can make it hard to know where to begin when imp ...
- Python-aiohttp百万并发
http://www.aikaiyuan.com/10935.html 本文将测试python aiohttp的极限,同时测试其性能表现,以分钟发起请求数作为指标.大家都知道,当应用到网络操作时,异步 ...
- git workflows
https://www.atlassian.com/git/tutorials/comparing-workflows Comparing Workflows The array of possibl ...
随机推荐
- winform 路径
System.AppDomain.CurrentDomain.BaseDirectory d:\project\bin\release\
- JavaScript里面的基本函数
1.主要有三类基本函数 <script type="text/javascript"> // 普通函数 function func1(arg){ return true ...
- 九宫重排_康拓展开_bfs
历届试题 九宫重排 时间限制:1.0s 内存限制:256.0MB 问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可 ...
- CodeForces B. Creating the Contest
http://codeforces.com/contest/1029/problem/B You are given a problemset consisting of nn problems. T ...
- JZOJ 5280 膜法师
好啰嗦......还好作者给了一句话题意,不然光看题就很耗费时间. 样例输入: 1 6 3 1 78 69 55 102 233 666 样例输出: 1 2 3 4 5 6 11 数据范围: 思路: ...
- div样式
DIV样式汇总 一.常用属性: 1.Height:设置DIV的高度. 2.Width:设置DIV的宽度. 例: <div style="width:200px;height:200px ...
- 《c程序设计语言》读书笔记-5.5-指针实现strncpy,strncat,strncmp
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> ...
- poj 3678 Katu Puzzle 2-SAT 建图入门
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
- python类基础
#coding:gbk class Person(): def __init__(self,age,gender,height,weight): self.age = age self.gender ...
- Android横竖屏切换生命周期
转自xiaoQLuhttp://www.cnblogs.com/xiaoQLu/p/3324503.html 开源帮助android获得了飞速的发展,开源也导致了数不清的碎片问题.android的前期 ...