题目如下:

On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].

Here, board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"], as shown in the diagram below.

We may make the following moves:

  • 'U' moves our position up one row, if the position exists on the board;
  • 'D' moves our position down one row, if the position exists on the board;
  • 'L' moves our position left one column, if the position exists on the board;
  • 'R' moves our position right one column, if the position exists on the board;
  • '!' adds the character board[r][c] at our current position (r, c) to the answer.

(Here, the only positions that exist on the board are positions with letters on them.)

Return a sequence of moves that makes our answer equal to target in the minimum number of moves.  You may return any path that does so.

Example 1:

Input: target = "leet"
Output: "DDR!UURRR!!DDD!"

Example 2:

Input: target = "code"
Output: "RR!DDRR!UUL!R!"

Constraints:

  • 1 <= target.length <= 100
  • target consists only of English lowercase letters.

解题思路:本题难度不大,把整个表格抽象成一个坐标轴,例如a的坐标是(0,0),l的坐标是(2,1),那么从a到l的路径就是要左右方向上移动一次(l的x坐标减去a的x坐标),上下的方向上移动两次(l的坐标减去a的y坐标)。这里有一点需要注意的的是起始或者终止的字符是z,因为不管是从z出发还是到达z,一定只能从经过u到达,而不能经由z的左边。

代码如下:

class Solution(object):
def alphabetBoardPath(self, target):
"""
:type target: str
:rtype: str
"""
board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
dic = {}
for i in range(len(board)):
for j in range(len(board[i])):
dic[board[i][j]] = (i,j)
x,y = 0,0
res = ''
for i in target:
x1,y1 = dic[i]
#
if i == 'z':
v = y - y1
if v > 0:
res += 'L' * v
else:
res += 'R' * (-v)
v = x - x1
if v > 0:
res += 'U' * v
else:
res += 'D' * (-v)
else:
v = x - x1
if v > 0:
res += 'U' * v
else:
res += 'D' * (-v)
v = y - y1
if v > 0:
res += 'L'*v
else:
res += 'R' * (-v)
res += '!'
x,y = x1,y1
return res

【leetcode】1138. Alphabet Board Path的更多相关文章

  1. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  2. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

  3. 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  4. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  5. 【leetcode】Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  6. 【leetcode】Binary Tree Maximum Path Sum (medium)

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  7. 【leetcode】1289. Minimum Falling Path Sum II

    题目如下: Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactl ...

  8. 【leetcode】931. Minimum Falling Path Sum

    题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...

  9. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

随机推荐

  1. 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_04.mybatis概述

  2. delphi xe2 64位嵌入汇编问题 https://bbs.csdn.net/topics/390333981

    Function xxx(xxx):xxx;assembler;asm  XOR RAX , RAX  ...end;这样的可以. 0 0 引用 ・ 举报 ・ 管理 5t4rk   回复于 2013- ...

  3. Mysql登录报1045错误

    MySQL在使用root密码登陆报 1045 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password ...

  4. python里一个class可以定义多个构造函数

    不行,一个class只能有一个用于构造对象的__init__函数但python中的变量是无类型的,因此传给__init__的参数可以是任何类型python中的函数参数在定义时可以有默认值,可以让__i ...

  5. supervisor启动elk7.4.0组件

    es [program:elasticsearch] command = /srv/app/elk/elasticsearch/bin/elasticsearch autostart = true s ...

  6. [19/06/07-星期五] CSS基础_布局&定位&背景样式

    一.固定布局(不适应设备的浏览器的变化) <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...

  7. 如何学习MySQL数据库管理员(OCP)认证(转)

    如何学习MySQL数据库管理员(OCP)认证 转自:   甲骨文专家中,MySQL 5.6数据库管理员( OCP )证明您的安装和优化MySQL服务器,设置复制和安全,执行数据库备份及性能优化和保护M ...

  8. PostgreSQL索引思考

    当在看Monetdb列存行只支持IMPRINTS和ORDERED这两种索引,且只支持定长数值类型时,就在思考,对于列存,还有必要建索引吗?在PostgreSQL的索引就要灵活很多,我对常用列建合理的索 ...

  9. HashMap底层为什么一定用数组

    HashMap源码数据结构: Entry[] table = new Entry[capacity]; 其中,Entry就是一个链表节点.如果将数组替换成LinkedList是否可行?如下: List ...

  10. Zend_Cache的使用

    一.Zend_Cache快速浏览 Zend_Cache 提供了一个缓存任何数据的一般方法. 在Zend Framework中缓存由前端操作,同时通过后端适配器(File, Sqlite, Memcac ...