鱼C论坛

 找回密码
 立即注册
查看: 2080|回复: 4

[技术交流] 961_重复 N 次的元素

[复制链接]
发表于 2019-5-20 15:05:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. /*
  2. 在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次。

  3. 返回重复了 N 次的那个元素。



  4. 示例 1:

  5. 输入:[1,2,3,3]
  6. 输出:3
  7. 示例 2:

  8. 输入:[2,1,2,5,3,2]
  9. 输出:2
  10. 示例 3:

  11. 输入:[5,1,5,2,5,3,5,4]
  12. 输出:5


  13. 提示:

  14. 4 <= A.length <= 10000
  15. 0 <= A[i] < 10000
  16. A.length 为偶数


  17. */
复制代码



  1. struct node_t
  2. {
  3.     int            key;
  4.     int            value;  /* key 出现的次数 */
  5.     struct node_t* left;
  6.     struct node_t* right;
  7. };



  8. struct node_t* root = NULL;



  9. void node_init(struct node_t* node_s, int key)
  10. {
  11.     if(node_s !=NULL)
  12.     {
  13.         node_s -> key   = key   ;
  14.         node_s -> value = 1     ;
  15.         node_s -> left  = NULL  ;
  16.         node_s -> right = NULL  ;
  17.     }
  18. }

  19. struct node_t* bst_insert(struct node_t* node_s, int key )
  20. {
  21.     if(node_s == NULL)
  22.     {
  23.         node_s = (struct node_t*)malloc(sizeof(struct node_t));
  24.         node_init(node_s,key);
  25.         return node_s;
  26.     }
  27.    
  28.     if(key == node_s->key)
  29.     {
  30.         node_s->value ++;
  31.         return node_s;
  32.     }
  33.     else if(key < node_s->key)
  34.     {
  35.         node_s->left = bst_insert(node_s->left, key );
  36.     }
  37.     else // key > node_s->key
  38.     {
  39.         node_s->right = bst_insert(node_s->right, key );
  40.     }


  41.     return node_s;

  42. }

  43. int* bst_dfs( struct node_t* node_s , int value)
  44. {
  45.    
  46.     int* ret_val_left  = NULL;
  47.     int* ret_val_right = NULL;
  48.     if(node_s == NULL)
  49.     {
  50.         return NULL;
  51.     }
  52.     if(node_s->value == value)
  53.     {
  54.         return &node_s->key;
  55.     }
  56.     else
  57.     {
  58.         ret_val_left  = bst_dfs(node_s->left,value);
  59.         ret_val_right = bst_dfs(node_s->right,value);
  60.         
  61.         if(ret_val_left!=NULL)
  62.         {
  63.             return ret_val_left;
  64.         }
  65.         else
  66.         {
  67.             return ret_val_right;
  68.         }
  69.     }
  70. }


  71. void bst_delete(struct node_t* bd_node_s)
  72. {
  73.     if(bd_node_s == NULL)
  74.     {
  75.         return;
  76.     }
  77.    
  78.     if(bd_node_s->left !=NULL)
  79.     {
  80.         bst_delete(bd_node_s->left);
  81.         bd_node_s->left = NULL;
  82.     }
  83.     if(bd_node_s->right !=NULL)
  84.     {
  85.         bst_delete(bd_node_s->right);
  86.         bd_node_s->right = NULL;
  87.     }
  88.    
  89.     free(bd_node_s);
  90.     bd_node_s = NULL;
  91. }

  92. int repeatedNTimes(int* A, int ASize)
  93. {
  94.     int* temp_ptr = NULL;
  95.     int ret_val = 0;
  96.    
  97.     for(int i = 0; i < ASize ; i++)
  98.     {
  99.         root = bst_insert(root, A[i]);
  100.     }
  101.     temp_ptr = bst_dfs( root,ASize / 2 );
  102.     ret_val = *temp_ptr;
  103.     bst_delete(root);
  104.     root = NULL;
  105.     return ret_val;
  106. }
复制代码



测试结果:
  1. 执行用时 : 132 ms, 在N-Repeated Element in Size 2N Array的C提交中击败了14.72% 的用户
  2. 内存消耗 : 38.9 MB, 在N-Repeated Element in Size 2N Array的C提交中击败了5.71% 的用户
  3. 102 / 102 个通过测试用例
  4. 状态:通过
  5. 执行用时:124 ms
  6. 提交时间:7 分钟之前


复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-20 15:08:48 | 显示全部楼层
用BST逐一插入,然后搜索就可以了。。

