题一:
难度简单260收藏分享切换为英文接收动态反馈
实现一个算法,确定一个字符串 s
的所有字符是否全都不同。
示例 1:
输入: s
= "leetcode"
输出: false
示例 2:
输入: s
= "abc"
输出: true
限制:
0 <= len(s) <= 100
s[i]
仅包含小写字母- 如果你不使用额外的数据结构,会很加分。
bool isUnique(char* astr){
int i=0;
int j=0;
for(i=0;i<strlen(astr);i++){
for(j=i+1;j<strlen(astr);j++){
if(astr[i]==astr[j]){
return false;
}
}
}
return true;
}
题二:
难度简单96收藏分享切换为英文接收动态反馈
URL化。编写一种方法,将字符串中的空格全部替换为%20
。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的“真实”长度。(注:用Java
实现的话,请使用字符数组实现,以便直接在数组上操作。)
示例 1:
输入:"Mr John Smith ", 13 输出:"Mr%20John%20Smith"
示例 2:
输入:" ", 5 输出:"%20%20%20%20%20"
提示:
- 字符串长度在 [0, 500000] 范围内。
char* replaceSpaces(char* S, int length){
char *ans = malloc(sizeof(char)*(length*3 + 1));
int n = 0;
while(length--){
if(*S==' '){
ans[n++] = '%';
ans[n++] = '2';
ans[n++] = '0';
}else{
ans[n++] = *S;
}
S++;
}
ans[n] = 0;
return ans;
}
题三 :
难度简单148收藏分享切换为英文接收动态反馈
给定两个由小写字母组成的字符串 s1
和 s2
,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入:s1
= "abc",s2
= "bca" 输出: true
示例 2:
输入:s1
= "abc",s2
= "bad" 输出: false
说明:
0 <= len(s1) <= 100
0 <= len(s2) <= 100
bool CheckPermutation(char* s1, char* s2){
int count=0;
int s=strlen(s1);
for(int i=0;i<s;i++){
for(int j=0;j<s;j++){
if(s1[i]==s2[j]){
count++;
}
}
}
if(count==s){
return true;
}
else{
return false;
}
}