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 }

 

posted @ 2017-12-18 12:21  逸朵  阅读(239)  评论(0)    收藏  举报