From 2197f49403a7e0a2bdc5c9dedd0b6fc809fbff19 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" <huangge1199> Date: Sun, 26 Dec 2021 14:43:31 +0800 Subject: [PATCH] =?UTF-8?q?5964:=E6=89=A7=E8=A1=8C=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=90=8E=E7=BC=80=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...OfAllSuffixInstructionsStayingInAGrid.java | 121 ++++++++++++++++++ ...onOfAllSuffixInstructionsStayingInAGrid.md | 67 ++++++++++ 2 files changed, 188 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/ExecutionOfAllSuffixInstructionsStayingInAGrid.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/ExecutionOfAllSuffixInstructionsStayingInAGrid.md diff --git a/src/main/java/leetcode/editor/cn/ExecutionOfAllSuffixInstructionsStayingInAGrid.java b/src/main/java/leetcode/editor/cn/ExecutionOfAllSuffixInstructionsStayingInAGrid.java new file mode 100644 index 0000000..306c3ed --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ExecutionOfAllSuffixInstructionsStayingInAGrid.java @@ -0,0 +1,121 @@ +//现有一个 n x n 大小的网格,左上角单元格坐标 (0, 0) ,右下角单元格坐标 (n - 1, n - 1) 。给你整数 n 和一个整数数组 +//startPos ,其中 startPos = [startrow, startcol] 表示机器人最开始在坐标为 (startrow, startcol) 的单元格上 +//。 +// +// 另给你一个长度为 m 、下标从 0 开始的字符串 s ,其中 s[i] 是对机器人的第 i 条指令:'L'(向左移动),'R'(向右移动),'U'(向上移 +//动)和 'D'(向下移动)。 +// +// 机器人可以从 s 中的任一第 i 条指令开始执行。它将会逐条执行指令直到 s 的末尾,但在满足下述条件之一时,机器人将会停止: +// +// +// 下一条指令将会导致机器人移动到网格外。 +// 没有指令可以执行。 +// +// +// 返回一个长度为 m 的数组 answer ,其中 answer[i] 是机器人从第 i 条指令 开始 ,可以执行的 指令数目 。 +// +// +// +// 示例 1: +// +// +// +// +//输入:n = 3, startPos = [0,1], s = "RRDDLU" +//输出:[1,5,4,3,1,0] +//解释:机器人从 startPos 出发,并从第 i 条指令开始执行: +//- 0: "RRDDLU" 在移动到网格外之前,只能执行一条 "R" 指令。 +//- 1: "RDDLU" 可以执行全部五条指令,机器人仍在网格内,最终到达 (0, 0) 。 +//- 2: "DDLU" 可以执行全部四条指令,机器人仍在网格内,最终到达 (0, 0) 。 +//- 3: "DLU" 可以执行全部三条指令,机器人仍在网格内,最终到达 (0, 0) 。 +//- 4: "LU" 在移动到网格外之前,只能执行一条 "L" 指令。 +//- 5: "U" 如果向上移动,将会移动到网格外。 +// +// +// 示例 2: +// +// +// +// +//输入:n = 2, startPos = [1,1], s = "LURD" +//输出:[4,1,0,0] +//解释: +//- 0: "LURD" +//- 1: "URD" +//- 2: "RD" +//- 3: "D" +// +// +// 示例 3: +// +// +// +// +//输入:n = 1, startPos = [0,0], s = "LRUD" +//输出:[0,0,0,0] +//解释:无论机器人从哪条指令开始执行,都会移动到网格外。 +// +// +// +// +// 提示: +// +// +// m == s.length +// 1 <= n, m <= 500 +// startPos.length == 2 +// 0 <= startrow, startcol < n +// s 由 'L'、'R'、'U' 和 'D' 组成 +// +// 👍 1 👎 0 + +package leetcode.editor.cn; + +//5964:执行所有后缀指令 +class ExecutionOfAllSuffixInstructionsStayingInAGrid { + public static void main(String[] args) { + //测试代码 + Solution solution = new ExecutionOfAllSuffixInstructionsStayingInAGrid().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int[] executeInstructions(int n, int[] startPos, String s) { + int[] arrs = new int[s.length()]; + for (int i = 0; i < s.length(); i++) { + int x = startPos[0]; + int y = startPos[1]; + String str = s.substring(i); + for (int j = 0; j < str.length(); j++) { + char ch = str.charAt(j); + if (ch == 'R') { + y++; + if (y >= n) { + break; + } + } else if (ch == 'L') { + y--; + if (y < 0) { + break; + } + } else if (ch == 'U') { + x--; + if (x < 0) { + break; + } + } else { + x++; + if (x >= n) { + break; + } + } + arrs[i]++; + } + } + return arrs; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/ExecutionOfAllSuffixInstructionsStayingInAGrid.md b/src/main/java/leetcode/editor/cn/doc/content/ExecutionOfAllSuffixInstructionsStayingInAGrid.md new file mode 100644 index 0000000..416c1cb --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/ExecutionOfAllSuffixInstructionsStayingInAGrid.md @@ -0,0 +1,67 @@ +<p>现有一个 <code>n x n</code> 大小的网格,左上角单元格坐标 <code>(0, 0)</code> ,右下角单元格坐标 <code>(n - 1, n - 1)</code> 。给你整数 <code>n</code> 和一个整数数组 <code>startPos</code> ,其中 <code>startPos = [start<sub>row</sub>, start<sub>col</sub>]</code> 表示机器人最开始在坐标为 <code>(start<sub>row</sub>, start<sub>col</sub>)</code> 的单元格上。</p> + +<p>另给你一个长度为 <code>m</code> 、下标从 <strong>0</strong> 开始的字符串 <code>s</code> ,其中 <code>s[i]</code> 是对机器人的第 <code>i</code> 条指令:<code>'L'</code>(向左移动),<code>'R'</code>(向右移动),<code>'U'</code>(向上移动)和 <code>'D'</code>(向下移动)。</p> + +<p>机器人可以从 <code>s</code> 中的任一第 <code>i</code> 条指令开始执行。它将会逐条执行指令直到 <code>s</code> 的末尾,但在满足下述条件之一时,机器人将会停止:</p> + +<ul> + <li>下一条指令将会导致机器人移动到网格外。</li> + <li>没有指令可以执行。</li> +</ul> + +<p>返回一个长度为 <code>m</code> 的数组 <code>answer</code> ,其中 <code>answer[i]</code> 是机器人从第 <code>i</code> 条指令 <strong>开始</strong> ,可以执行的 <strong>指令数目</strong> 。</p> + +<p> </p> + +<p><strong>示例 1:</strong></p> + +<p><img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/1.png" style="width: 145px; height: 142px;" /></p> + +<pre> +<strong>输入:</strong>n = 3, startPos = [0,1], s = "RRDDLU" +<strong>输出:</strong>[1,5,4,3,1,0] +<strong>解释:</strong>机器人从 startPos 出发,并从第 i 条指令开始执行: +- 0: "<em><strong>R</strong></em>RDDLU" 在移动到网格外之前,只能执行一条 "R" 指令。 +- 1: "<em><strong>RDDLU</strong></em>" 可以执行全部五条指令,机器人仍在网格内,最终到达 (0, 0) 。 +- 2: "<em><strong>DDLU</strong></em>" 可以执行全部四条指令,机器人仍在网格内,最终到达 (0, 0) 。 +- 3: "<em><strong>DLU</strong></em>" 可以执行全部三条指令,机器人仍在网格内,最终到达 (0, 0) 。 +- 4: "<em><strong>L</strong></em>U" 在移动到网格外之前,只能执行一条 "L" 指令。 +- 5: "U" 如果向上移动,将会移动到网格外。 +</pre> + +<p><strong>示例 2:</strong></p> + +<p><img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/2.png" style="width: 106px; height: 103px;" /></p> + +<pre> +<strong>输入:</strong>n = 2, startPos = [1,1], s = "LURD" +<strong>输出:</strong>[4,1,0,0] +<strong>解释:</strong> +- 0: "<em><strong>LURD</strong></em>" +- 1: "<em><strong>U</strong></em>RD" +- 2: "RD" +- 3: "D" +</pre> + +<p><strong>示例 3:</strong></p> + +<p><img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/3.png" style="width: 67px; height: 64px;" /></p> + +<pre> +<strong>输入:</strong>n = 1, startPos = [0,0], s = "LRUD" +<strong>输出:</strong>[0,0,0,0] +<strong>解释:</strong>无论机器人从哪条指令开始执行,都会移动到网格外。 +</pre> + +<p> </p> + +<p><strong>提示:</strong></p> + +<ul> + <li><code>m == s.length</code></li> + <li><code>1 <= n, m <= 500</code></li> + <li><code>startPos.length == 2</code></li> + <li><code>0 <= start<sub>row</sub>, start<sub>col</sub> < n</code></li> + <li><code>s</code> 由 <code>'L'</code>、<code>'R'</code>、<code>'U'</code> 和 <code>'D'</code> 组成</li> +</ul> +<div><li>👍 1</li><li>👎 0</li></div> \ No newline at end of file