LeetCode Weekly Contest 147 2問目 - Alphabet Board Path [1138]

Source

LeetCode Weekly Contest 147
問題文

問題概要

省略

解法

省略

cLayversion 20190802-1)のコード [C++に変換後]

#include<bits/stdc++.h>
using namespace std;
const char *board[6] = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z    "};
int x[26];
int y[26];
class Solution{
  public:
  string alphabetBoardPath(string target){
    int cx=0, cy=0, i, j, k;
    string res;
    for(i=0;i<(6);i++){
      for(j=0;j<(5);j++){
        if('a' <= board[i][j]  &&  board[i][j] <= 'z'){
          k = board[i][j] - 'a';
          x[k] = i;
          y[k] = j;
        }
      }
    }
    for(i=0;i<(target.size());i++){
      k = target[i] - 'a';
      while(cx > x[k]){
        cx--;
        res += "U";
      }
      while(cy > y[k]){
        cy--;
        res += "L";
      }
      while(cx < x[k]){
        cx++;
        res += "D";
      }
      while(cy < y[k]){
        cy++;
        res += "R";
      }
      res += "!";
    }
    return res;
  }
}
;

// cLay varsion 20190802-1

// --- original code ---
// const char *board[6] = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z    "};
// int x[26], y[26];
// 
// class Solution {
// public:
//   string alphabetBoardPath(string target) {
//     int i, j, k;
//     int cx = 0, cy = 0;
//     string res;
//     
//     rep(i,6) rep(j,5) if('a' <= board[i][j] <= 'z'){
//       k = board[i][j] - 'a';
//       x[k] = i;
//       y[k] = j;
//     }
// 
//     rep(i,target.size()){
//       k = target[i] - 'a';
//       while(cx > x[k]) cx--, res += "U";
//       while(cy > y[k]) cy--, res += "L";
//       while(cx < x[k]) cx++, res += "D";
//       while(cy < y[k]) cy++, res += "R";
//       res += "!";
//     }
// 
//     return res;
//   }
// };
// 
// {
//   // main関数を適当に処理する
// }

Current time: 2024年04月20日14時45分13秒
Last modified: 2019年08月10日16時44分08秒 (by laycrs)
Tags: Competitive_Programming_Incomplete LeetCode
トップページに戻る

Logged in as: unknown user (not login)

ログイン: