[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", "88", and "818" are all strobogrammatic.

翻转180度后对称的数有:8->8, 0->0, 1->1, 6->9, 9->6,从两边向中间检查对应位置的两个数是否满足对称数就行了。比如619,先判断6和9是有映射的,然后1和自己又是映射,所以是对称数。有点像判断回文Palindrome,回文判断是否相等,这里判断是否满足那几个数字的条件。判断是可以直接写条件判断,也可以用HashMap存放数字的映射,然后用双指针从两边向中间查看。

Java:

public class Solution {
    public boolean isStrobogrammatic(String num) {
        HashMap<Character, Character> map = new HashMap<Character, Character>();
        map.put(‘1‘,‘1‘);
        map.put(‘0‘,‘0‘);
        map.put(‘6‘,‘9‘);
        map.put(‘9‘,‘6‘);
        map.put(‘8‘,‘8‘);
        int left = 0, right = num.length() - 1;
        while(left <= right){
            if(!map.containsKey(num.charAt(right)) || num.charAt(left) != map.get(num.charAt(right))){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

Python:

class Solution:
    lookup = {‘0‘:‘0‘, ‘1‘:‘1‘, ‘6‘:‘9‘, ‘8‘:‘8‘, ‘9‘:‘6‘}

    def isStrobogrammatic(self, num):
        n = len(num)
        for i in xrange((n+1) / 2):
            if num[n-1-i] not in self.lookup or                num[i] != self.lookup[num[n-1-i]]:
                return False
        return True

C++:

class Solution {
public:
    bool isStrobogrammatic(string num) {
        unordered_map<char, char> m {{‘0‘, ‘0‘}, {‘1‘, ‘1‘}, {‘8‘, ‘8‘}, {‘6‘, ‘9‘}, {‘9‘, ‘6‘}};
        for (int i = 0; i <= num.size() / 2; ++i) {
            if (m[num[i]] != num[num.size() - i - 1]) return false;
        }
        return true;
    }
};

 

类似题目: 

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

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

  

原文地址:https://www.cnblogs.com/lightwindy/p/8491309.html

时间: 2024-12-13 06:26:22

[LeetCode] 246. Strobogrammatic Number 对称数的相关文章

[LeetCode] 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"

LeetCode:Happy Number - 快乐数

1.题目名称 Happy Number(快乐数) 2.题目地址 https://leetcode.com/problems/happy-number/ 3.题目内容 英文: Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, repla

[LeetCode#247] Strobogrammatic Number II

Problem: 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",&quo

Leetcode 248: Strobogrammatic Number 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

246. Strobogrammatic Number

首先找到那几种是可以Strobogrammatic的 然后一个walker,一个runner,然后walker <= runner的时候比较是否是一组一组的 要等于因为2就并不是Strobogrammatic 1 public boolean isStrobogrammatic(String num) { 2 Map<Character, Character> map = new HashMap<Character, Character>(); 3 map.put('6',

[LeetCode] 260. Single Number III 单独数 III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is