<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)246:中心對稱數(shù)

          共 2499字,需瀏覽 5分鐘

           ·

          2021-04-25 14:01

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 中心對稱數(shù),我們先來看題面:
          https://leetcode-cn.com/problems/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.

          中心對稱數(shù)是指一個數(shù)字在旋轉了 180 度之后看起來依舊相同的數(shù)字(或者上下顛倒地看)。
          請寫一個函數(shù)來判斷該數(shù)字是否是中心對稱數(shù),其輸入將會以一個字符串的形式來表達數(shù)字。

          示例


          示例 1:
          輸入: "69"
          輸出: true

          示例 2:
          輸入: "88"
          輸出: true

          示例 3:
          輸入: "962"
          輸出: false


          解題


          翻轉后對稱的數(shù)就那么幾個,我們可以根據(jù)這個建立一個映射關系:8->8, 0->0, 1->1, 6->9, 9->6,然后從兩邊向中間檢查對應位置的兩個字母是否有映射關系就行了。比如619,先判斷6和9是有映射的,然后1和自己又是映射,所以是對稱數(shù)。

          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;
              }
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-240題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)241:為運算表達式設計優(yōu)先級
          LeetCode刷題實戰(zhàn)242:有效的字母異位詞
          LeetCode刷題實戰(zhàn)243:最短單詞距離
          LeetCode刷題實戰(zhàn)244:最短單詞距離 II
          LeetCode刷題實戰(zhàn)245:最短單詞距離 III

          瀏覽 32
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产黄视| 日本a视频在线 | 日本不卡高清视频 | 大黑屌视频 | 欧美日韩网|