[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;
    }
}
时间: 2024-08-23 22:53:28

[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 characters. For example, Given s = “eceba”, T is "ece" which its length is 3. Analysis: This is a very very typical question in using slide wind

[LeetCode] 163. Missing Ranges 缺失区间

Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. Example: Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,Output: ["2", "4->49", "51-

leetcode[163]Missing Ranges

Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76-

【LeetCode】Missing Ranges

Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74&q

LeetCode – Refresh – Missing Ranges

1 class Solution { 2 public: 3 string getRange(int start, int end) { 4 ostringstream oss; 5 if (start == end) { 6 oss << start; 7 } else { 8 oss << start << "->" << end; 9 } 10 return oss.str(); 11 } 12 vector<strin

[LeetCode] 246. Strobogrammatic Number 对称数

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", "

Leetcode 246: Strobogrammatic Number

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", "

[LeetCode] 248. Strobogrammatic Number III 对称数III

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. For example,Given low = "50&qu

[LeetCode] 247. Strobogrammatic Number II 对称数II

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Find all strobogrammatic numbers that are of length = n. For example,Given n = 2, return ["11","69","88","96"