题目如下:

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. Delphi XE2 之 FireMonkey 入门(39) - 控件基础: TScrollBox、TVertScrollBox、TFramedScrollBox、TFramedVertScrollBox

    Delphi XE2 之 FireMonkey 入门(39) - 控件基础: TScrollBox.TVertScrollBox.TFramedScrollBox.TFramedVertScrollB ...

  2. MYSQL启用数据库错误:ERROR 2002 (HY000)

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) MYSQL服务 ...

  3. 【奇技淫巧】linux 定时任务 crontab 反弹 shell

    日期:2018-11-26 13:47:34 介绍:如何使用定时任务来反弹 shell? 0x01. 基本命令 参数 -e:编辑该用户的计时器设置: -l:列出该用户的计时器设置: -r:删除该用户的 ...

  4. ETF替代规则

    0)禁止现金替代:是指在申购.赎回基金份额时,该成份证券不允许使用现金作为替代. 1)允许现金替代:是指在申购基金份额时,允许使用现金作为全部或部分该成份证券的替代,但在赎回基金份额时,该成份证券不允 ...

  5. 打印GC日志

    所需参数如下: -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -verbose:gc -Xloggc:gc.log 会在根目录生成 gc.log 文件,里面记录 ...

  6. centos7:storm集群环境搭建

    1.安装storm 下载storm安装包 在线下载 wget http://apache.fayea.com/storm/apache-storm-1.1.1/apache-storm-1.1.1.t ...

  7. Pycharm激活方法(license server方法)

    pycharm所有版本 http://www.jetbrains.com/pycharm/download/previous.html 打开激活窗口 选择 Activate new license w ...

  8. redis学习(二)

    深入了解redis字符串,列表,散列和有序集合命令,了解发布,订阅命令和其他命令.   一,字符串   1.字符串可以存储3种类型的值 字符串,整数,浮点数 2.运算命令列表 incr : incr ...

  9. pyhton之解析html的表格

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' from bs4 import BeautifulS ...

  10. FastDFS分布式文件服务器

    5.分布式文件服务器FastDFS(阿里巴巴) 5.1什么是FastDFS FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制,充分考虑了冗余备份.负载均衡 ...