当前位置:网站首页>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;
}
边栏推荐
- HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct
- kitti2bag 安装出现的各种错误
- Route jump: click the operation button of the list to jump to another menu page and activate the corresponding menu
- 使用SOAPUI访问对应的esb工程
- Contains an object field at offset position
- Insect structure and Deconstruction
- [paper notes] learning to grasp with primitive shaped object policies
- 个人用同花顺软件买股票安全吗?怎么炒股买股票呢
- Uni app Baidu cloud realizes OCR ID card recognition
- Classic model – RESNET
猜你喜欢

Nepal graph learning Chapter 3_ Multithreading completes 6000w+ relational data migration

Restful API interface design standards and specifications

Uni app custom navigation bar component

栖霞消防开展在建工地消防安全培训

ABP framework Practice Series (I) - Introduction to persistence layer

Click event

Upload file / text / picture, box shadow

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

Qt 中 deleteLater 使用总结

请求对象,发送请求
随机推荐
分割、柱子、list
Navicat16 wireless trial
Drag and drop
MySQL高级篇第一章(linux下安装MySQL)【下】
Add an "open search description" to the site to adapt to the browser's "site search"“
Request object, send request
开源!ViTAE模型再刷世界第一:COCO人体姿态估计新模型取得最高精度81.1AP
指南针app是正规的吗?到底安不安全
Open Camera异常分析(一)
智能制造学习记录片和书籍
MySQL advanced Chapter 1 (installing MySQL under Linux) [2]
kitti2bag 安装出现的各种错误
Classic model - Nin & googlenet
(15)Blender源码分析之闪屏窗口显示菜单功能
Some mobile phones open USB debugging, and the solution to installation failure
Multimedia elements, audio, video
[paper notes] learning to grasp with primitive shaped object policies
What does virtualization mean? What technologies are included? What is the difference with private cloud?
2020 summary: industrial software development under Internet thinking
Evaluation - analytic hierarchy process