880:索引处的解码字符串(未完成)

This commit is contained in:
huangge1199@hotmail.com 2021-04-25 23:52:44 +08:00
parent dbf69d5616
commit e55b6f047b
2 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,95 @@
//给定一个编码字符串 S请你找出 解码字符串 并将其写入磁带解码时从编码字符串中 每次读取一个字符 并采取以下步骤
//
//
// 如果所读的字符是字母则将该字母写在磁带上
// 如果所读的字符是数字例如 d则整个当前磁带总共会被重复写 d-1
//
//
// 现在对于给定的编码字符串 S 和索引 K查找并返回解码字符串中的第 K 个字母
//
//
//
// 示例 1
//
// 输入S = "leet2code3", K = 10
//输出"o"
//解释
//解码后的字符串为 "leetleetcodeleetleetcodeleetleetcode"
//字符串中的第 10 个字母是 "o"
//
//
// 示例 2
//
// 输入S = "ha22", K = 5
//输出"h"
//解释
//解码后的字符串为 "hahahaha" 5 个字母是 "h"
//
//
// 示例 3
//
// 输入S = "a2345678999999999999999", K = 1
//输出"a"
//解释
//解码后的字符串为 "a" 重复 8301530446056247680 1 个字母是 "a"
//
//
//
//
// 提示
//
//
// 2 <= S.length <= 100
// S 只包含小写字母与数字 2 9
// S 以字母开头
// 1 <= K <= 10^9
// 题目保证 K 小于或等于解码字符串的长度
// 解码后的字符串保证少于 2^63 个字母
//
// Related Topics
// 👍 135 👎 0
package leetcode.editor.cn;
//880:索引处的解码字符串
public class DecodedStringAtIndex {
public static void main(String[] args) {
//测试代码
Solution solution = new DecodedStringAtIndex().new Solution();
//o
System.out.println(solution.decodeAtIndex("leet2code3", 10));
//h
System.out.println(solution.decodeAtIndex("ha22", 5));
//a
System.out.println(solution.decodeAtIndex("a2345678999999999999999", 1));
//内存溢出
System.out.println(solution.decodeAtIndex("y959q969u3hb22odq595", 222280369));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String decodeAtIndex(String S, int K) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < S.length(); i++) {
if (Character.isDigit(S.charAt(i))) {
String temp = str.toString();
for (int j = 0; j < S.charAt(i) - '0' - 1; j++) {
str.append(temp);
if (str.length() >= K) {
return "" + str.charAt(K - 1);
}
}
} else {
str.append(S.charAt(i));
if (str.length() >= K) {
break;
}
}
}
return "" + str.charAt(K - 1);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,49 @@
<p>给定一个编码字符串 <code>S</code>。请你找出<em> </em><strong>解码字符串</strong> 并将其写入磁带。解码时,从编码字符串中<strong> 每次读取一个字符 </strong>,并采取以下步骤:</p>
<ul>
<li>如果所读的字符是字母,则将该字母写在磁带上。</li>
<li>如果所读的字符是数字(例如 <code>d</code>),则整个当前磁带总共会被重复写&nbsp;<code>d-1</code> 次。</li>
</ul>
<p>现在,对于给定的编码字符串 <code>S</code> 和索引 <code>K</code>,查找并返回解码字符串中的第&nbsp;<code>K</code>&nbsp;个字母。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>S = &quot;leet2code3&quot;, K = 10
<strong>输出:</strong>&quot;o&quot;
<strong>解释:</strong>
解码后的字符串为 &quot;leetleetcodeleetleetcodeleetleetcode&quot;
字符串中的第 10 个字母是 &quot;o&quot;
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>S = &quot;ha22&quot;, K = 5
<strong>输出:</strong>&quot;h&quot;
<strong>解释:</strong>
解码后的字符串为 &quot;hahahaha&quot;。第 5 个字母是 &quot;h&quot;
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>S = &quot;a2345678999999999999999&quot;, K = 1
<strong>输出:</strong>&quot;a&quot;
<strong>解释:</strong>
解码后的字符串为 &quot;a&quot; 重复 8301530446056247680 次。第 1 个字母是 &quot;a&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= S.length &lt;= 100</code></li>
<li><code>S</code>&nbsp;只包含小写字母与数字 <code>2</code><code>9</code></li>
<li><code>S</code>&nbsp;以字母开头。</li>
<li><code>1 &lt;= K &lt;= 10^9</code></li>
<li>题目保证 <code>K</code> 小于或等于解码字符串的长度。</li>
<li>解码后的字符串保证少于&nbsp;<code>2^63</code>&nbsp;个字母。</li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 135</li><li>👎 0</li></div>