题目描述 LeetCode 811
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.
Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".
We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.
Example 1:
Input:
["9001 discuss.leetcode.com"]
Output:
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
Explanation:
We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.
Example 2:
Input:
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output:
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
Explanation:
We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
Notes:
- The length of cpdomains will not exceed 100.
- The length of each domain name will not exceed 100.
- Each address will have either 1 or 2 "." characters.
- The input count in any count-paired domain will not exceed 10000.
- The answer output can be returned in any order.
中文描述
见例子就好
解题思路
这道题考察的特别扎实,看代码吧
C语言代码
这道题目很有挑战力,所以为了更好地写好这道题目,更好地反思回顾,我会讲的很细,简而言之分模块。听说集齐七颗龙珠(模块)就能召唤神龙(最终程序),现在就来随我集龙珠吧!!!
- 1,连接数字和字符串的 C 语言代码,也就是说,把数字和字符串拼接起来,再放到字符串中。
# include <stdio.h>
# include <string.h>
void xx(char temp[], int x)
{
int i = 0;
int length = 0;
char tmp;
while(x != 0)
{
temp[length ++ ] = x%10 + '0';
x = x/10;
}
// 反转 ,当执行while之后,temp数组中存放,1009,反转之后存放 9001
for (i = 0; i < length/2; i ++)
{
tmp = temp[i];
temp[i] = temp[length - i - 1];
temp[length -i - 1] = tmp;
}
temp[length ++] = ' ';
temp[length] = '\0';
}
main()
{
int a = 9001;
char b[100] = "discuss.leetcode.com";
char temp[100];
xx(temp, a); // 将数字 a = 9001 转化为单个字符,存放到 temp 字符串数组中
strcat(temp, b); // 拼接数字字符串 和 字符串
printf("%s\n\n", temp);
printf("%c ", 8 + '0'); // 将数字转化为字符
printf("%d ", '8' - '0'); // 将字符转化为数字
printf("\n\n");
}
- 2,实现 Example 1 之不完全版
# include <stdio.h>
# include <string.h>
int dd(int size)
{
int i = 0;
int sum = 1;
for ( i = 0; i < size; i ++)
{
sum *= 10;
}
return sum;
}
int charinverseint(char number[])
{
int size = strlen(number);
int i;
int sum = 0;
for (i = 0; i < size; i ++)
{
sum += (number[i] - '0') * dd(size - i - 1);
}
return sum;
}
void subdomainVisits(char** cpdomains, int cpdomainsSize, int* returnSize)
{
int i, j;
int len = 0;
int flag = 0;
char domains[100][100];
int domains_i = 0 , domains_j = 0;
char str_int[20];
len = strlen( * ( cpdomains ) );
for ( i = 0; i < len; i ++ )
{
domains_j = 0;
if( * ( * (cpdomains ) + i) == ' ' || * ( * (cpdomains ) + i ) == '.')
{
flag ++;
for (j = i + 1; j < len; j ++)
{
domains[domains_i][domains_j ++ ] = * ( * (cpdomains ) + j) ;
}
domains[domains_i][domains_j] = '\0';
domains_i ++;
}
if (flag == 0)
{
str_int[i] = * ( * (cpdomains ) + i);
}
if (flag == 1)
{
str_int[i] = '\0';
}
}
for (i = 0; i < domains_i; i++)
{
printf("%d %s\n", charinverseint(str_int), domains[i]);
}
}
main()
{
char *words[4] = {"9001 discuss.leetcode.com"};
int cpdomainsSize = 1;
int returnSize = 1;
subdomainVisits(words, cpdomainsSize, &returnSize);
}
- 3,二重指针的返回
# include <stdio.h>
char** vv()
{
static char *word[4];
char** p;
char temp[100][100] = {"abc", "fff", "hhh"};
p = word;
*(p) = temp[0];
*(p + 1) = temp[1];
*(p + 2) = temp[2];
return p;
}
main()
{
char** y;
int i;
y = vv();
for (i = 0; i < 3; i ++)
{
printf("%s\n", *(y + i));
}
}
- 4,完整实现 Example 1
# include <stdio.h>
# include <string.h>
void numberversechar(char temp[], int x)
{
int i = 0;
int length = 0;
char tmp;
while(x != 0)
{
temp[length ++ ] = x%10 + '0';
x = x/10;
}
// 反转
for (i = 0; i < length/2; i ++)
{
tmp = temp[i];
temp[i] = temp[length - i - 1];
temp[length -i - 1] = tmp;
}
temp[length ++] = ' ';
temp[length] = '\0';
}
int dd(int size)
{
int i = 0;
int sum = 1;
for ( i = 0; i < size; i ++)
{
sum *= 10;
}
return sum;
}
int charinverseint(char number[])
{
int size = strlen(number);
int i;
int sum = 0;
for (i = 0; i < size; i ++)
{
sum += (number[i] - '0') * dd(size - i - 1);
}
return sum;
}
char** subdomainVisits(char** cpdomains, int cpdomainsSize, int* returnSize)
{
int i, j;
int len = 0;
int flag = 0;
char domains[100][100];
int domains_i = 0 , domains_j = 0;
char str_int[20];
char temp[100][100];
static char *word[100];
char** ddomains;
ddomains = word;
len = strlen( * ( cpdomains ) );
for ( i = 0; i < len; i ++ )
{
domains_j = 0;
if( * ( * (cpdomains ) + i) == ' ' || * ( * (cpdomains ) + i ) == '.')
{
flag ++;
for (j = i + 1; j < len; j ++)
{
domains[domains_i][domains_j ++ ] = * ( * (cpdomains ) + j) ;
}
domains[domains_i][domains_j] = '\0';
domains_i ++;
}
if (flag == 0)
{
str_int[i] = * ( * (cpdomains ) + i);
}
if (flag == 1)
{
str_int[i] = '\0';
}
}
/* for (i = 0; i < domains_i; i++)
{
printf("%d %s\n", charinverseint(str_int), domains[i]);
}*/
for (i = 0; i < domains_i; i ++)
{
numberversechar(temp[i], charinverseint(str_int));
strcat(temp[i], domains[i]);
* ( ddomains + i ) = temp[i];
//printf("1 %s\n", temp[i]);
//printf("%s\n", *(ddomains + i));
}
*returnSize = domains_i;
return ddomains;
}
main()
{
char *words[4] = {"9001 discuss.leetcode.com"};
int cpdomainsSize = 1;
int returnSize = 1;
char** doo;
int i;
doo = subdomainVisits(words, cpdomainsSize, &returnSize);
printf("returnSize = %d\n", returnSize);
for (i = 0; i < returnSize; i ++)
{
printf("%s\n", *(doo + i));
}
}
- 5,完整实现
# include<stdio.h>
# include<string.h>
void numberversechar(char temp[], int x)
{
int i = 0;
int length = 0;
char tmp;
while(x != 0)
{
temp[length ++ ] = x%10 + '0';
x = x/10;
}
// 反转
for (i = 0; i < length/2; i ++)
{
tmp = temp[i];
temp[i] = temp[length - i - 1];
temp[length -i - 1] = tmp;
}
temp[length ++] = ' ';
temp[length] = '\0';
}
int dd(int size)
{
int i = 0;
int sum = 1;
for ( i = 0; i < size; i ++)
{
sum *= 10;
}
return sum;
}
int charinverseint(char number[])
{
int size = strlen(number);
int i;
int sum = 0;
for (i = 0; i < size; i ++)
{
sum += (number[i] - '0') * dd(size - i - 1);
}
return sum;
}
char** subdomainVisits(char** cpdomains, int cpdomainsSize, int* returnSize)
{
int i, j , k;
/* yanzhengchuanguolaide word
for(i = 0; i < cpdomainsSize; i ++)
{
printf("%s\n", *(cpdomains + i));
}*/
int len;
int flag = 0;
int oo = 0;
char domains[10000][100];
int domains_i = 0, domains_j = 0;
int number[10000];
char str[100] = "";
char str_domains[10000][100];
static char *word[10000];
char **ddomains;
ddomains = word;
for (i = 0; i < cpdomainsSize; i ++)
{
len = strlen(* ( cpdomains + i ));
flag = 0;
for(j = 0; j < len; j ++)
{
domains_j = 0;
if( * ( * (cpdomains + i) + j) == ' ' || * ( * (cpdomains + i) + j) == '.')
{
flag ++;
for(k = j + 1; k < len; k ++)
{
domains[domains_i][domains_j ++ ] = * ( * (cpdomains + i) + k) ;
}
domains[domains_i][domains_j] = '\0';
domains_i ++;
if (flag == 1)
{
str[j] = '\0';
//printf("%s\n", str);
}
if(* ( * (cpdomains + i) + j) == '.')
{
number[oo ++ ] = charinverseint(str);
//printf("%d, %d, %s\n", oo - 1, number[oo - 1], str);
}
}
if (flag == 0)
{
str[j] = * ( * (cpdomains + i) + j);
}
}
number[oo ++] = charinverseint(str);
//printf("%d, %d, %s\n", oo - 1, number[oo - 1], str);
}
// 验证一下
/* for (i = 0; i < domains_i; i ++)
{
printf("%d, %s\n", number[i], domains[i]);
}*/
for (i = 0; i < domains_i - 1; i ++)
{
if(number[i] != 0)
{
for (j = i + 1; j < domains_i; j ++)
{
if(strcmp(domains[i], domains[j]) == 0)
{
number[i] += number[j];
number[j] = 0;
}
}
}
}
j = 0;
for (i = 0; i < domains_i; i ++)
{
if(number[i] != 0)
{
numberversechar(str_domains[i], number[i]);
strcat(str_domains[i], domains[i]);
* ( ddomains + j ) = str_domains[i];
j ++;
}
}
*returnSize = j;
return ddomains;
}
main()
{
char *words[100] = {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
int cpdomainsSize = 4;
int returnSize = 0;
char** doo;
int i;
doo = subdomainVisits(words, cpdomainsSize, &returnSize);
printf("returnSize = %d\n", returnSize);
for (i = 0; i < returnSize; i ++)
{
printf("%s\n", *(doo + i));
}
}
思考
- 这道题目难吗? 貌似不难,简单的字符串处理一下,考察数字转化字符串,字符串转化数字,二重指针,字符串拼接,不难但是折磨人,断断续续做了一周,好烦。
- 建议特别有兴趣就做,没有就放弃这个吧