hackrank Sorting Array of Strings

Posted zjhangia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hackrank Sorting Array of Strings相关的知识,希望对你有一定的参考价值。

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] != ){
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的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5427 A problem of sorting 水题

python sorting_parts_of_speech.py

leetcode-mid-array-sorting and searching - 215 Kth Largest Element in an Array

Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted

Hackrank Equal DP

排序算法视频 《6 分钟演示 15 种排序算法》