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 ...
随机推荐
- JavaScript 运行机制详解:深入理解Event Loop
Philip Roberts的演讲<Help, I'm stuck in an event-loop>,详细.完整.正确地描述JavaScript引擎的内部运行机制. 一.为什么JavaS ...
- 【Oracle】删除重复记录
--复习autotrace: SET AUTOTRACE OFF --不生成AUTOTRACE 报告,这是缺省模式 SET AUTOTRACE ON EXPLAIN --AUTOTRACE只显示优化器 ...
- PHP 超强过滤函数
PHP 超强过滤函数 你有每次要过滤的时候总是去翻曾经的过滤代码的时候么? 你有搜索过怎样防过滤,防攻击的PHP解决方法么? 你有对全然遵循'过滤输入,避免输出',Web界经典说辞么? 事实上 ...
- UVA 12125 March of the Penguins
题意: 给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1 ...
- 出发 Let's Go
今天是中秋佳节,而恰好我这天过生日,晚上睡觉前又恰好听到温岚唱的祝我生日快乐,心里挺高兴的. 最近,由于公司需要,可能要学习Python和Tribon了,全是未知的,一点不了解的东西,也忽然想起了在这 ...
- 配置Apache2 管理 SVN
软件环境:CentOS-7-x86_64 1.安装 mod_dav_svn 模块 2.在Apache2下增加管理配置,如: cd /etc/httpd/conf.d vim subversion. ...
- hiho 分冶专题
hiho的每周一题都不会很难,基本上就是一些很裸和经典的问题,这一次写了几道分冶专题的题,做个总结. 分冶最简单的就是二分,二分说简单,很简单,不过7,8行代码,不过也常常写挂,写成无限循环. 直接看 ...
- 一种获取spring环境上下文方法:SpringContextUtil
获得spring里注册Bean的有好几种方法,这里介绍一种比较简单的方法: import org.springframework.beans.BeansException; import org.sp ...
- 一个完整的SSL连接建立过程
客户端浏览器连接到Web服务器,发出建立安全连接通道的请求. 服务器接受客户端请求,发送服务器证书做为响应. 客户端验证服务器证书的有效性,如果验证通过,则用服务器证书中包含的服务器公钥加密一个会话密 ...
- 匈牙利算法(素数伴侣(HW1112))
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<vector> #include<string&g ...