【leetcode】1138. Alphabet Board Path
题目如下:
On an alphabet board, we start at position
(0, 0)
, corresponding to characterboard[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 characterboard[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的更多相关文章
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
- 【LeetCode】687. Longest Univalue Path 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 【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 ...
- 【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. ...
- 【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 ...
- 【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 ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- Linux_OpenSSH远程连接
目录 目录 SSH server SSH服务属性 SSH协议执行原理 SSH连接方式 ssh Commands scp commands sftp commands SSH在生产环境中的使用 系统间备 ...
- http协议组成
摘要 本文主要内容介绍什么是http协议 了解http协议之前我们先了解一下当我们输入一个url到页面渲染出来,这个过程发什么了什么呢~ 网络开启线程开始解析url DNS解析域名,查询IP 建立TC ...
- DG on Windows 10 S: 执行任意代码
DG on Windows 10 S: 执行任意代码 windows 10 S版本是什么鬼? 众所周知,我们使用的是windows 10企业版 LTSC.更准确一点,CMD运行winver,我的版本是 ...
- 20191118 Spring Boot官方文档学习(4.8)
4.8. RSocket RSocket参考 RSocket是用于字节流传输的二进制协议.它通过单个连接传递的异步消息来启用对称交互模型. Spring框架的spring-messaging模块在客户 ...
- clearfix:after 的用法
想要清除浮动就要在父元素上 加上 clearfix:after .clearfix:after { <----在类名为“clearfix”的元素内最后面加入内容: content: " ...
- Linux 查看CPU 核数 还有 CPU 个数的命令
cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l 个数 cat /proc/cpuinfo | grep 'process' | ...
- Gym 101986D Making Perimeter of the Convex Hull Shortest(凸包+极角排序)
首先肯定是构造一个完整的凸包包括所有的点,那么要使得刚好有两个点在外面,满足这个条件的只有三种情况. 1.两个在凸包上但是不连续的两个点. 2.两个在凸包上但是连续的两个点. 3.一个在凸包上,还有一 ...
- laravel5.5结合bootstrap上传插件fileinput 上传图片
引入相关js <script src="{{ asset('bootstrap-fileinput/js/fileinput.js') }}"></script& ...
- PHP排序函数sort、rsort、asort、arsort、ksort、krsort
1.sort函数用于对数组元素值从低到高排序,去除原始索引元素,重新生成0,1,2..的键2.rsort函数用于对数组元素值从高到低排序,去除原始索引元素,重新生成0,1,2..的键3.asort函数 ...
- 小白学Python(19): Pyinstaller 生成 exe 文件
python 默认并不包含 PyInstaller 模块,因此需要自行安装 PyInstaller 模块. 安装 PyInstaller 模块与安装其他 Python 模块一样,使用 pip 命令安装 ...