[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 ...
随机推荐
- JavaScript的DOM操作(三)
1.相关元素操作: var a = document.getElementById("id"); var b = a.nextSibling,找a的下一个同辈元素,注意空格 var ...
- ionic 项目分享【转】No.3
写在文章前:由于最近研究ionic框架,深感这块的Demo寥寥可数,而大家又都藏私,堂堂天朝,何时才有百家争鸣之象,开源精神吾辈当仁不让! 原文地址暂时忘记了 ,如果有知道的麻烦在评论处帮忙说一下 , ...
- html input[type=file] css样式美化【转藏】
相信做前端的都知道了,input[type=file]和其他输入标签不一样,它的事件必须是在触发自身元素上,而不能是其他元素.所以不能简单的把input隐藏,放个button上去. <a hre ...
- PHP获得header头进行分析
学web的人都知道,要深刻的理解就一定要对HTTP协议有深刻的理解,这样你才能理解整个运行的流程,有些功能你才能理解应该 如何去实现,比如:仿盗链啊,定义IP后切换页面语种的版本啊,等等, 这里就来对 ...
- 导出文本、表格、图像到PDF格式文件中(学习整理)
1.测试例子: 需要导入的外部jar包: 相关API http://www.coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/pack ...
- [转]操作xml,将xml数据显示到treeview的C#代码
XmlDocument xml = new XmlDocument(); private void Form1_Load(object sender, EventArgs e) { CreateXML ...
- Bayeux协议
Bayeux 协议-- Bayeux 1.0草案1 本备忘录状态 This document specifies a protocol for the Internet community, and ...
- 从网页psd到html的开发
从网上下载了一张psd的网页设计稿,初学html+css,所以记录一下我的学习过程.原图是这个样子: 原图 ...
- 在 Sublime Text 3 中运行 PHP
参考http://segmentfault.com/blog/tony/1190000000395951 把php添加到环境变量 1.我的电脑->属性->高级系统设置->高级-> ...
- 使用PHP对文件进行压缩解压(zip)
使用虚拟主机进行文件上传时最常用的工具莫过于FTP了,但是使用FTP有一个弊端就是文件太多时上传或下载速度比较慢,如果上传时将文件打包,上传后在 空间解压缩,同样下载前将文件打包压缩以压缩包的形式下载 ...