当前位置:网站首页>String notes
String notes
2022-04-23 05:55:00 【fattt_】
character string (String)
String related concepts
java:String Built in type , Reference data type , Non modifiable , If you want to change, consider StringBuffer,StringBuilder,char[]
C++:std::string Changeable , It can also be used. char
C: Only char[]
It should be noted that :
1、C++ in "+" Operator complexity undefined , But it is generally considered to be linear .
2、 stay C++ in std::string substr and java Medium String Of subString Different parameters .
3、 character in range :
C/C++[-128,127] It usually translates into undesigned[0,255]( Platform related )
java[0,65535]( Platform related )
Here are some examples of string related interview questions .
example 1,0-1 In exchange for
Put one 0-1 strand ( Contains only 0 and 1 String of ) Sort , You can exchange any two positions , Ask the minimum number of exchanges ?
analysis : Use fast platoon partition?? The leftmost 0 And the rightmost 1 Don't worry about it
such as 000…000001011…111111111
// Part of the code is as follows
int answer=0;
for(int i=0,j=len-1;i<j;++i,--j){
for(;(i<j)&&(a[i]=='0');++i);
for(;(j>i)&&(a[j]=='1');--j);
if(i<j) ++answer;
}
example 2, String replacement and copying
Delete a string and all a, And copy all b. notes : The character array is large enough ( Don't open up new space )
analysis : Delete first a, You can use the space of the original string
// Delete first a, You can use the space of the original string
int n=0,numb=0;
for(int i=0; s[i]; i++){
if(s[i] != 'a'){
s[n++] == s[i];}
if(s[i] == 'b'){
++numb;}
}
s[n]=0;
// Copy again b, Note that the string should be lengthened
int newLength = n+numb;
s[newLength]=0;
for(int i=newLength-1,j=n-1;j>=0;--j){
// Here we use the reverse copy method , The original information will not be overwritten , Keep the original information unchanged
s[i--]=s[j];
if(s[j]=='b') s[i--]='b';
}
This question adopts a conventional technique **“ Copy backwards ”**(memery copy), It can ensure that the original information will not be overwritten .
example 3、 Exchange asterisks
A string contains only * Number and number , Please put all its asterisks at the beginning .
analysis :
Method 1: Quick line up partition— The relative order of numbers will change
Loop invariants :[0,i-1] All are asterisk ,[i,j-1] It's all numbers ,[j,n-1] Not detected .
for(int i=0,j=0;j<n;++j){
if(s[j]=='*') swap(s[i++],s[j]);
}
Method 2: use “ Copy backwards ”, The relative order of numbers remains unchanged
int j=n-1;
for(int i=n-1;i>=0;--i){
if(isdigit(s[i])) {
s[j--]=s[i];// If i It's the number. , Then copy to j; If i yes * Number , Then skip
}
for(;j>=0;--j){
s[j]='*';// When i=-1 when , It means that the number has been copied , Then the front j All become *
}
}
example 4、 Substring modifier
Given two strings a and b, ask b Whether it is a An anamorphic word of a substring of . For example, the input a=hello,b=lel,lle,ello All are true, however b=elo yes false.
analysis : Slide window idea
The typical sliding window idea of this problem , Dynamically maintain a window . such as b Is the length of the 3, Then we need to investigate a[0,2],[1,3],[2,4] Whether and b It's an anamorphic word . So how to talk to b Comparison? ?
We use one hash, Based on the particularity of string , It can be used [0,255] perhaps [0,65535] Array of , For the time being, they are all lowercase English letters ,[0,25] To express b The number of occurrences of each word in .
We can save how many non 0 time , It will be useful in the future .
int nonZero = 0;
for(int i=0; i<lenb; i++){
if(++num[b[i]-'a']==1){
//b[i]-'a' Is to put b The inside number i Bit to 0-25 A number in the middle
++nonZero;//nonZero It's statistics num How many numbers in the array are right and wrong 0 Of
}
}
We use it b Subtract... From the number of times in a The type of character in a window in the , If the result is all 0, Then find such a substring .
Be careful :num[] The meaning of becomes the difference of character type
// The first window [0,lenb-1]( Be careful :lena<lenb unsolvable )
for(int i=0; i<lenb; +++i){
int c = a[i] - 'a';
--num[c];
if(num[c] == 0){
--nonZero;
}else if(num[c] == -1){
++nonZero;
}
}
if(nonZero == 0) return true;
How the window moves ? Move one bit to the right
New window a[i-lenb+1, i]
Old window a[i-lenb,i-1]
Throw away a[i-lenb], Join in a[i]
for(int i=lenb; i<lena; i++){
int c=a[i-lenb] - 'a';
++num[c];
if(num[c]==1) ++nonZero;
else if(num[c]==0) --nonZero;
c=a[i]-'a';
--num[c];
if(num[c]==0) --nonZero;
else if(num[c]==-1) ++nonZero;
if(nonZero==0) return true;
}
版权声明
本文为[fattt_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541585990.html
边栏推荐
- SQL注入
- 如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
- 在Jupyter notebook中用matplotlib.pyplot出现服务器挂掉、崩溃的问题
- 基于thymeleaf实现数据库图片展示到浏览器表格
- Development environment EAS login license modification
- opensips(1)——安装opensips详细流程
- MySql基础狂神说
- 域内用户访问域外samba服务器用户名密码错误
- io.lettuce.core.RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
- umi官网yarn create @umijs/umi-app 报错:文件名、目录名或卷标语法不正确
猜你喜欢
图解numpy数组矩阵
PyQy5学习(三):QLineEdit+QTextEdit
框架解析1.系统架构简介
Graphic numpy array matrix
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
PreparedStatement防止SQL注入
实操—Nacos安装与配置
Pytorch learning record (XII): learning rate attenuation + regularization
创建二叉树
Pytorch学习记录(五):反向传播+基于梯度的优化器(SGD,Adagrad,RMSporp,Adam)
随机推荐
redhat实现目录下特定文本类型内关键字查找及vim模式下关键字查找
Rsync for file server backup
Latex快速入门
excel获取两列数据的差异数据
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
EditorConfig
MySQL triggers, stored procedures, stored functions
Solve the error: importerror: iprogress not found Please update jupyter and ipywidgets
What is JSON? First acquaintance with JSON
去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
Shansi Valley P290 polymorphism exercise
Pytorch Learning record (XIII): Recurrent Neural Network
异常的处理:抓抛模型
图解numpy数组矩阵
interviewter:介绍一下MySQL日期函数
JDBC操作事务
Multithreading and high concurrency (3) -- synchronized principle
SQL基础:初识数据库与SQL-安装与基本介绍等—阿里云天池
深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索