LeetCode:贪婪算法

贪婪算法基础

717. 1-bit and 2-bit Characters

class Solution {
public boolean isOneBitCharacter(int[] bits) {
/**
* 思路:
* 利用一个指针来从左向右扫描数组,我们就是要看最后两位构不构成2B,如果扫描到倒数第二是1,说明,前面所有位都已经被解析,所以1必定和0,构成2B
* 但是如果将要扫描的已经略过倒数第二位指到了倒数第一位,那么肯定最后一位只能是1B
*/
int point = 0;
while(point<bits.length-1)
{
point = point +bits[point]+1; //注意,这里巧妙的将2B进行了移动
}
//到这里point指的位置,如果不为长度-1,那么必为长度+1
return point==bits.length-1;
}
}

  

LeetCode:贪婪算法的更多相关文章

  1. [LeetCode] Jump Game II(贪婪算法)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. [LeetCode] Assign Cookies 分点心

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  3. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  4. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  5. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. [LeetCode] Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. 数据库设计--数据流图(DFD)

    1.数据流图的定义 数据流图(DFD)是结构化分析方法中使用的工具,它以图形的方式描绘数据在系统中流动和处理的过程, 因为它仅仅反映系统必须完毕的逻辑功能.所以它是一种功能模型. 在结构化开发方法中. ...

  2. 程序猿的量化交易之路(24)--Cointrader之RemoteEvent远程事件实体(11)

    转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrader.top/ 在量化交易系统中.有些事件是远端传来的,比方股票的价格数据等.所以,在这 ...

  3. .net 取当前代码的行号及类名称

    取方法入口的行号及类名 //System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true); //int a ...

  4. Deferred Shader GBuffer 感性认识。。。

  5. 【SpringMVC学习11】SpringMVC中的拦截器

    Springmvc的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理.本文主要总结一下springmvc中拦截器是如何定义的,以及测试拦截器的执行情况和使用 ...

  6. ios侧滑返回:完美解决 interactivePopGestureRecognizer 卡住的问题

    interactivePopGestureRecognizer是iOS7推出的解决VeiwController滑动后退的新功能,虽然很实用,但是坑也很多啊(比如在rootViewcontroller下 ...

  7. MD5值转换(Hex 32位 <-> base64 24位)

    关于MD5值的原理本文不在介绍,本文主要介绍MD5值的两种编码的相互转换(32位和BASE64编码的24位),实际应用过程中经常会涉及到两种编码的相互转换.快熟使用工具tomeko.net. C#示例 ...

  8. 转 【MQTT】在Windows下搭建MQTT服务器

    MQTT简介 MQ 遥测传输 (MQTT) 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放.简单.轻量.易于实现.这些特点使它适用于受限环境.该协议的特点有: 使用发布/订阅消息模式,提供 ...

  9. 基于Ranking-CNN的年龄识别(CVPR_2017)

    作为学习记录,将所做PPT摘录如下:

  10. HDU 4417 划分树+二分

    题意:有n个数.m个询问(l,r,k),问在区间[l,r] 有多少个数小于等于k. 划分树--查找区间第k大的数.... 利用划分树的性质.二分查找在区间[l,r]小于等于k的个数. 假设在区间第 i ...