当前位置:网站首页>763. dividing alphabetic intervals
763. dividing alphabetic intervals
2022-06-26 03:45:00 【Mr Gao】
763. Division of alphabet
character string S It's made up of lowercase letters . We're going to divide this string into as many pieces as possible , The same letter can appear in at most one segment . Returns a list representing the length of each string fragment .
Example :
Input :S = “ababcbacadefegdehijhklij”
Output :[9,7,8]
explain :
The result is “ababcbaca”, “defegde”, “hijhklij”.
Each letter appears in at most one segment .
image “ababcbacadefegde”, “hijhklij” The division is wrong , Because the number of segments is small .
We can use two auxiliary arrays for this problem , It can solve the problem well :
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* partitionLabels(char * s, int* returnSize){
int r[26];
int r_t[26];
int i;
int *re=(int *)malloc(sizeof(int)*26);
for(i=0;i<26;i++){
r[i]=0;
r_t[i]=0;
}
i=0;
while(s[i]!='\0'){
r[s[i]-'a']++;
i++;
}
int sum=0;
int now=-1;
i=0;
int size=0;
while(s[i]!='\0'){
r_t[s[i]-'a']++;
sum++;
if( r_t[s[i]-'a']==r[s[i]-'a']){
sum=sum-r[s[i]-'a'];
if(sum==0){
re[size++]=i-now;
now=i;
}
}
i++;
}
*returnSize=size;
return re;
}
边栏推荐
- usb peripheral 驱动 - 枚举
- Evaluation - analytic hierarchy process
- Various errors in kitti2bag installation
- Uni app custom drop-down selection list
- "Renegotiation" agreement
- Classic model - Nin & googlenet
- Group counting notes - instruction pipeline of CPU
- MySQL addition, deletion, query and modification (primary level)
- The kotlin project is running normally and the R file cannot be found
- Kotlin learning apply plugin: 'kotlin Android extensions‘
猜你喜欢

String到底能不能改变?

Evaluation - analytic hierarchy process

小米电视的网页和珠宝的网页

阿里云函数计算服务一键搭建Z-Blog个人博客

Xiaomi TV's web page and jewelry's web page

Comparison of static methods and variables with instance methods and variables

【LOJ#6718】九个太阳「弱」化版(循环卷积,任意模数NTT)

点击事件

The kotlin project is running normally and the R file cannot be found

TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
随机推荐
ABP framework Practice Series (III) - domain layer in depth
进程之间的通信方式
Uni app, the text implementation expands and retracts the full text
Click event
After Ali failed to start his job in the interview, he was roast by the interviewer in the circle of friends (plug)
String到底能不能改变?
面试阿里测开岗失败后,被面试官在朋友圈吐槽了......(心塞)
解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题
2022.6.24-----leetcode.515
2022.6.25-----leetcode. Sword finger offer 091
816. 模糊坐标
(15)Blender源码分析之闪屏窗口显示菜单功能
个人用同花顺软件买股票安全吗?怎么炒股买股票呢
Communication mode between processes
Is it safe to open a fund account? How to apply
Uni app Baidu cloud realizes OCR ID card recognition
Gradient
Xiaomi TV's web page and jewelry's web page
C # knowledge structure
请求对象,发送请求