当前位置:网站首页>880. decoded string at index
880. decoded string at index
2022-06-30 06:04:00 【Mr Gao】
880. Decoded string at index
Given an encoding string S. Please find out Decoding strings And write it to tape . When decoding , From the encoding string Read one character at a time , And take the following steps :
If the characters read are letters , Then write the letter on the tape .
If the characters read are numbers ( for example d), The entire current tape will be rewritten in total d-1 Time .
Now? , For a given encoding string S And index K, Find and return the... In decoding string K Letters .
Example 1:
Input :S = “leet2code3”, K = 10
Output :“o”
explain :
The decoded string is “leetleetcodeleetleetcodeleetleetcode”.
The... In the string 10 One letter is “o”.
Example 2:
Input :S = “ha22”, K = 5
Output :“h”
explain :
The decoded string is “hahahaha”. The first 5 One letter is “h”.
Example 3:
Input :S = “a2345678999999999999999”, K = 1
Output :“a”
explain :
The decoded string is “a” repeat 8301530446056247680 Time . The first 1 One letter is “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';
}
There is also a powerful algorithm , It's great to start from the back :
// Reverse processing
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. Calculate the length of the expanded string
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. From post-processing
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;
}
边栏推荐
猜你喜欢

Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?

Inno setup the simplest user-defined interface effect

UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)

Golang's handwritten Web Framework

About modifying dual system default startup item settings

After getting these performance test decomposition operations, your test path will be more smooth

Strlen and sizeof, array length and string length, don't be silly
一个完整的性能测试流程

Projet Web de déploiement du serveur Cloud

CompletionService使用及原理(源码分析)
随机推荐
requests. The difference between session () sending requests and using requests to send requests directly
C language code record
超简单 STM32 RTC闹钟 时钟配置
1380. lucky numbers in matrices
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
[GPU] basic operation
旋转标注工具roLabelImg
MySQL事物
583. 两个字符串的删除操作-动态规划
[ansible series] fundamentals -01
重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
Tornado frame foundation
MySQL index
Intelligent deodorizer embedded development
[GPU] basic operation of GPU (I)
MySQL transaction
[road of system analyst] collection of wrong topics in Project Management Chapter
ES6数组遍历与ES5数组遍历