LeetCode Day2
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的更多相关文章
- 【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 ...
- leetcode每日刷题计划-简单篇day2
今天数模比赛爆肝&操作系统大作业 脖子疼orz先把题过了保证flag不倒..个别细节回头看吧 Num 13 罗马数字转整数 Roman to Integer 一遍提交过,开始编译出了点问题 具 ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- 【从零开始学BPM,Day2】默认表单开发
[课程主题]主题:5天,一起从零开始学习BPM[课程形式]1.为期5天的短任务学习2.每天观看一个视频,视频学习时间自由安排. [第二天课程] Step 1 软件下载:H3 BPM10.0全开放免费下 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- gdb 调试coredump文件过程
gdb 调试coredump文件过程: 第一步:首先需要一个进程的coredump文件,怎么搞出coredump文件呢? 1. ps -fax|grep 进程名称 找到 ...
- Timer和TimerTask
目录结构: Timer和TimerTask 一个Timer调度的例子 如何终止Timer线程 关于cancle方式终止线程 反复执行一个任务 schedule VS. scheduleAtFixedR ...
- JAVA常见异常集锦(持续更新)
No1:Nested in org.springframework.beans.factory.parsing.BeanDefinitionParsingException 2013-07-02 10 ...
- IT人士的职业规范——凝视
这两天将系统敲完了,该总体调试了,调试的过程中,发现了一个非常大的问题,就是自己的凝视写的不够,有时候不明确U层这个事件是做什么的,有时候不知道这个事件传递的是什么參数,有时候不知道相应的B层和 ...
- synchronized关键字的用法
synchronized用于给方法或者块加锁用的,只有获得该对象或者块的锁的对象才能够执行里面的代码,否则将阻塞在那里,等待该锁被释放,然后获得该锁继续执行.比如下面模拟售票的代码: /** * 模拟 ...
- C#调用SAPWebService
其实和调用其它WebService 没有很大不同 只是不了解SAP的人 可能不太明白 SAP接口中的相关参数 //调用接口 , 创建对象 ServiceReference1.Z_IF_MM_VEND ...
- linux学习笔记之硬盘分区
引用:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/03/2997098.html PS:本文仅对知识点作总结.详情请参考原文. 首先 ...
- Windows 端口和任务 查看 相关命令
netstat -aon|findstr " //查看端口 tasklist|findstr " //根据PID找到对应的程序 taskkill /f /t /im netbox. ...
- pom文件详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Android url中文编码问题
最近项目遇见一个很奇葩问题,关于URL问题,项目中加载图片,图片的URL含有中文,但是,我的手机可以加载,没问题,同事也都可以,但是测试手机却不可以,加载失败,找到问题,就是URL含有中文问题. 解决 ...