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 ...
随机推荐
- Spring整合Quartz
目录[-] 一.Spring创建JobDetail的两种方式 二.整合方式一示例步骤 1.将spring核心jar包.quartz.jar和Spring-context-support.jar导入类路 ...
- cf#366....
惨惨惨.... 我需要av.. b题意看错想了个加强版博弈结果发现完全没必要= =....cwa12到结束....中途想看d....只会n^4暴力啊.. 题解明天补上
- HBase的基本操作
1.输入hbase shell进入HBase shell
- ZendStudio快捷键 注释的快捷键
注:本文省略“通用快捷键”描述,诸如:ctrl+N=新建,ctrl+O=打开,ctrl+C=复制,ctrl+V,ctrl+X……等等几乎所有软件都通用的一组快捷键,而着重介绍zde独有的快捷键,了解并 ...
- a:hover span 隐藏/显示 问题
:hover是我们在CSS设计中最常运用的伪类之一,许多绚丽效果的实现离不开伪类:hover,比如我们常见的纯CSS菜单.相册效果等等. 或许用了这么久的伪类:hover,还有部分朋友还不完全了解ho ...
- 如何让secureCRT显示Linux的颜色
style="padding-bottom: 0px; line-height: 1.5; margin: 0px; padding-left: 0px; padding-right: 0p ...
- 1206: B.求和
题目描述 点击这里 对于正整数n,k,我们定义这样一个函数f,它满足如下规律 现在给出n和k,你的任务就是要计算f(n,k)的值. 输入 首先是一个整数T,表示有T组数据 接下来每组数据是n和k(1& ...
- 解决nginx上传模块nginx_upload_module传递GET参数
解决nginx上传模块nginx_upload_module传递GET参数的方法总结 最近用户反映我们的系统只能上传50M大小的文件, 希望能够支持上传更大的文件. 很显然PHP无法轻易实现大文件上传 ...
- 一步一步学python(四) - 字典
1.字典的使用 创建字典:phonebook = {'Alice': '1234' , 'Beth':'9120'} 2.dict函数 >>>items = [('name','Gu ...
- Nutch配置
http://www.linuxidc.com/Linux/2011-12/48782.htm http://wiki.apache.org/nutch/NutchHadoopTutorial htt ...