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. 一些常用运行命令和CMD命令

    运行命令 1. 进入服务页面的命令: services.msc 2. 远程连接命令:mstsc.exe 3. 配置电脑启动项 msconfig 4. 计算器 calc.exe 5. 设定关机时间(se ...

  2. B. Sereja and Mirroring

    B. Sereja and Mirroring time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. Jquery ui datepicker 设置日期范围,如只能隔3天

    最近的后台项目前端使用了jquery ui 日历控件自然就使用了jquery ui 的   datepicker 后台数据比较好大,一般是千万级的和百万级的关联,查询会很慢,所以后加想多加些过滤条件, ...

  4. 【Java基础】单例模式

    单例模式:保证整个项目运作期间某一个对象有且只有一个. 主要有:1,饿汉式:2,懒汉式. class Singleton1//饿汉式 { private Singleton1(){}//先把构造方法私 ...

  5. URL 对特殊字符的处理

    看到很多人说可以通过转移字符来进行转义,避免URL在请求的时候出错. 现在有了更好的方法了.不然还不得把半个ASCII码表给进行转义了! import java.io.UnsupportedEncod ...

  6. nodejs实现接收Snmp的Trap消息

    var assert = require('assert'); var ASN1 = { EOC: 0, Boolean: 1, Integer: 2, BitString: 3, OctetStri ...

  7. iOS中通知的添加和移除

    我们都知道viewWillAppear:方法是在控制器的view将要显示的时候调用的,而viewWillDisappear:方法是在控制器的view将要隐藏的时候调用.很多时候我们根据自身需要将相关代 ...

  8. ios 文字图标

    如何使用自定义字体 在讲icon font之前,首先先来看看普通自定义字体是如何在ios中使用的,两个原理是一样的.这里以KaushanScript-Regular为例: Step 1: 导入字体文件 ...

  9. IOS开发系列 --- 核心动画

    原始地址:http://www.cnblogs.com/kenshincui/p/3972100.html 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥i ...

  10. jQuery.on() 函数详解

    on() 函数用于为指定元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于 ...