不过时空复杂度已经炸飞了。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-20 15:10:09 | 显示全部楼层
回到题意:在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次。

所以在BST插入的过程中,不需要插入完全,只要在插入时发现已经存在就可以中断插入然后直接返回了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-20 15:14:05 | 显示全部楼层
本帖最后由 风扫地 于 2019-5-21 09:44 编辑

优化后的实现:
  1. struct node_t
  2. {
  3.     int            key;
  4.     int            value;  /* key 出现的次数 */
  5.     struct node_t* left;
  6.     struct node_t* right;
  7. };



  8. struct node_t* root = NULL;
  9. int* temp_ptr = NULL;



  10. void node_init(struct node_t* node_s, int key)
  11. {
  12.     if(node_s !=NULL)
  13.     {
  14.         node_s -> key   = key   ;
  15.         node_s -> value = 1     ;
  16.         node_s -> left  = NULL  ;
  17.         node_s -> right = NULL  ;
  18.     }
  19. }

  20. struct node_t* bst_insert(struct node_t* node_s, int key )
  21. {
  22.     if(node_s == NULL)
  23.     {
  24.         node_s = (struct node_t*)malloc(sizeof(struct node_t));
  25.         node_init(node_s,key);
  26.         return node_s;
  27.     }
  28.    
  29.     if(key == node_s->key)
  30.     {
  31.         node_s->value ++;
  32.         temp_ptr = &(node_s->key);
  33.         return node_s;
  34.     }
  35.     else if(key < node_s->key)
  36.     {
  37.         node_s->left = bst_insert(node_s->left, key );
  38.     }
  39.     else // key > node_s->key
  40.     {
  41.         node_s->right = bst_insert(node_s->right, key );
  42.     }


  43.     return node_s;

  44. }

  45. int* bst_dfs( struct node_t* node_s , int value)
  46. {
  47.    
  48.     int* ret_val_left  = NULL;
  49.     int* ret_val_right = NULL;
  50.     if(node_s == NULL)
  51.     {
  52.         return NULL;
  53.     }
  54.     if(node_s->value == value)
  55.     {
  56.         return &node_s->key;
  57.     }
  58.     else
  59.     {
  60.         ret_val_left  = bst_dfs(node_s->left,value);
  61.         ret_val_right = bst_dfs(node_s->right,value);
  62.         
  63.         if(ret_val_left!=NULL)
  64.         {
  65.             return ret_val_left;
  66.         }
  67.         else
  68.         {
  69.             return ret_val_right;
  70.         }
  71.     }
  72. }


  73. void bst_delete(struct node_t* bd_node_s)
  74. {
  75.     if(bd_node_s == NULL)
  76.     {
  77.         return;
  78.     }
  79.    
  80.     if(bd_node_s->left !=NULL)
  81.     {
  82.         bst_delete(bd_node_s->left);
  83.         bd_node_s->left = NULL;
  84.     }
  85.     if(bd_node_s->right !=NULL)
  86.     {
  87.         bst_delete(bd_node_s->right);
  88.         bd_node_s->right = NULL;
  89.     }
  90.    
  91.     free(bd_node_s);
  92.     bd_node_s = NULL;
  93. }

  94. int repeatedNTimes(int* A, int ASize)
  95. {

  96.     int ret_val = 0;
  97.    
  98.     for(int i = 0; i < ASize ; i++)
  99.     {
  100.         root = bst_insert(root, A[i]);
  101.         if(temp_ptr!=NULL)
  102.         {
  103.             break;
  104.         }
  105.     }
  106.     ret_val = *temp_ptr;
  107.    
  108.     bst_delete(root);
  109.     root = NULL;
  110.     temp_ptr = NULL;
  111.     return ret_val;
  112. }
复制代码

其中dfs代码可以省略(因为是错的)。

有了不错的改善:
  1. 执行用时 : 40 ms, 在N-Repeated Element in Size 2N Array的C提交中击败了56.23% 的用户
  2. 内存消耗 : 8.7 MB, 在N-Repeated Element in Size 2N Array的C提交中击败了77.14% 的用户
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-21 10:07:47 | 显示全部楼层
用python就简单多了

class Solution:
    def repeatedNTimes(self, A: List[int]) -> int:
        s = set();
        for each in A:
            if each not in s:
                s.add(each)
            else:
                return each



执行用时 : 64 ms, 在N-Repeated Element in Size 2N Array的Python3提交中击败了94.11% 的用户
内存消耗 : 14 MB, 在N-Repeated Element in Size 2N Array的Python3提交中击败了96.83% 的用户
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-3 10:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表