码迷,mamicode.com
首页 > 其他好文 > 详细

hackrank Sorting Array of Strings

时间:2019-01-06 20:48:08      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:rap   strcmp   class   www.   hacker   string   hal   -o   http   

https://www.hackerrank.com/challenges/sorting-array-of-strings/problem

 1 int flag = 1;
 2 int distinct_characters(const char* a);
 3 
 4 int lexicographic_sort(const char* a, const char* b) {
 5 
 6 if(strcmp(a, b) > 0){
 7     flag = 0;
 8 }else flag = 1;
 9   
10   return flag;
11 }
12 
13 int lexicographic_sort_reverse(const char* a, const char* b) {
14   if(strcmp(a, b) < 0){
15       flag = 0;
16   }else flag = 1;
17   return flag;
18 }
19 
20 int distinct_characters(const char* a){
21     int dis_char = strlen(a);
22     int len = strlen(a);
23     for(int i = 0; i < len; i ++){
24         if(a[i] != \0){
25             for(int j = i + 1; j < len; j ++){
26                 if(a[i] == a[j]){
27                     dis_char --;
28                     break;
29                 } 
30             }
31         }
32     }
33     return dis_char;
34 }
35 
36 
37 int sort_by_number_of_distinct_characters(const char* a, const char* b) {
38   int dis_a = distinct_characters(a);
39   int dis_b = distinct_characters(b);
40   if(dis_a > dis_b){
41       flag = 0;
42   }else if(dis_a == dis_b){
43       if(strcmp(a, b) > 0){
44           flag = 0;
45       }else flag = 1;
46   }else flag = 1;
47    
48  
49   return flag;
50 }
51 
52 int sort_by_length(const char* a, const char* b) {
53   int len_a = strlen(a);
54   int len_b = strlen(b);
55   if(len_a > len_b){
56       flag = 0;
57   }
58   else if(len_a == len_b){
59       if(strcmp(a, b) > 0){
60           flag = 0;
61       }else{
62           flag = 1;
63       }
64   }else  flag = 1;
65   return flag;
66 }
67 
68 void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const char* b)){
69   // int result = cmp_func;
70   int result;
71   for (int i = 0; i < len; i++) {
72     for (int j = i + 1; j < len; j++) {
73       result = (*cmp_func)((*(arr + i)), (*(arr + j)));
74       if (result == 0) {
75         char *temp;
76         temp = *(arr + i);
77         *(arr + i) = *(arr + j);
78         *(arr + j) = temp;
79       }
80     }
81   }
82 }

 

hackrank Sorting Array of Strings

标签:rap   strcmp   class   www.   hacker   string   hal   -o   http   

原文地址:https://www.cnblogs.com/zjhangia/p/10230165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!