当前位置:文档之家› 互联网大公司面试笔试题开发

互联网大公司面试笔试题开发

思路:利用栈的思想每次遇到“..”时,将退栈至上一个'/' 位置。

1. #include <stdio.h>2. #include <string.h>3. #include <stdlib.h>4.5. char * convert_path_opt (const char * path )6. {7. char * result = NULL ;8. int top = 0 ;9. int path_len = 0 ,10. index = 0 ,11. point_cnt = 0 ;12.13. / ** <check * /14. if (NULL == path )15. {16. fprintf (stderr ,“convert_path_opt:invalid argument。

\ n”);17. 返回NULL ;18. }19.20. / ** <分配内存* /21. path_len = strlen (path );22. result = (char *)malloc (path_len * sizeof (char ));23. if (NULL == 结果)24. {25. fprintf (stderr ,“convert_path_opt:无法访问malloc。

\n”);26. 返回NULL ;27. }28.29. / ** <convert * /30. while (index < path_len )31. {32. / ** <点之前复制字符。

* /33. while (index < path_len && path [ index ] != '。

' )34. {35. result [ top ++] = path [ index ++];36. }37.38. / ** <反点。

* /39. for (point_cnt = 0 ;40. index < path_len && path [ index ] == '。

' ;41. ++ point_cnt ,++ index );42.43. if (point_cnt == 2 )44. {45. - 顶部;46. while (- top > 0 && result [ top ] != '/' );47. ++ 顶部;48. }49.50. ++ index ;51. }52.53. 结果[ top ] = '\ 0' ;54.55. 返回结果;56.}57.58.int main ()59.{60. char * path = “/ home / news /。

/。

/ tmp / game /../”;61. char * result = NULL ;62.63. result = convert_path_opt (path );64. printf (“\ nResult is%s。

\ n”,result );65.66. 返回0 ;67.}1.1. 2. 比较两个字符串的不同,返回不同点的位置(相对第二字符串而言)1.2.1. #include <stdio.h>3.4. char * compare_strings (const char * plhs ,5. const char * prhs )6. {7. int lhs_index = 0 ,8. rhs_index = 0 ;9.10. / ** <检查参数。

* /11. if (NULL == plhs || NULL == prhs )12. {13. fprintf (stderr ,“copmare_strings:invalid argument。

\n”);14. 返回NULL ;15. }16.17. / ** <比较* /18. 而(plhs [ lhs_index ] != '\ 0'19. && prhs [ rhs_index ] != '\ 0' )20. {21. if (plhs [ lhs_index ] != prhs [ rhs_index ] )22. 打破;23. ++ lhs_index ;24. ++ rhs_index ;25. }26.27. return prhs + rhs_index ;28.}29.30.int main ()31.{32. char * plhs = “你好,世界。

”;33. char * prhs = “你好,世界。

”;34. char * pdiff = NULL ;35.36. pdiff = compare_strings (plhs ,prhs );37. printf (“\ nDifference is%s。

\ n”,pdiff );38.39. 返回0 ;40.}41.1.42.3. 给定一个100 亿的整数文件,输出前100 个最小值。

43.44.45.46.思路:大数据+ top k 问题。

47.48.1. #include <stdio.h>49.#include <stdlib.h>50.#include <time.h>51.52./ ** <max_heap * /53.struct max_heap54.{55. int current_index ;56. INT 容量;57. int * elem ;58.};59.typedef struct max_heap max_heap ;60.61./ ** <allocate max_heap * /62.max_heap * alloc_max_heap (const的INT 帽)63.{64. max_heap * new_heap = NULL ;65.66. / ** <检查参数* /67. if (cap <= 0 )68. {69. fprintf (stderr ,“alloc_max_heap:invalid parameter。

\n”);70. 返回NULL ;71. }72.73. / ** <分配内存* /74. new_heap = (max_heap *)malloc的(的sizeof (max_heap ));75. if (NULL == new_heap )76. {77. fprintf (stderr ,“alloc_max_heap:malloc失败。

\ n”);78. 返回NULL ;79. }80.81. new_heap - > elem = (int *)calloc (cap ,sizeof (int ));82. if (NULL == new_heap - > elem )83. {84. fprintf (stderr ,“alloc_max_heap:calloc失败。

\ n”);85. free (new_heap ),new_heap = NULL ; / ** <免费分配内存。

* /86. 返回NULL ;87. }88.89. / ** <设置其他参数。

* /90. new_heap - > capacity = cap ;91. new_heap - > current_index = 0 ;92.93. 返回new_heap ;94.}95.96./ ** <destroy max_heap * /97.空隙destroy_max_heap (max_heap ** 堆)98.{99. if (NULL != 堆)100. {101. if ((* heap )- > elem )102. {103. free ((* heap )- > elem );104. (* heap )- > elem = NULL ;105. }106.107. free (* heap );108. * heap = NULL ;109. }110. }111.112. / ** <交换元素* /113. void swap_int (int * number ,int lhs ,int rhs )114. {115. int tmp = number [ lhs ];116. number [ lhs ] = number [ rhs ];117. number [ rhs ] = tmp ;118. }119.120. / ** <插值* /121. 空隙push_value (max_heap * 堆,常量INT 值)122. {123. int left_child = 0 ,124. right_child = 0 ,125. parent_index = 0 ;126.127. / ** <检查参数* /128. if (NULL == heap )129. {130. fprintf (stderr ,“push_value:invalid parameter。

\ n”);131. 回归;132. }133. if (heap - > current_index == heap - > capacity )134. {135. fprintf (stderr ,“push_value:这个max_heap已满。

\ n”);136. 回归;137. }138.139. / ** <推送元素* /140. parent_index = heap - > current_index ;141. heap - > elem [ heap - > current_index ++] = value ;142.143. while (parent_index > = 0 )144. {145. left_child = 2 * parent_index + 1 ;146. right_child = 2 * parent_index + 2 ;147.148. if (left_child < heap - > current_index149. && heap - > elem [ left_child ] > heap - > elem [ parent_index ] )150. {151. swap_int (heap - > elem ,left_child ,parent_index );152. }153.154. if (right_child < heap - > current_index155. && heap - > elem [ right_child ] > heap - > elem [ parent_index ] )156. {157. swap_int(heap->elem,right_child,parent_index);158. }159.160. - parent_index ;161. }162. }163.164. / ** <pop元素* /165. INT pop_value (max_heap * 堆)166. {167. int ret = 0 ,168. left_child = 0 ,169. right_child = 0 ,170. parent_index = 0 ,171. swap_index = 0 ;172.173. / ** <检查参数。

相关主题