码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 246: Strobogrammatic Number

时间:2017-12-18 14:27:23      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:str   ted   cti   ret   false   ++   else   down   ber   

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 }

 

Leetcode 246: Strobogrammatic Number

标签:str   ted   cti   ret   false   ++   else   down   ber   

原文地址:http://www.cnblogs.com/liangmou/p/8056940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!