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. 文件权限和目录权限详解(rwx)

    [文件] r:可读,可以使用cat命令查看文件内容: w:可写,可以编辑或删除文件: x:可执行,可以当作命令提交给内核 [目录] r:可以对此目录执行ls,列出内部所有文件 w:可以在此目录创建文件 ...

  2. 后缀自动机(SAM)

    *在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...

  3. datatables常见报错

    Uncaught TypeError: Cannot read property 'style' of undefined 分析:列表配置 columnDefs 列数不匹配 来自为知笔记(Wiz)

  4. JS一些应用脚本

    脚本一: //重置下标(可以修改成自己所需要的脚本) function ReSetBoxOrder() { var q = 0;//下标值 var a = 0;//暂未用到 //循环当前tbody下的 ...

  5. OC多文件开发介绍

    OC多文件开发介绍: 1.为什么要使用多文件? 在工作中,通常把不同的类放到不同的文件中,每个类的声明和实现分开,声明写在.h头文件中,实现写在相应的.m文件中去,类名是什么,文件名的前缀就是什么.假 ...

  6. Open judge 07和为给定数

    总时间限制: 1000ms 内存限制: 65536kB 传送门 类似的题传送门 描述 给出若干个整数,询问其中是否有一对数的和等于给定的数. 输入 共三行: 第一行是整数n(0 < n < ...

  7. Ubuntu1404+Django1.9+Apache2.4部署配置2配置文件设置

    转载注明出处,个人博客:http://www.cnblogs.com/wdfwolf3/ Django首要的部署平台是WSGI,它是Python Web服务器和应用的标准.使用Apache和mod_w ...

  8. leetcode中一些要用到动态规划的题目

    需要仔细回顾的题目: 1.Interleaving String   交叉存取字符串 2.Decode Ways   字符串解码 3.Subsets   Subsets II          求一个 ...

  9. 简易的highcharts公共绘图模块封装--基于.net mvc

    运行效果: 之所以选择这个图表插件,是因为它较其他同类插件轻量且中文文档详细完整,Demo丰富,配置使用简单.具体内容请登录中文官网:http://www.hcharts.cn/ 项目详细: 项目环境 ...

  10. 两个winform窗体同步

    /// <summary>        /// 初始left距离        /// </summary>        int initx = 0;        /// ...