当前位置:网站首页>880. 索引处的解码字符串
880. 索引处的解码字符串
2022-06-30 05:59:00 【Mr Gao】
880. 索引处的解码字符串
给定一个编码字符串 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”。
int re;
int kt;
void f(char * s,int end,int epo){
while(epo&&kt!=0){
int j=0;
while(j!=end&&kt!=0){
if(s[j]>='a'&&s[j]<='z'){
kt--;
//printf("--%c %d ",s[j],kt);
if(kt==0){
s[j+1]='\0';
re=j;
}
}
else{
f(s,j,s[j]-'0'-1);
}
j++;
}
epo--;
}
}
char * decodeAtIndex(char * s, int k){
int i=0;
kt=k;
re=0;
f(s,strlen(s),1);
return s+re;
// while(s[i]!='\0'){
// if(s[i]>='a'&&s[i]<='z'){
// k--;
// printf("--%c %d",s[i],k);
// if(k==0){
// s[i+1]='\0';
// return s+i;
// }
// }
// else{
// int epo=s[i]-'0'-1;
// int end=i;
// while(epo){
// int j=0;
// while(j!=i){
// if(s[j]>='a'&&s[j]<='z'){
// k--;
// printf("%c %d ",s[j],k,j);
// if(k==0){
// s[j+1]='\0';
// return s+j;
// }
// }
// j++;
// }
// epo--;
// }
// }
// i++;
// }
return 'a';
}
这还有一种比较厉害的算法,从后面开始计算非常棒:
// 逆向处理
char * decodeAtIndex(char * S, int K)
{
if (S == NULL || strlen(S) == 0 || K < 1) {
return NULL;
}
char* result = (char*)malloc(2 * sizeof(char));
if (result != NULL) {
memset(result, 0, 2 * sizeof(char));
}
int len = strlen(S);
long strSize = 0;
// 1. 计算展开后字符串长度
for (int i = 0; i < len; i++) {
char c = S[i];
if (isalpha(c)) {
strSize++;
continue;
}
if (isdigit(c)) {
strSize = strSize * (c - '0');
}
}
if (K > strSize) {
return NULL;
}
// 2. 从后处理
for (int i = len - 1; i >= 0; i--) {
K = K % strSize;
if (K == 0 && isalpha(S[i])) {
result[0] = S[i];
result[1] = '\0';
return result;
}
if (isdigit(S[i])) {
strSize /= S[i] - '0';
} else {
strSize--;
}
}
return NULL;
}
边栏推荐
- Leetcode56. consolidation interval
- Tornado frame foundation
- Title: enter two positive integers m and N to find their maximum common divisor and minimum common multiple
- 领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
- Attempt to redefine 'timeout' at line 2 solution
- How to automatically renew a token after it expires?
- hashlips_ art_ Engine-1.0.6 usage
- Cisco VXLAN配置
- 重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
- Idea of capturing mobile terminal variant combination
猜你喜欢
CompletableFuture从了解到精通,你想知道的这里都有
[deep learning] data segmentation
MySQL advanced (Advanced SQL statement)
接口中方法详解
UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
Summary of redis learning notes (I)
[road of system analyst] collection of wrong topics in Project Management Chapter
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
Solitidy - fallback 回退函数 - 2种触发执行方式
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
随机推荐
Qt之QListView的简单使用(含源码+注释)
Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
Tornado frame foundation
What indicators should safety service engineers pay attention to in emergency response?
数据读写:Unity中基于C#脚本实现数据读写功能
8 ways to earn passive income
Codeforces Round #390 (Div. 2) D. Fedor and coupons
Intelligent deodorizer embedded development
STM32F103系列控制的OLED IIC 4针
Basic operations of C language
SSL证书续费相关问题详解
Who is promoting the new inflection point of audio and video industry in 2022?
SHELL
1380. lucky numbers in matrices
MySQL index
Inno setup the simplest user-defined interface effect
MySQL數據庫用戶管理
[ansible series] fundamentals 02 module debug
从零开发 stylelint规则(插件)
token 过期后,如何自动续期?