当前位置:网站首页>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;
}
边栏推荐
- Inno setup the simplest user-defined interface effect
- Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
- Strlen and sizeof, array length and string length, don't be silly
- 反編譯正常回編譯出現問題自己解决辦法
- Common address collection
- 10-【istio】istio Sidecar
- ES6扩展运算符(...)
- 46. 全排列-dfs双百代码
- Leetcode search insert location
- [regular expression series] greedy and non greedy patterns
猜你喜欢

Do you know how to show the health code in only 2 steps

MySQL日志管理、数据备份、恢复

Cisco VXLAN配置

MySQL數據庫用戶管理

JS prototype chain object function relationship

MySQL高级SQL语句

About modifying dual system default startup item settings

What indicators should safety service engineers pay attention to in emergency response?

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

Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"
随机推荐
Inno setup the simplest user-defined interface effect
CompletableFuture从了解到精通,你想知道的这里都有
[ansible series] fundamentals -01
Summary of 2 billion redis data migration
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
How to print pthread_ t - How to print pthread_ t
雲服務器部署 Web 項目
反編譯正常回編譯出現問題自己解决辦法
RSA and AES
Cisco VXLAN配置
C language code record
MySQL transaction
Vscode configuration proxy
Detailed explanation of issues related to SSL certificate renewal
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
【学习强化学习】总目录
ES6扩展运算符(...)
Develop stylelint rules from zero (plug-ins)