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. 格而知之8:我所理解的Runtime(3)

    关联对象 14.使用Category对类进行拓展的时候,只能添加方法,而不适合添加属性(可以添加属性,也可以正常使用get方法和set方法,只是不会自动生成以下划线开头命名的成员变量). 可以通过关联 ...

  3. Oracle学习笔记(2)——过程和函数

    过程和函数统称为PL/SQL子程序,通过输入.输出参数或输入/输出参数与其调用者交换信息.他们是被命名的PL/SQL块,被编译后存储在数据库中,以备执行.因此,可以在数据库中直接按名称使用它们. 1. ...

  4. JVM工作原理和特点(一些二逼的逼神面试官会问的问题)

    作为一种阅读的方式了解下jvm的工作原理 ps:(一些二逼的逼神面试官会问的问题) JVM工作原理和特点主要是指操作系统装入JVM是通过jdk中Java.exe来完毕,通过以下4步来完毕JVM环境. ...

  5. 【git学习二】git基础之git管理本地项目

    1.背景        git基础打算分两部分来说,一部分是对于本地项目的管理,第二部分是对于远程代码仓库的操作. git运行本地项目管理包含对于相关文件的追踪,暂存区的比較分析,提交,撤销等功能. ...

  6. 玩转iOS开发 - JSON 和 Xml 数据解析

    前言 Json 和xml是网络开发中经常使用的数据格式,JSON轻量级.xml相对较复杂.所以如今用JSON的比例很大.基本上从server获取的返回数据都是JSON格式的,作为iOS开发人员,解析J ...

  7. 第一个MyBatis程序

    最近研究了一些MyBatis技术,虽然工作中还未用到,但是觉得了解一下也是不错的.这里记录了第一个简单的Demo程序,防止自己忘记. 第一步需要配置Mybatis-config.xml文件.注意:这里 ...

  8. 【CCTYPE函数系列】

    #include <cctype>的函数 c++中应该是#include <cctype> c中应该是#include <ctype.h> 以下为字符函数库中常用的 ...

  9. kvm 图形化安装

    为了再后续查看方便,我还是完整的记录kvm图形化安装. 介于网络环境的原因,我选择NAT. 2,安装kvm前的准备工作 2.1 关闭防火墙  setenforce 0    vi /etc/sysco ...

  10. 获取系统的IP

    如何获取Linux系统的ip.当时一直用这种方法获取IP的. ifconfig|grep 'Bcast'|awk -F ':' '{print $2}'|awk '{print $1}' 今天偶然发现 ...