Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit97ff3af

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent6338108 commit97ff3af

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O1 -otest anagrams_in_string.c
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
5+
/**
6+
* Note: The returned array must be malloced, assume caller calls free().
7+
*/
8+
int*findAnagrams(char*s,char*p,int*returnSize){
9+
*returnSize=0;
10+
int*res=malloc(11000*sizeof(int));
11+
inti,pat_len=0;
12+
intcount[128]= {0 };
13+
for (i=0;p[i]!='\0';i++) {
14+
count[p[i]]++;
15+
}
16+
pat_len=i;
17+
18+
intl=0,r=0,len=0;
19+
while (s[r]!='\0') {
20+
if (--count[s[r++]] >=0) {
21+
len++;
22+
}
23+
if (r-l >=pat_len) {
24+
if (len==pat_len) {
25+
res[(*returnSize)++]=l;
26+
}
27+
if (++count[s[l++]]>0) {
28+
len--;
29+
}
30+
}
31+
}
32+
33+
returnres;
34+
}
35+
36+
intmain(intargc,char**argv)
37+
{
38+
if (argc!=3) {
39+
fprintf(stderr,"Usage: ./test string pattern\n");
40+
exit(-1);
41+
}
42+
43+
char*t=argv[1];
44+
char*s=argv[2];
45+
inti,count;
46+
int*results=findAnagrams(s,t,&count);
47+
for (i=0;i<count;i++) {
48+
printf("%d ",results[i]);
49+
}
50+
printf("\n");
51+
52+
return0;
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp