[LeetCode#246] Missing Ranges Strobogrammatic Number
Problem:
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
For example, the numbers "69", "88", and "818" are all strobogrammatic.
Analysis:
This kind of problem is very very easy!!! Keep clam and carry on!
Basic idea:
1. identify which digits are valid for construct a Strobogrammatic Number.
Instant idea: 0, 1, 8.
Hint from the question: 6, 9
private boolean isStroboDigit(Character c) {
if (c == '0' || c == '1' || c == '6' || c == '8' || c == '9')
return true;
return false;
} 2. To look a num upside own is equal to reverse it. (with '6' change into '9', '9' change into '6')
char c = num.charAt(i);
if (isStroboDigit(c)) {
if (c == '6')
buffer.append('9');
else if(c == '9')
buffer.append('6');
else
buffer.append(c);
} else {
return false;
} My common mistakes in implementation:
1. write : (forget to change i++ into i--, when realizing we should scan from the last charcter!)
for (int i = len - 1; i >= 0; i--)
into
for (int i = len - 1; i >= 0; i++) 2. return buffer.toString.equals(num);
Forget () after toString method.
Solution:
public class Solution {
public boolean isStrobogrammatic(String num) {
if (num == null)
throw new IllegalArgumentException("num is null");
int len = num.length();
if (len == 0)
return true;
StringBuffer buffer = new StringBuffer();
for (int i = len - 1; i >= 0; i--) {
char c = num.charAt(i);
if (isStroboDigit(c)) {
if (c == '6')
buffer.append('9');
else if(c == '9')
buffer.append('6');
else
buffer.append(c);
} else {
return false;
}
}
return buffer.toString().equals(num);
} private boolean isStroboDigit(Character c) {
if (c == '0' || c == '1' || c == '6' || c == '8' || c == '9')
return true;
return false;
}
}
[LeetCode#246] Missing Ranges Strobogrammatic Number的更多相关文章
- [LeetCode#159] Missing Ranges Strobogrammatic Number
Problem: Given a string, find the length of the longest substring T that contains at most 2 distinct ...
- LeetCode 163. Missing Ranges (缺失的区间)$
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- [LeetCode#163] Missing Ranges
Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...
- [leetcode]163. Missing Ranges缺失范围
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- 【LeetCode】Missing Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- [LeetCode] 228. Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- LeetCode 246. Strobogrammatic Number (可颠倒数字) $
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
随机推荐
- HTML+CSS 整站 步骤
文件夹管理: CSS JS img font html 根据设计图,划分区块 ,即页面布局 重置样式 ;padding:0;} 写main.css 注意:1 距离尽量使用偶数,避免奇数 2 在使用定 ...
- PHP制作简单的日历
在这里分享一个PHP制作的日历 <?php //万年历if($_GET['year']){$year = $_GET['year'];}else{$year = date("Y&quo ...
- 基于.net 职责链来实现 插件模式
插件式的例子 QQ电脑管家,有很多工具列表,点一下工具下载后就可以开始使用了 eclipse ,X Server 等等 插件式的好处 插件降低框架的复杂性,把扩展功能从框架中剥离出来 让第三方有机会来 ...
- (转)Asp.net的HttpCookie写入汉字读取时为乱...
今天有个问我:在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cook ...
- [压缩解压缩] SharpZip--压缩、解压缩帮助类
里面有三个类都是用于压缩和解压缩的.大家看下图片 看下面代码吧 /// <summary> /// 类说明:SharpZip /// 编 码 人:苏飞 /// 联系方式:361983679 ...
- struts2-ognl 访问静态方法
在内网基本上还真没看到有哥们发现这个问题, 在google上有的哥们说 这是 v 2.3.20的一个bug, 有的人说在该版本中已经不建议通过ognl方式访问静态方法了. 对于这两种说法, 我比较赞同 ...
- java 反射 - 获取成员变量的值.
通过反射,可以获取所有声明的成员变量(包括所有的),代码如下: package spt.test.src; public class Person { private String name = &q ...
- java实现时间的比较
时间大小的比较以及把String类型的时间转换为Date类是时间在开发中是非常常见的,下面的主要是一个工具方法 public class Test { public static void main( ...
- 一个简单的web服务器例子
一个简单的web容器小例子,功能十分简单,只能访问静态资源,对于新手来说还是有一定的意义.主要分三个类 1.server类:主要功能开启socketServer,阻塞server,接收socket访问 ...
- 转:探讨android更新UI的几种方法
本文转自:http://www.cnblogs.com/wenjiang/p/3180324.html 作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因 ...