当前位置:网站首页>763. 划分字母区间
763. 划分字母区间
2022-06-26 03:20:00 【Mr Gao】
763. 划分字母区间
字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。
示例:
输入:S = “ababcbacadefegdehijhklij”
输出:[9,7,8]
解释:
划分结果为 “ababcbaca”, “defegde”, “hijhklij”。
每个字母最多出现在一个片段中。
像 “ababcbacadefegde”, “hijhklij” 的划分是错误的,因为划分的片段数较少。
这题我们可以使用两个辅助数组,可以很好的解决问题:
/** * 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;
}
边栏推荐
- MySQL advanced part (IV: locking mechanism and SQL optimization)
- Xgboost, lightgbm, catboost -- try to stand on the shoulders of giants
- MySQL development environment
- 优化——多目标规划
- ASP. Net startup and running mechanism
- js实现文字跑马灯效果
- Camera-memory内存泄漏分析(二)
- Uni app, the text implementation expands and retracts the full text
- Android gap animation translate, scale, alpha, rotate
- String到底能不能改变?
猜你喜欢

Uni app custom drop-down selection list

progress bar

Open Camera异常分析(一)

Slide the menu of uni app custom components left and right and click switch to select and display in the middle

ABP framework

进度条

MySQL开发环境

Group note data representation and operation check code

Digital twin intelligent water service, breaking through the development dilemma of sponge City

You cannot call Glide. get() in registerComponents(), use the provided Glide instance instead
随机推荐
Redux thunk simple case, advantages, disadvantages and thinking
MySQL addition, deletion, query and modification (Advanced)
Uni app QR code scanning and identification function
Is Guoxin golden sun reliable? Is it safe to open a securities account?
Preparation for wechat applet development
XGBoost, lightGBM, CatBoost——尝试站在巨人的肩膀上
Nebula Graph学习篇3_多线程完成6000w+关系数据迁移
Click event
Add an "open search description" to the site to adapt to the browser's "site search"“
Procédures stockées MySQL
Evaluation - analytic hierarchy process
Uni app custom drop-down selection list
What does virtualization mean? What technologies are included? What is the difference with private cloud?
mysql 常用语句
“再谈”协议
Mysql database foundation
Group note data representation and operation check code
Solve the problem that the uniapp plug-in Robin editor reports an error when setting the font color and background color
Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct