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. python——super()

    一.问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: class A: def __init__(self): pr ...

  2. java8 lambda 与 stream

    参见:https://www.bilibili.com/video/av14372754/?p=2

  3. 【BIEE】11_BIEE图形报表在谷歌浏览器64.0.3282.140中访问图例乱码解决

    如上图,使用谷歌浏览器访问BIEE图形报表的时候,标题.图例等涉及到中文的地方全部乱码了!但是用IE打开是不会乱码的,这是由于:谷歌需要设置编码格式 但是55版本以后,谷歌公司为了加快浏览器的速度,提 ...

  4. 解决zabbix“ZBX_NOTSUPPORTED: Timeout while executing a shell script”报错

    如题所示,在zabbix_server使用zabbix_get获取自定义“UserParameter”对应的脚本的数据时,出现了如题所示的报错信息 [root@nmp01 scripts]# /usr ...

  5. Java中的split函数的用法

    Java中的 split  函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回: 例如: String str="1234@abc"; String[] a ...

  6. Hive优化策略

    hive优化目标 在有限的资源下,运行效率高. 常见问题 数据倾斜.Map数设置.Reduce数设置等 hive运行 查看运行计划 explain [extended] hql 例子 explain ...

  7. CSS遮罩层,全兼容

    <script type="text/javascript"> $(function(){ $('#divLocker').css({ "position&q ...

  8. Unmapped Spring configuration files found.

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架后,IDEA弹出如下提示: 2.解决方案: File --> Project Structure --> M ...

  9. hdu - 5033 - Building(单调栈)

    题意:N 幢楼排成一列(1<=N<=10^5),各楼有横坐标 xi(1<=xi<=10^7) 以及高度 hi(1<=hi<=10^7),在各楼之间的Q个位置(1&l ...

  10. linux kernel学习笔记-5内存管理_转

    void * kmalloc(size_t size, gfp_t gfp_mask); kmalloc()第一个参数是要分配的块的大小,第一个参数为分配标志,用于控制kmalloc()的行为. km ...