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.

 1 public class Solution {
 2     public bool IsStrobogrammatic(string num) {
 3         if (num == null || num.Length == 0) return true;
 4
 5         int i = 0, j = num.Length - 1;
 6
 7         while (i <= j)
 8         {
 9             if ((num[i] == ‘6‘ && num[j] == ‘9‘) || (num[i] == ‘9‘ && num[j] == ‘6‘) || (num[i] == ‘8‘ && num[j] == ‘8‘) || (num[i] == ‘0‘ && num[j] == ‘0‘) || (num[i] == ‘1‘ && num[j] == ‘1‘))
10             {
11                 i++;
12                 j--;
13             }
14             else
15             {
16                 return false;
17             }
18         }
19
20         return true;
21     }
22 }
时间: 2024-11-08 23:36:05

Leetcode 246: Strobogrammatic Number的相关文章

[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#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#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"

[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 Strobogrammatic Number II

原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-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