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. 【Excle数据透视表】如何重复显示行字段的项目标签

    前提:该数据透视表以表格形式显示 解决办法: 通过报表布局设置"重复所有项目标签" 修改前样式 步骤 单击数据透视表中任意单元格→设计→报表布局→重复所有项目标签 修改后样式

  2. 工作总结 获取html 标签 自定义属性值 根据html 自定义属性 获取 到标签

    FFID    HFID function getElementByAttr(tag, attr, value) { var aElements = document.getElementsByTag ...

  3. requests 模块入门玩法和高级玩法

    1.安装 pip install requests 2. http://docs.python-requests.org/zh_CN/latest/user/quickstart.html http: ...

  4. Spring事务管理简介

    © 版权声明:本文为博主原创文章,转载请注明出处 1.什么是事务 - 事务是指逻辑上的一组操作,这组操作要么全部成功,要么全部失败 2.事务特性(ACID) - 1.原子性(Atomicity):指事 ...

  5. (一)spark算子 分为3大类

    value类型的算子 处理数据类型为value型的算子(也就是这个算子只处理数据类型为value的数据),可以根据rdd的输入分区与输出分区的关系分为以下几个类型 (1)输入分区与输出分区一对一型 m ...

  6. cache和内存

    CPU与内存 北桥:主桥,主要用来处理高速信号,负责与处理器的联系:CPU通过FSB前端总线来访问内存控制器. 南桥:IO桥,负责IO总线之间的通信,比如PCI总线.SATA.USB等,可以连接光驱. ...

  7. 查看cup是32位还是64位

    1#echo  $HOSTTYPE 2#uname -a 3#getconf LONG_BIT

  8. 改进Spring中的分页技术

    Spring中有一个PagedListHolder,能够实现分页. 但此类有几个缺点: 1. 使用此类的代码比較繁琐 2. 此类存放的数据源是全部的记录集,即对于记录数为1000条的数据,即使我们仅仅 ...

  9. Spring WebSocket Support官方文档+翻译

    实时更新技术能够应用在很多场景中,比如在浏览器中聊天.股票报价.状态更新.现场直播.这些需求对时间的延迟性都很敏感,但是我们可以发现他们存在这共有的共性. 标准的HTTP请求,是一次请求对应一次相应. ...

  10. 线程池 http请求

    package com.aibi.cmdc.test; import java.io.BufferedReader; import java.io.InputStream; import java.i ...