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. vue-router push

    //push 方法一 对象 // this.$router.push({path:'HelloWorld2'}); //push 方法二 命名的路由 // this.$router.push({nam ...

  2. 我是怎样理解web页面的

    事实上web页面包括三部分东东 1.页面展示的元素(HTML) 2.页面元素展示的样式(CSS) 3.控制页面元素的交互(JavaScript) 不管页面多么复杂,从这三方面去看,都会得到清晰的认识的 ...

  3. js获取时间查并实现倒计时读条

    <script type="text/javascript"> $().ready(function () {// 每增加一个切换,就要增加一行,tab1不变,其他的都 ...

  4. Android实用工具

    1 json类:hiJson 格式化json字符串 2 sqlite类:sqlitespy,SQLiteExpertSetup 3

  5. declare @t table

    DECLARE @t TABLE(date char(21))INSERT @t SELECT '1900-1-1 00:00:00.000'INSERT @t SELECT '1900-1-1 00 ...

  6. asp.net core mvc视频A:笔记3-1.视图基本用法

    常用介绍 注意:ViewBag是对View的封装,所以如果两者键值(Key)是一样的话,后者会覆盖前者. 新建项目,添加空控制器 小技巧-快速添加视图 控制器方法,使用ViewData和ViewBag ...

  7. 《HTML 5网页开发实例具体解释》样章、内容简单介绍、前言

    http://spu.jd.com/1167757597.html http://product.dangdang.com/23484942.html 样章 http://download.csdn. ...

  8. php优化(php.ini)

    PHP优化 ------------------------------------- 尽量选择php5.4及以上的版本,里面很多优化参数已经移除了相比以前版本   1.引擎解析优化和加速 1)eac ...

  9. hdu 3172 Virtual Friends(并查集,字典树)

    题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...

  10. java锁之wait,notify(wait会释放锁,notify仅仅只是通知,不释放锁)

    wait是指在一个已经进入了同步锁的线程内,让自己暂时让出同步锁,以便其他正在等待此锁的线程可以得到同步锁并运行,只有其他线程调用了notify方法(notify并不释放锁,只是告诉调用过wait方法 ...