携帯キーボード


1.携帯のキーボード

  • 1.親指は上下左右4方向にしか移動できず、キーボードを動かすセルの距離は1です.

  • 2.左の列の3つの数字1、4、7を入力する場合は、左手親指を使用します.

  • 3.右の列の3つの数字3、6、9を入力する場合は、右手親指を使用します.

  • 4.中間列の数字2、5、8、0を4つ入力する場合は、2つの親指の現在のキーボード位置に近い親指を使用します.
  • 4-1. 2つの親指の距離が等しい場合は、右利きは右手親指、左利きは左手親指を使用します.
  • //  numbers	                            hand	      result
    // [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5]	"right"	   "LRLLLRLLRRL"
    // [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2]	"left"	   "LRLLRRLLLRR"
    // [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]	    "right"	   "LLRLLRLLRL"
    2.方法

  • pseudo code

  • L=[1,4,7]/R=[3,6,9]

  • 次に、現在の左手と右手の位置を記録するLHRH変数を作成します.

  • forEachを使用して数値を巡回
  • 巡回時にL/Rに属する場合にRESULTを確立する.
  • 題は2,5,8,0時に左手と右手の位置を測定して距離
  • を求める
  • また、距離が同じなら手によって違います.
  • LHRH位置更新
  • 最終的には2,5,8,0で右手と左手の位置を測定して距離を求めることが肝心である.
    function solution(numbers, hand) {
      const pos = {
        1: [0, 0],
        2: [0, 1],
        3: [0, 2],
        4: [1, 0],
        5: [1, 1],
        6: [1, 2],
        7: [2, 0],
        8: [2, 1],
        9: [2, 2],
        "*": [3, 0],
        0: [3, 1],
        "#": [3, 2],
      };
    
      let lh = "*";
      let rh = "#";
      let result = "";
    
      function dis(num) {
        // 현채 lh,rh에서 누르려는 num 까지의 거리가 얼마냐를 측정
        const ld =
          Math.abs(pos[lh][0] - pos[num][0]) + Math.abs(pos[lh][1] - pos[num][1]);
    
        const rd =
          Math.abs(pos[rh][0] - pos[num][0]) + Math.abs(pos[rh][1] - pos[num][1]);
    
        if (ld === rd) return hand === "left" ? "L" : "R";
        return ld < rd ? "L" : "R";
      }
    
      for (let num of numbers) {
        if (num % 3 === 1) {
          result += "L";
          lh = num;
        } else if (num !== 0 && num % 3 === 0) {
          result += "R";
          rh = num;
        } else {
          result += dis(num);
          result[result.length - 1] === "R" ? (rh = num) : (lh = num);
        }
      }
    
      return result;
    }
    あはは!距離がPOSの配列では、各番号の位置を書き、最終距離=上下距離(POS[0])+左右距離(POS[1])の式で右手と左手の距離を比較する.
    計算#ケイサン#
    別の方法を発見して共有する
    function solution(numbers, hand) {
      hand = hand[0] === 'r' ? 'R' : 'L';
      let position = [1, 4, 4, 4, 3, 3, 3, 2, 2, 2];
      let h = { L: [1, 1], R: [1, 1] };
      return numbers
        .map(x => {
          if (/[147]/.test(x)) {
            h.L = [position[x], 1];
            return 'L';
          }
          if (/[369]/.test(x)) {
            h.R = [position[x], 1];
            return 'R';
          }
          let distL = Math.abs(position[x] - h.L[0]) + h.L[1];
          let distR = Math.abs(position[x] - h.R[0]) + h.R[1];
          if (distL === distR) {
            h[hand] = [position[x], 0];
            return hand;
          }
          if (distL < distR) {
            h.L = [position[x], 0];
            return 'L';
          }
          h.R = [position[x], 0];
          return 'R';
        })
        .join('');
    }
    regX.test()より簡潔な姿でより際立っています.hand,h={L,R}を用いて可読性を向上させることも注目すべき点である.
    最終的にはPOSを記録して一つ一つ計算しましょう.韓Qには解決策があると思って、ずっと頭が痛いです.