题目如下:

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. multiple datasource config

    Hi Harshit S. project structure: multiple datasource config as follows: step 1: step 2:add a datasou ...

  2. Flink容错机制

    Flink的Fault Tolerance,是在在Chandy Lamport Algorithm的基础上扩展实现了一套分布式Checkpointing机制,这个机制在论文"Lightwei ...

  3. Java多线程学习——图片下载

    实现多线程方式1:继承类Thread——重写方法run——调用方法start 从网络下载图片首先要增加包commons-io.jar import org.apache.commons.io.File ...

  4. MySQL学习-数据库设计以及sql的进阶语句

    1.数据库设计 关系型数据库建议在E-R模型的基础上,我们需要根据产品经理的设计策划,抽取出来模型与关系,制定出表结构,这是项目开始的第一步 在开发中有很多设计数据库的软件,常用的如power des ...

  5. java保留2位或n位小数

    1.直接使用字符串处理 double ds = Double.valueOf(String.format("%.3f", Math.random()).toString()); 这 ...

  6. 【Linux开发】Ubuntu下几个软件的配置记录backup

    调用ubuntu命令行的方法:ctrl+alt+t gcc -o test test.c 开发工具包括eclipse,Qt等全部放入了/opt/路径下,java开发环境放在了/usr/local/jd ...

  7. kafka学习(四)

    集群成员关系 kafka使用Zookeeper 来维护集群成员的信息.每个broker都有一个唯一标识符,这个标识符可以在配置里指定,也可以自动生成.在broker启动的时候,它通过创建临时节点把自己 ...

  8. 决解nginx代理的django项目的admin站点无法访问,和没样式的问题。

    首先我们先解决无法访问admin站点的问题 首先我们先修改一下nginx的配置,添加红色框框的部分. 然后重新启动一下nginx 访问一下admin站点 发现没有样式了. 我们先修改/fast_foo ...

  9. 20191127 Spring Boot官方文档学习(4.18-4.24)

    4.18.JTA的分布式事务 通过使用Atomikos或Bitronix嵌入式事务管理器,Spring Boot支持跨多个XA资源的分布式JTA事务.部署到合适的Java EE应用程序服务器时,还支持 ...

  10. [DS+Algo] 002 一维表结构

    目录 1. 顺序表 1.1 分类 1.2 实现方式 1.3 扩容问题 1.4 操作 2. 链表 2.1 分类 2.2 链表相关操作 2.3 链表 VS 顺序表 3. 关于代码实现 1. 顺序表 1.1 ...