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的更多相关文章

  1. [LeetCode#159] Missing Ranges Strobogrammatic Number

    Problem: Given a string, find the length of the longest substring T that contains at most 2 distinct ...

  2. LeetCode 163. Missing Ranges (缺失的区间)$

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  3. [LeetCode#163] Missing Ranges

    Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...

  4. [leetcode]163. Missing Ranges缺失范围

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  5. [LeetCode] 163. Missing Ranges 缺失区间

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  6. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  7. 【LeetCode】Missing Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  8. [LeetCode] 228. Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...

  9. LeetCode 246. Strobogrammatic Number (可颠倒数字) $

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

随机推荐

  1. ASP.NET+ashx+jQuery动态添加删除表格

    aspx: <script src="../script/jquery-1.4.4.min.js" type="text/javascript" lang ...

  2. Base64原理简介

    Base64是一种编码方式,通常用于将二进制数据转换成可见字符的形式,该过程可逆. 过程大致如下: 1. 对64个可见字符,进行一个索引编码.索引是二进制的值,对应找到一个可见字符. Base64 编 ...

  3. MICROSOFT REPORT VIEWER 2012之无法加载相关的dll

    使用VS 2012开发报表, 如果是使用的微软的报表控件的话,默认是使用的MICROSOFT REPORT VIEWER 2012,本地开发基本上没问题,但是一发布服务器,就会发现坑了,微软挖坑从来就 ...

  4. python基础知识五

    数据结构基本上就是---它们可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的. python中有三种内建的数据结构---列表.元祖和字典. 我们将会学习如何使用它们,以及它们如何使编程变得 ...

  5. javascript 动态操作Html

    <html> <body> <p>aaaaa</p> <input type="button" value="con ...

  6. SQL Server备份还原数据库中的小把戏

    备份数据库时出现一个不太了解的错误 ,错误信息“is formatted to support  1 media families, but 2 media families are expected ...

  7. java加密类型和算法名称

    项目里有各种加密方法,但从来没有仔细研究过.一般只是copy.这几天遇到一些问题,看了一下加密代码,觉得有些疑惑. 我们知道jdk已经为我们包装好了很多的算法.但究竟包装了哪些算法,怎么去掉这些算法我 ...

  8. jQuery慢慢啃之回调(十三)

    1.callbacks.add(callbacks)//回调列表中添加一个回调或回调的集合 // a sample logging function to be added to a callback ...

  9. php解决下单、抽奖并发导致的库存负数的问题

    我们知道数据库处理sql是一条条处理的,假设购买商品的流程是这样的: sql1:查询商品库存 if(库存数量 > 0) {     //生成订单...     sql2:库存-1 } 当没有并发 ...

  10. Debian6安装XP系统

    1.下载一键包,必须要在 root目录 下执行,可以先用 pwd 命令查看当前所在目录.执行命令:wget http://d.yxlgh.com/vps/zmdebian6xp.sh 2.执行 zmd ...