Power of Two

 /**
  * LeetCode: Power of Two
  * Given an integer, write a function to determine if it is a power of two.
  *
  * @author LuoPeng
  * @time 2015.8.3
  *
  */
 public class PowerOfTwo {

     public boolean isPowerOfTwo(int n) {

         boolean result = false;
         if ( n == 1) {
             result = true;
         } else if ( n > 1) {
             while ( n % 2 == 0) {
                 n = n/2;
             }
             result = (n==1)?true:false;
         }
         return result;
     }

 }

Summary Ranges

 /**
  * LeetCode: Summary Ranges
  * Given a sorted integer array without duplicates, return the summary of its ranges.
  * For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].
  *
  * If the next value is equal to the current value+1, then the next value should be merged together.
  * Input:[0, 1, 5, 7, 8]; output:["0->1", "5", "7->8"
  * Input:[3, 5, 6, 7, 9]; output:["3", "5->7", "9"]
  *
  * @author LuoPeng
  * @time 2015.8.4
  *
  */
 public class SummaryRanges {

     /**
      *
      * @param nums
      * @return
      */
     public List<String> summaryRanges(int[] nums) {

         if ( nums == null) {return null;}

         List<String> result = new ArrayList<String>();
         if ( nums.length != 0) {
             String temp = null;
             int length = nums.length;
             int start = 0;
             for ( int i = 1; i < length; i++) {
                 if ( nums[i] - nums[i-1] != 1) {
                     if ( i == start+1) {
                         // the value should be itself
                         temp = "" + nums[start];
                     }  else {
                         temp = nums[start] + "->" + nums[i-1];
                     }
                     result.add(temp);
                     start = i;
                 }
             }

             // the last element
             if ( length == start+1) {
                 temp = "" + nums[start];
             }  else {
                 temp = nums[start] + "->" + nums[length-1];
             }
             result.add(temp);
         }

         return result;

     }

 }

LeetCode Day2的更多相关文章

  1. 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion

    [Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...

  2. leetcode每日刷题计划-简单篇day2

    今天数模比赛爆肝&操作系统大作业 脖子疼orz先把题过了保证flag不倒..个别细节回头看吧 Num 13 罗马数字转整数 Roman to Integer 一遍提交过,开始编译出了点问题 具 ...

  3. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  4. 【从零开始学BPM,Day2】默认表单开发

    [课程主题]主题:5天,一起从零开始学习BPM[课程形式]1.为期5天的短任务学习2.每天观看一个视频,视频学习时间自由安排. [第二天课程] Step 1 软件下载:H3 BPM10.0全开放免费下 ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. IOS 掉用系统发短信

    #import <MessageUI/MessageUI.h> MFMessageComposeViewControllerDelegate #pragma mark - 调用ios系统短 ...

  2. ORM框架Hibernate (一) 对DAO封装和抽象

    说明 前面已经给大家介绍了Struts这个框架,Struts是对Web项目的表示层进行了封装,而Hibernate是对Web项目中DAO层进行封装,也即是.NET中我们常用到的D层封装,即对访问数据库 ...

  3. char与byte的差别

    非常多刚開始学习的人(包含我,已经学了一年多java了)肯会对char和byte这两种数据类型有所疑惑,相互混淆,今天特地查了好多资料,对byte和char两种数据类型进行了总结和比較,先将结果与大家 ...

  4. gcc和arm-linux-gcc区别

    安装arm-linux-gcc的时候,查了不少资料,总算环境搭好了.于是,想写个程序员的经典程序---hello world. 语法都没错,生成test.c. 命令行运行:arm-linux-gcc ...

  5. JS--Div中数据滚动到最后一条重新从头开始滚动

    在做东西时要求让数据滚动,且滚动到最后一条时,从头再开始滚动,样图如下:

  6. No DEFAULT or UI configuration directive found!

    虚拟机安装OracleLinux出错: No DEFAULT or UI configuration directive found! 解决方法:我使用第一条就成功了 在报错信息后面的boot命令行输 ...

  7. Core Bluetooth 概述 【官方文档翻译】

    Core Bluetooth 框架在Mac和iOS平台,为配备了低功耗蓝牙无线技术的设备提供了进行通信所需要的类.例如,您的应用程序可以发现,探索,和低功耗的外围设备进行交互,如心率监视器.数字温控器 ...

  8. Linq 查询基本操作

    - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 - let 子句 - 复合from子句 - 在某 ...

  9. nginx,php日志分割

    作者:zhanhailiang 日期:2014-01-06 默认nginx只会生成一个access.log和一个error.log,并且每天不断积累,日志文件会变的非常大,如果需要做一下日志的分析,无 ...

  10. Struts2 校验

    Struts2校验格式: actionName-methodName-invalidation.xml  该配置文件必须和action类在同一个包下. <?xml version="1 ...