每日微軟面試題

每日微軟面試題——day 1

<以下微軟面試題全來自網絡>

<以下答案與分析純屬個人觀點,不足之處,還望不吝指出^_^>

題:.編寫反轉字符串的程序,要求優化速度、優化空間。

分析:構建兩個迭代器p 和 q ,在一次遍歷中,p的位置從字串開頭向中間前進,q從字串末尾向中間后退,反轉字串只要每次遍歷都交換p和q所指向的內容即可,直到p和q在中間相遇,這時循環次數剛好等于 字串的長度/2。

實現代碼:


[cpp] view plaincopy
  1. /**?
  2. author:?花心龜?
  3. blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. ??
  6. #include?<stdio.h>??
  7. void?reverse(char?*_str,int?_l)?//反轉函數,_l指要反轉字串的長度??
  8. {??
  9. ?char?*p=_str,*q=_str+_l-1,temp;??
  10. ??
  11. ?while(q>p)??
  12. ???{???
  13. ?????temp=*p;??
  14. ?????*p=*q;??
  15. ?????*q=temp;??
  16. ???
  17. ?????p++;??
  18. ?????q--;??
  19. ???}??
  20. }??
  21. ???
  22. int?main()??
  23. {??
  24. ??charstr0[11]=?"0123456789";???????
  25. ?reverse(str0,sizeof(str0)-1);??
  26. ?printf("str0?=?%s\n",str0);??
  27. ???
  28. ??char?str1[6]="01234";??
  29. ?reverse(str1,sizeof(str1)-1);??
  30. ?printf("str1?=?%s",str1);??
  31. ??return?0;??
  32. }??



題:.在鏈表里如何發現循環鏈接?

分析:可以構建兩個迭代器p和pp,p一次向移動一個節點,pp一次移動兩個節點,如果p和pp在某個時刻指向了同一個節點,那么該鏈表就有循環鏈接。

實現代碼:

[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. #include?<stdio.h>??
  6. #include?<malloc.h>??
  7. ??
  8. #define?TEST??
  9. ??
  10. struct?list_node??
  11. {??
  12. ??int?data;??
  13. ??list_node?*?next;??
  14. };??
  15. list_node?*head;?//指向頭結點??
  16. ??
  17. void?list_create()??
  18. {??
  19. ??int?i;??
  20. ??list_node?*p=NULL;??
  21. ??head?=?(list_node*)malloc(sizeof(list_node));??
  22. ??head->data=0;?????//頭結點數據設為??
  23. ??head->next?=?NULL;??
  24. ??
  25. ??p=head;??
  26. ??for(i=1;?i<6;?i++)?//創建個結點??
  27. ????{??
  28. ??????p->next?=?(list_node*)malloc(sizeof(list_node));??
  29. ??????p->next->data?=?i;??
  30. ??????p->next->next?=?NULL;??
  31. ??????p=p->next;??
  32. ????}??
  33. ??p->next?=?head;??//使尾結點的下一個結點指針指向頭結點,構成循環鏈表??
  34. ??
  35. #ifdef?TEST??
  36. ??p=head;??
  37. ??for(i=0;?i<12&&p!=NULL;?i++)??
  38. ????{??
  39. ??????printf("%d?",p->data);??
  40. ??????p=p->next;??
  41. ????}??
  42. ??printf("\n");??
  43. #endif??
  44. }??
  45. ??
  46. void?cycleList_test()??
  47. {??
  48. ??if(head==NULL?||?head->next?==?NULL)??
  49. ??????return;??
  50. ??list_node?*p=NULL,*pp=NULL;??
  51. ??p=head;??
  52. ??pp=head->next->next;??
  53. ??while(p!=pp?)??
  54. ????{??
  55. ??????p=p->next;??
  56. ??????pp=pp->next->next;??
  57. ??
  58. ??????if(p==NULL?||?pp==NULL)??
  59. ????{??
  60. ??????printf("不是循環鏈表");??
  61. ??????return?;??
  62. ????}??
  63. ????}??
  64. ??printf("是循環鏈表");??
  65. }??
  66. ??
  67. void?list_destroy()??
  68. {??
  69. ??list_node?*pmove=NULL,*pdel=NULL;??
  70. ??pmove=head->next;??
  71. ??
  72. ?while(pmove!=head)??
  73. ???{??
  74. ?????pdel=pmove;??
  75. ?????pmove=pmove->next;??
  76. ?????free(pdel);?????
  77. ???}??
  78. ??
  79. ??free(head);??
  80. }??
  81. int?main()??
  82. {??
  83. ??list_create();???//構建循環鏈表??
  84. ??cycleList_test();//測試是否是循環鏈表??
  85. ??list_destroy();??//銷毀鏈表??
  86. ??return?0;??
  87. }?


題:.給出洗牌的一個算法,并將洗好的牌存儲在一個整形數組里。

分析:首先54張牌分別用0到53 的數值表示并存儲在一個整形數組里,數組下標代表紙牌所在的位置。接下來,遍歷整個數組,在遍歷過程中隨機產生一個隨機數,并以該隨機數為下標的數組元素與當前遍歷到的數組元素進行對換。時間復雜度為O(n) (注:所得到的每一種結果的概率的分母越大越好)

實現代碼:

[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. ??
  6. #include?<stdio.h>??
  7. #include?<time.h>??
  8. #include?<stdlib.h>??
  9. ??
  10. void?shuffle(int?boke[])??//洗牌??
  11. {??
  12. ??int?i,r,t;??
  13. ??srand((unsigned)time(NULL));?//隨機數種子??
  14. ??for(i=0;?i<54;?i++)??
  15. ????{??
  16. ??????r=(rand()%107)/2;??
  17. ??
  18. ??????//交換??
  19. ??????t=boke[i];??
  20. ??????boke[i]=boke[r];??
  21. ??????boke[r]=t;??
  22. ????}??
  23. }??
  24. ??
  25. int?main()??
  26. {??
  27. ??int?boke[54],i;??
  28. ??for(i=0;i<54;i++)?//初始化紙牌??
  29. ????boke[i]=i;??
  30. ??
  31. ??printf("before?shuffle:\n");??
  32. ??for(i=0;?i<54;?i++)????//打印??
  33. ??????printf("%d?",boke[i]);??
  34. ??
  35. ??
  36. ??shuffle(boke);?????//洗牌??
  37. ??
  38. ??
  39. ??printf("\nafter?shuffle:\n");??
  40. ??for(i=0;?i<54;?i++)???//打印??
  41. ??????printf("%d?",boke[i]);??
  42. ??
  43. ??return?0;??
  44. }??



題:寫一個函數,檢查字符串是否是整數,如果是,返回其整數值。

(或者:怎樣只用4行代碼編寫出一個從字符串到長整形的函數?)

分析:略

實現代碼:

[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. ??
  6. #include?<stdio.h>??
  7. ??
  8. long?convert(const?char?*str)?//?4行代碼?下面我用了四句語句,不知當不當否,權當娛樂^^??
  9. {??
  10. ??long?value=0,f=1;?????//f將決定value是否為負數??
  11. ??
  12. ??if(*str?==?'-')?str++,f=-1;??
  13. ??
  14. ??for(;*str!='\0'?&&?*str>='0'?&&?*str<='9';?str++)??
  15. ????value=value*10+(*str-'0');????
  16. ??
  17. ??return?*str=='\0'?value*f:0;??//如果不為整數返回??
  18. }??
  19. ??
  20. int?main()??
  21. {??
  22. ??
  23. ??char?str0[]?=?"1234567";??
  24. ??printf("%d\n",convert(str0));??
  25. ??
  26. ??char?str1[]?=?"4.56";??
  27. ??printf("%d\n",convert(str1));??
  28. ??
  29. ??char?str2[]?=?"-1234567";??
  30. ??printf("%d\n",convert(str2));??
  31. ??
  32. ??char?str3[]?=?"-4.56";??
  33. ??printf("%d\n",convert(str3));??
  34. ????
  35. ??return?0;??
  36. }?


題:怎樣從頂部開始逐層打印二叉樹結點數據?請編程。

分析不用遞歸,定義兩個棧(或數組),也能方便漂亮地搞定。首先棧1存儲第一層中的節點也就是根節點,然后每次循環,打印棧1中的元素,再將棧1中的節點更新為下一層中的節點。總共循環logn+1次。


實現代碼(以下遺漏了對二叉樹的銷毀操作):


[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. #include?<stdio.h>??
  6. #include?<malloc.h>??
  7. ??
  8. typedef?struct?bt_node??
  9. {??
  10. ??int?data;??
  11. ??struct?bt_node?*rchild;??
  12. ??struct?bt_node?*lchild;??
  13. }BinTree,node_t;??
  14. ??
  15. BinTree?*myTree;??
  16. ??
  17. node_t*?bt_new_node(int?data)??
  18. {??
  19. ??node_t*?node?=?(node_t*)malloc(sizeof(node_t));??
  20. ??node->rchild?=?NULL;??
  21. ??node->lchild?=?NULL;??
  22. ??node->data?=?data;??
  23. ??
  24. ??return?node;??
  25. }??
  26. void?bt_create()??
  27. {??
  28. ??//第一層根節點,數字11表示第一層的第一個位置,以下類似??
  29. ??myTree?=?bt_new_node(11);??
  30. ??
  31. ??//創建第二層節點???
  32. ??myTree->lchild?=?bt_new_node(21);??
  33. ??myTree->rchild?=?bt_new_node(22);???
  34. ??
  35. ??
  36. ??//創建第三層節點??
  37. ??myTree->lchild->lchild?=?bt_new_node(31);??
  38. ??myTree->lchild->rchild?=?bt_new_node(32);??
  39. ??myTree->rchild->lchild?=?bt_new_node(33);??
  40. ??myTree->rchild->rchild?=?bt_new_node(34);??
  41. ??
  42. ??//創建第四層節點??
  43. ??myTree->rchild->rchild->rchild?=?bt_new_node(48);??
  44. }??
  45. ??
  46. void?print_layer_by_layer()??//逐層打印二叉樹非遞歸算法(主題)??
  47. {??
  48. ??node_t*?stack1[100]={0};//棧??
  49. ??node_t*?stack2[100]={0};//棧??
  50. ??int?T1=0,T2=0;???????????//棧頂下標??
  51. ??
  52. ??
  53. ??stack1[T1++]=myTree;???//根節點入棧??
  54. ??while(T1)?//若棧為空則停止循環??
  55. ????{??
  56. ??????while(T1)??
  57. ????{??
  58. ??????T1--;??
  59. ??????printf("%d?",stack1[T1]->data);?//打印棧頂元素??
  60. ??????stack2[T2++]=stack1[T1];????????//將棧元素轉存到棧2中??
  61. ????}??
  62. ??????printf("\n");??
  63. ??
  64. ??????//此時棧已存儲了當前行的各元素??
  65. ??????//通過棧得到下一行中的節點并更新到棧中??
  66. ??????while(T2)????
  67. ????{??
  68. ??????T2--;??
  69. ??????if(stack2[T2]->rchild?!=?NULL)??
  70. ????????stack1[T1++]=stack2[T2]->rchild;??
  71. ??
  72. ??????if(stack2[T2]->lchild?!=?NULL)??
  73. ????????stack1[T1++]=stack2[T2]->lchild;??
  74. ????}??
  75. ????}??
  76. }??
  77. int?main()??
  78. {??
  79. ??bt_create();????????????//創建二叉樹??
  80. ??print_layer_by_layer();?//逐層打印??
  81. ??return?0;??
  82. }??

另:關于此題的其它想法請看3樓我的回復,回復上的編碼步驟為廣度優先遍歷二叉樹算法,不過本人不才哈,不知題目中的意思是一層一層的打印呢,還是按照層的順序從左到右一個一個打印呢(好像也是逐層打印),且不管它,通過對兩種算法比較,上面使用棧的算法功能更強,至少打完一層可以再打一個回行。而使用隊列的廣度優先遍歷二叉樹算法有效率優勢,且代碼簡潔易懂,就是不能打完一層后回行。^_^ 純屬個人觀點,望不吝指教。


題:怎樣把一個鏈表掉個順序(也就是反序,注意鏈表的邊界條件并考慮空鏈表)?

分析:這題比較有意思,我想了個高效的實現。首先定義兩個迭代器 p 和 q,q從第一個節點開始遍歷,p從第二個節點開始遍歷,每次遍歷將頭指針的next指向p的next,然后將p的next 反指向q(此時q是p的前一個節點),也就是說將每個節點的鏈接方向掉過來,最后尾節點變成了頭節點,頭節點變成了尾節點,時間復雜度為高效的O(n)


圖示:(最后一個N指空節點)


以下便是是我簡潔的實現代碼:

[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. ???
  6. #include?<stdio.h>??
  7. #include?<malloc.h>??
  8. ???
  9. #define?TEST??
  10. ???
  11. typedef?struct?list_node??
  12. {??
  13. ??int?data;??
  14. ??structlist_node?*?next;??
  15. }list_node;??
  16. ???
  17. list_node?*head;?//頭結點??
  18. ???
  19. void?list_create()??
  20. {??
  21. ??int?i;??
  22. ??list_node?*p;??
  23. ??head?=?(list_node*)malloc(sizeof(list_node));??
  24. ??head->data=0;?????//頭結點數據設為??
  25. ??head->next?=?NULL;??
  26. ???
  27. ??p=head;??
  28. ??for(i=1;i<6;?i++)?//創建5個結點??
  29. ????{??
  30. ??????p->next?=?(list_node*)malloc(sizeof(list_node));??
  31. ??????p->next->data?=?i;??
  32. ??????p->next->next?=?NULL;??
  33. ??????p=p->next;??
  34. ????}??
  35. ???
  36. #ifdef?TEST??
  37. ??p=head;??
  38. ??while(p!=NULL)???//打印該鏈表??
  39. ????{??
  40. ??????printf("%d",p->data);??
  41. ??????p=p->next;??
  42. ????}??
  43. ??printf("\n");??
  44. #endif??
  45. }??
  46. ???
  47. void?list_reverse()??//使鏈表反序?(主題)??
  48. {??
  49. ??printf("^^after?reversing:\n");??
  50. ??if(head?==?NULL?||?head->next==NULL)?return?;//如果head為空,則返回??
  51. ??list_node?*p,*q;??
  52. ??q=head;??
  53. ??p=head->next;??
  54. ???
  55. ??while(p!=NULL)??
  56. ????{??
  57. ????head->next=p->next;?//將頭指針的next指向p的下一個節點??
  58. ????p->next=q;??????????//將p的next值反指向它的前一個節點q??
  59. ????q=p;????????????????//q移向p的位置??
  60. ????p=head->next;???????//p移向它的下一個位置??
  61. ????}??
  62. ?????
  63. ????head?=?q;??
  64. ???
  65. #ifdef?TEST??
  66. ??p=head;??
  67. ??while(p!=NULL)????????//打印該鏈表??
  68. ????{??
  69. ??????printf("%d",p->data);??
  70. ??????p=p->next;??
  71. ????}??
  72. ??printf("\n");??
  73. #endif??
  74. ???
  75. }??
  76. ???
  77. void?list_destroy()??//銷毀函數??
  78. {??
  79. ??list_node?*pmove=NULL,*pdel=NULL;??
  80. ??pmove=head;??
  81. ???
  82. ?while(pmove!=head)??
  83. ???{??
  84. ?????pdel=pmove;??
  85. ?????free(pdel);????
  86. ?????pmove=pmove->next;??
  87. ???}??
  88. }??
  89. int?main()??
  90. {??
  91. ??list_create();???//構建單鏈表??
  92. ??list_reverse();??//反轉鏈表??
  93. ??list_destroy();??//銷毀鏈表??
  94. ??return?0;??
  95. }?


題:求隨機數構成的數組中找到長度大于=3的最長的等差數列

輸出等差數列由小到大:?

如果沒有符合條件的就輸出[0,0]

格式:

輸入[1,3,0,5,-1,6]

輸出[-1,1,3,5]

要求時間復雜度,空間復雜度盡量小

分析基本算法思路(采用動態規劃思想):首先,只要得到數列的公差和一個首項就可以確定一個等差數列,因此我們要尋找最長等差數列的公差以及首項。其次,為了方便查找公差和首項,我們應該將原數組進行由小到大排序,這樣各兩數之間的公差也是成遞增形勢的,這樣我們就可以避免回溯查找首項

因此,在搜尋公差及首項的過程中,我們可以分兩三個決策階段:

1、如果公差為0,應該做何處理。

2、如果公差不為0,應該做何處理。

3、如果找到的數列長度是當前最長的做相應的處理

? ? ? 針對以上情況,我們應該選擇一種合適的數據結構——平衡排序樹,stl中的set,map,mutiset,multimap是以紅黑樹結構為形勢的容器,無疑是非常合適的,根據題目情況,可能存在具有相同元素的數組,因此我們選擇multiset,這樣無論我們對數據進行插入排序,查找都是比較高效的,因此總體上是可以滿意的。

? ? ? 最后有幾項時間上的優化不在這里說明,詳情可看代碼。若有不足之處,望能不吝指出!^_^


我的實現代碼:


[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. #include?<iostream>??
  6. #include?<ctime>??
  7. #include?<set>??
  8. using?namespace?std;??
  9. ??
  10. void?show_longest_seq(const?multiset<int>&?myset)??
  11. {??
  12. ????int?maxLength?=?0,?curr_pos?=?0,?curr_d?=?0,?counter=0,i=0;?//一些輔助變量??
  13. ????int?d_result,?a1_result;?//存儲最長等差數列的公差以及首項??
  14. ????multiset<int>::const_iterator?set_it1,set_it2;??
  15. ??
  16. ??????
  17. ????/*?
  18. ?????????(主題)尋找長度最長的等差數列,最壞情況下時間復雜度為O(n^3)?
  19. ????*/??
  20. ????for(set_it1?=?myset.begin();?set_it1?!=?myset.end();)??
  21. ????{??
  22. ????????for(set_it2=set_it1,set_it2++;?set_it2?!=?myset.end();)//第二層循環從set_it1所指的下一個元素開始遍歷??
  23. ????????{??
  24. ????????????curr_d?=?*set_it2?-?*set_it1;?//算得當前公差,注意由于set為自排序容器,從小到大排列,所以curr_d恒為正??
  25. ??
  26. ????????????if(curr_d?==?0)?//?如果公差為0??
  27. ????????????{??
  28. ????????????????counter?=?myset.count(*set_it1);??
  29. ????????????????set_it2?=?myset.upper_bound(*set_it1);//(優化項)跳過與set_it1相等的元素??
  30. ????????????}??
  31. ????????????else??
  32. ????????????{??
  33. ????????????????counter?=?2;?//(優化項)最小長度要求要不小于所以直接從開始累加??
  34. ????????????????while(myset.find(*set_it1?+?counter*curr_d)?!=?myset.end())?//計算數列項個數??
  35. ????????????????????++counter;??
  36. ??
  37. ????????????????set_it2?=?myset.upper_bound(*set_it2);//?(優化項)跳過與*set_it2相等的元素??
  38. ????????????}??
  39. ??
  40. ??????????????
  41. ????????????if(counter?>?maxLength)??//如果新數列長度大于maxLength??
  42. ????????????{??
  43. ????????????????d_result?=?curr_d;??
  44. ????????????????a1_result?=?*set_it1;??
  45. ????????????????????????maxLength?=?counter;??
  46. ????????????}??
  47. ????????}??
  48. ??
  49. ????????curr_pos?+=?myset.count(*set_it1);?????//計算第一層循環遍歷到的當前位置??
  50. ????????if(myset.size()-curr_pos?<?maxLength)??//?(優化項)如果集合中剩下的元素小于最大數列長度,就退出循環??
  51. ????????????break;??
  52. ??
  53. ????????set_it1?=?myset.upper_bound(*set_it1);?//下一次set_it1?的位置,并跳過相同元素??
  54. ????}??
  55. ??
  56. ??
  57. ??
  58. ??
  59. ????/*?
  60. ???????打印最長等差數列?
  61. ????*/??
  62. ????if(maxLength?<=?2)??
  63. ????{??
  64. ????????cout<<"longest_seq:[0,0]"<<endl;??
  65. ????}??
  66. ????else??
  67. ????{??
  68. ????????cout<<"longest_seq:";??
  69. ??????????
  70. ????????for(i?=?0;?i<maxLength;??i++)??
  71. ????????????cout<<*(myset.find(a1_result?+?i*d_result))<<'?';??
  72. ??
  73. ????????cout<<endl;??
  74. ????}??
  75. }??
  76. //Blog:http://blog.csdn.net/zhanxinhang??
  77. ?test?in?main??
  78. int?main()??
  79. {??
  80. ????int?a[]={1,3,0,5,-1,6};??
  81. ????multiset<int>?myset;??
  82. ????myset.insert(a,a+6);??
  83. ????show_longest_seq(myset);??
  84. ????cout<<endl;??
  85. ??
  86. ????int?l;??
  87. ????srand((unsigned)time(NULL));??
  88. ????for(int?j?=?0;?j?<?5;?j++)??
  89. ????{??
  90. ????????myset.clear();??
  91. ????????cout<<"input:[?";??
  92. ????????l=rand()%10;??
  93. ????????for(int?i?=?0;?i?<?l;?++i)??
  94. ????????{??
  95. ????????????int?element?=?rand()%10;??
  96. ????????????myset.insert(element);??
  97. ????????????cout<<element<<'?';??
  98. ????????}??
  99. ????????cout<<']'<<endl;??
  100. ????????show_longest_seq(myset);??
  101. ????????cout<<endl;??
  102. ????}??
  103. ??
  104. ??
  105. ????return?0;??
  106. }??


附一張測試結果圖:





題:兩個鏈表,一升一降。合并為一個升序鏈表。

分析假設升序的鏈表為鏈表1,降序的鏈表為鏈表2,p1,p2分別作為它們的迭代器,還有一個合并鏈表用于存放合并后的數據

法一、最容易想到的且容易實現的就是使兩個表都變成升序,然后就是經典的合并排序算法的步驟了,步驟是構建p1,p2兩個迭代器,分別用于迭代兩個鏈表,每次遍歷,若p1所指的節點數據大于p2所指的節點數據則將p1所指的節點數據插入到要合并鏈表中去且使p1指向下一個節點,否則將p2將所指的節點數據插入到合并鏈表中去且使p2指向下一個節點,直到p1和p2任意一個指向了NULL為止。最后可能兩個鏈表中尚有剩余的節點,將其逐個插入到合并鏈表中去即可。

法二、使用遞歸方法后序遍歷降序鏈表2,遍歷順序就相當于升序的順序了。在遞歸遍歷鏈表2的過程中,需要處理有以下三件事:(注意順序)

(1) 如果p2的數據小于p1的就插入到合并鏈表中
(2) 如果p2的數據大于p1,那么就對鏈表1循環遍歷,每次將p1中的數據插到合并鏈表中,直到p2不大于p1,且p1不為空

(3) 如果p1為空,就直接將p2插入到合并鏈表中

(這個方法你想到了沒!)

完整實現代碼:
[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. ???
  6. #include?<stdio.h>??
  7. #include?<malloc.h>??
  8. #include?<stdlib.h>???
  9. ??
  10. typedef?struct?list_node??
  11. {??
  12. ??int?data;??
  13. ??struct?list_node?*?next;??
  14. }list_node;??
  15. ???
  16. list_node?*list1=NULL;?//鏈表頭結點??
  17. list_node?*list2=NULL;?//鏈表頭結點??
  18. ??
  19. void?list_print(const?list_node?*p)//打印該鏈表函數??
  20. {??
  21. ??if(p==NULL)return;??
  22. ??while(p!=NULL)?????
  23. ????{??
  24. ??????printf("%d?",p->data);??
  25. ??????p=p->next;??
  26. ????}??
  27. ??printf("\n");??
  28. }??
  29. ??
  30. void?list_create(list_node*?&head,?int?data[],?int?N)??
  31. {??
  32. ??if(data?==?NULL)?return;??
  33. ??
  34. ??int?i;??
  35. ??list_node?*p;??
  36. ??p?=?(list_node*)malloc(sizeof(list_node));??
  37. ??p->data?=?data[0];??
  38. ??p->next?=?NULL;??
  39. ??head?=?p;???
  40. ??for(i=1;i<N;?i++)???
  41. ????{??
  42. ??????p->next?=?(list_node*)malloc(sizeof(list_node));??
  43. ??????p->next->data?=?data[i];??
  44. ??????p->next->next?=?NULL;??
  45. ??????p=p->next;??
  46. ????}??
  47. }??
  48. ??
  49. void?list_reverse(list_node*?&head)??//使鏈表反序??
  50. {??
  51. ??if(head?==?NULL)?return?;//如果head1為空,則返回??
  52. ??list_node?*p,*q;??
  53. ??q=head;??
  54. ??p=head->next;??
  55. ???
  56. ??while(p!=NULL)??
  57. ????{??
  58. ????head->next=p->next;?//將頭指針的next指向p的下一個節點??
  59. ????p->next=q;??????????//將p的next值反指向它的前一個節點q??
  60. ????q=p;????????????????//q移向p的位置??
  61. ????p=head->next;???????//p移向它的下一個位置??
  62. ????}??
  63. ??
  64. ????head?=?q;??
  65. }??
  66. ??
  67. void?list_destroy(list_node?*head)??//銷毀函數??
  68. {??
  69. ??list_node?*pmove=NULL,*pdel=NULL;??
  70. ??pmove=head;??
  71. ???
  72. ?while(pmove!=head)??
  73. ???{??
  74. ?????pdel=pmove;??
  75. ?????free(pdel);????
  76. ?????pmove=pmove->next;??
  77. ???}??
  78. }??
  79. ??
  80. list_node*?merge_two_list()?//合并鏈表1和鏈表2(法一)??
  81. {??
  82. ????list_reverse(list2);????//反轉鏈表使之與鏈表一樣升序排列??
  83. ????list_node?*list_merged;??//和并后的鏈表??
  84. ????list_node?*p1,*p2,*p0;????????
  85. ????list_merged?=?(list_node*)malloc(sizeof(list_node));??
  86. ????list_merged->data?=?0;??
  87. ????list_merged->next?=?NULL;??
  88. ????p0?=?list_merged;??
  89. ??
  90. ????p1=list1;??
  91. ????p2=list2;??
  92. ????while(p1!=NULL?&&?p2!=NULL)??
  93. ????{??
  94. ????????if(p1->data?<?p2->data)??
  95. ????????{??
  96. ????????????p0->next=(list_node*)malloc(sizeof(list_node));??
  97. ????????????p0->next->data=p1->data;??
  98. ????????????p0->next->next=NULL;??
  99. ????????????p0=p0->next;??
  100. ????????????p1=p1->next;??
  101. ????????}??
  102. ????????else??
  103. ????????{??
  104. ????????????p0->next=(list_node*)malloc(sizeof(list_node));??
  105. ????????????p0->next->data=p2->data;??
  106. ????????????p0->next->next=NULL;??
  107. ????????????p0=p0->next;??
  108. ????????????p2=p2->next;??
  109. ????????}??
  110. ????}??
  111. ????while(p1!=NULL)??
  112. ????{??
  113. ????????????p0->next=(list_node*)malloc(sizeof(list_node));??
  114. ????????????p0->next->data=p1->data;??
  115. ????????????p0->next->next=NULL;??
  116. ????????????p0=p0->next;??
  117. ????????????p1=p1->next;??
  118. ????}??
  119. ????while(p2!=NULL)??
  120. ????{??
  121. ????????????p0->next=(list_node*)malloc(sizeof(list_node));??
  122. ????????????p0->next->data=p2->data;??
  123. ????????????p0->next->next=NULL;??
  124. ????????????p0=p0->next;??
  125. ????????????p2=p2->next;??
  126. ????}??
  127. ????return?list_merged;??
  128. }??
  129. ??
  130. ??
  131. ??
  132. list_node*?p0=(list_node*)malloc(sizeof(list_node));??
  133. list_node*?phead=p0;??
  134. list_node*?&p1=list1;?//p1與list1綁定??
  135. list_node*?foreach(list_node*?p2)?//遞歸合并(法二)??
  136. {??
  137. ????if(p2==NULL)?return?phead;??
  138. ??
  139. ????foreach(p2->next);???
  140. ??
  141. ????if(p1->data?>?p2->data)??
  142. ????{??
  143. ????????p0->next?=?(list_node*)malloc(sizeof(list_node));??
  144. ????????p0->next->data?=?p2->data;??
  145. ????????p0->next->next?=?NULL;???
  146. ????????p0=p0->next;??
  147. ????????return?phead;??
  148. ????}??
  149. ????while(p1!=NULL?&&?p1->data<=p2->data)??
  150. ????{??
  151. ????????p0->next?=?(list_node*)malloc(sizeof(list_node));??
  152. ????????p0->next->data?=?p1->data;??
  153. ????????p0->next->next?=?NULL;???
  154. ????????p0=p0->next;??
  155. ????????p1?=?p1->next;??
  156. ????}??
  157. ????if(p1==NULL)??
  158. ????{??
  159. ????????p0->next?=?(list_node*)malloc(sizeof(list_node));??
  160. ????????p0->next->data?=?p2->data;??
  161. ????????p0->next->next?=?NULL;???
  162. ????????p0=p0->next;??
  163. ????}??
  164. ??
  165. ????return?phead;??
  166. }??
  167. //Blog:http://blog.csdn.net/zhanxinhang??
  168. int?main()??
  169. {??
  170. ????int?list1_data[]?=?{1,4,6,8,10};????//鏈表數據升序?可在這里該數據進行測試??
  171. ????int?list2_data[]?=?{14,9,3,2};??????//鏈表數據降序??
  172. ??
  173. ????list_create(list1,list1_data,5);???//構建單鏈表??
  174. ????list_create(list2,list2_data,4);???//構建單鏈表??
  175. ????list_print(list1);??
  176. ????list_print(list2);??
  177. ??
  178. ????//list_node?*list_merged=merge_two_list();?//合并兩個鏈表??
  179. ????//list_print(list_merged->next);??????//打印合并后的鏈表??
  180. ??
  181. ????list_node?*list_merged2=foreach(list2);????//使用遞歸合并兩個鏈表??
  182. ????list_print(list_merged2->next);??
  183. ??
  184. ????list_destroy(list1);????????????????//銷毀鏈表??
  185. ????list_destroy(list2);????????????????//銷毀鏈表??
  186. //??list_destroy(list_merged);??????????//銷毀合并后的鏈表??
  187. ????list_destroy(list_merged2);?????????//銷毀合并后的鏈表??
  188. ????system("pause");??
  189. ????return?0;??
  190. }??


:如何刪除鏈表的倒數第m的元素?

分析構建p0,p1兩個迭代器,初始使p0和p1指向頭結點,接著使p1移動到第m+1項,然后指向頭得p0與p1同時前進,當p1指向空節點的時候結束,這時p0所指的位置就是倒數第m個,時間復雜度為O(n)

實現代碼:(為了學習的需要,現在C++朋友采用c++實現,不能滿足c朋友要采用c實現的愿望,此實為c++朋友的遺憾,不過一切重在思想。^_^

[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. #include?<iostream>??
  6. #include?<list>??
  7. using?namespace?std;??
  8. ??
  9. class?App??
  10. {??
  11. ????list<int>?*plist;???
  12. ??
  13. ????void?delete_node_at_last(int?m)?//today`s?topic??
  14. ????{??
  15. ????????list<int>::iterator?p0,?p1;??
  16. ????????p0=p1=p2=plist->begin();??
  17. ??
  18. ????????//使p1移到第m+1項??
  19. ????????for(int?i=1;?i<=m;?i++)??
  20. ????????????p1++;??
  21. ??
  22. ????????//p0和p1同時前進,當p1到達終點時p0所指向的就是倒數第m個節點??
  23. ????????while(p1!=plist->end())??
  24. ????????????p0++,p1++;??
  25. ??
  26. ????????//刪除節點??
  27. ????????plist->erase(p0);??
  28. ????}??
  29. ??
  30. ????void?create_list()??
  31. ????{??
  32. ????????int?list_data[]={1,2,3,4,5,6,7,8,9};?//鏈表數據??
  33. ????????plist?=?new?list<int>(list_data,?list_data+sizeof(list_data)/sizeof(int));??
  34. ????}??
  35. ??
  36. ????void?print_list()??
  37. ????{??
  38. ????????list<int>::iterator?it;??
  39. ????????for(it=plist->begin();?it!=plist->end();?it++)??
  40. ????????????cout<<*it<<'?';??
  41. ????}??
  42. ??
  43. public:??
  44. ????//test?in?run?funtion??
  45. ????void?run()??
  46. ????{??
  47. ????????create_list();??
  48. ????????delete_node_at_last(3);?//刪除倒數第三個節點??
  49. ????????print_list();??
  50. ????}??
  51. ??
  52. ????~App()??
  53. ????{??
  54. ????????delete?plist;??
  55. ????}??
  56. };??
  57. //Blog:http://blog.csdn.net/zhanxinhang??
  58. int?main()??
  59. {??
  60. App?myapp;??
  61. myapp.run();??
  62. system("pause");??
  63. return?0;??
  64. }?




:1、如何判斷一個字符串是對稱的?如a,aa,aba。? ?

? ? ? ? ?2、如何利用2函數找出一個字符串中的所有對稱子串?


分析

? ? ? ??看第一個問題判斷字符串對稱,有以下兩種方法。

? ? ? ? 方法一、使迭代器p1指向頭,p2指向尾。使p1,p2相對而行(—> | <),每次比較p1,p2是否相等,直到它們相遇。

? ? ? ??方法二、使p1、p2指向中間的一個元素(如果字符串長度為偶數的話就指向中間兩個相鄰的元素),使它們相向而行(<— |—>),每次比較p1,p2是否相等。直到它們一個指向頭一個指向尾便結束。??

(可以看出方法1明顯好于方法2)

? ? ? ??在看第二個問題打印所有的對稱子串,判斷對稱子串的問題我們似乎已經解決,且有以上兩種方法,針對現在的情況是否兩種都合適呢,確切的說不是。如果是方法一,請想象一下,如果要p1,p2一個指向子串的頭一個指向子串的尾,那么至少要兩層循環一層控制p1一層控制p2,還有一層循環用于判斷子串是否對稱,也就是說總共需要三層嵌套循環,時間復雜度指數為3。而如果選擇方法二呢? 兩層循環就能實現,不過要適應現在這種情況需要做些修改,就是針對偶數長度的子串進行一次遍歷,再針對奇數長度的子串進行一次遍歷,也就是兩層嵌套循環中內層有兩次循環,時間復雜度指數為2。詳情且看代碼。


實現加測試代碼:

[cpp] view plaincopy
  1. /**?
  2. Author:花心龜?
  3. Blog:http://blog.csdn.net/zhanxinhang?
  4. **/??
  5. ??
  6. #include?<iostream>??
  7. #include?<cstdlib>??
  8. #include?<ctime>??
  9. using?namespace?std;??
  10. class?App??
  11. {??
  12. ????typedef?string::iterator?Iter_t;??
  13. ??
  14. ????string?m_str;??
  15. ????void?print(Iter_t?p1,?Iter_t?p2)?//打印從p1開始到p2結束的字符??
  16. ????{??
  17. ????????while(p1<=p2)??
  18. ????????{??
  19. ????????????cout<<*p1;??
  20. ????????????p1++;??
  21. ????????}??
  22. ????????cout<<"?|?";??
  23. ????}??
  24. ????bool?isHuiwen1(Iter_t?start,Iter_t?end)?//法1判斷是否為對稱子串??
  25. ????{??
  26. ????????Iter_t?p1?=?start;??
  27. ????????Iter_t?p2?=?end;??
  28. ????????int?times?=?(distance(p1,p2)+1)?/?2;?//比較次數??
  29. ????????while(times)??
  30. ????????{??
  31. ????????????if(*p1?!=?*p2)??
  32. ????????????????return?false;??
  33. ??
  34. ????????????p1++;???
  35. ????????????p2--;???
  36. ????????????times--;??
  37. ????????}??
  38. ????????return?true;??
  39. ????}??
  40. ??
  41. ????bool?isHuiwen2(Iter_t?mid_it)?//法2判斷是否為對稱子串??
  42. ????{??
  43. ????????Iter_t?p1,p2;??
  44. ????????p1?=?p2?=?mid_it;??
  45. ??
  46. ????????while(p1>=m_str.begin()?&&?p2?<?m_str.end())???
  47. ????????{?????????????
  48. ????????????if(*p1?!=?*p2)???
  49. ????????????????break;????
  50. ????????????print(p1,p2);//打印從p1到p2的字符??
  51. ????????????p1--,p2++;??
  52. ????????}??
  53. ??
  54. ????????p1?=?p2?=?mid_it;??
  55. ????????p2++;?//使p2向前移動一個位置,此時p1,p2分別指向中間兩個相鄰位置??
  56. ????????while(p1>=m_str.begin()?&&?p2?<?m_str.end())????
  57. ????????{??
  58. ????????????if(*p1?!=?*p2)??
  59. ????????????????return?false;??
  60. ????????????print(p1,p2);//打印從p1到p2的字符??
  61. ????????????p1--,p2++;??
  62. ????????}??
  63. ????????return?true;??
  64. ????}??
  65. ??
  66. ????void?show_all_substr_of_huiwen1()?//法一打印所有對稱子串??
  67. ????{??
  68. ????????Iter_t?p2=m_str.end()-1;??
  69. ????????Iter_t?p1?=?m_str.begin();??
  70. ????????for(;p1!=m_str.end();p1++)??
  71. ????????????for(p2=p1;p2!=m_str.end();p2++)??
  72. ????????????{??
  73. ????????????????if(isHuiwen1(p1,p2))??
  74. ????????????????????print(p1,p2);??
  75. ????????????}??
  76. ????}??
  77. ??
  78. ????void?show_all_substr_of_huiwen2()??//法二打印所有對稱子串??
  79. ????{??
  80. ????????Iter_t?it?=?m_str.begin();??
  81. ??
  82. ????????for(;it!=m_str.end();it++)??
  83. ????????{??
  84. ????????????isHuiwen2(it);??
  85. ????????}??
  86. ????}??
  87. ??
  88. public:??
  89. ??
  90. ????void?run()??
  91. ????{??
  92. ????????m_str="abaaba";??
  93. ????????cout<<"測試數據為:abaaba"<<endl;??
  94. ????????show_all_substr_of_huiwen1();??
  95. ????????cout<<endl;??
  96. ????????show_all_substr_of_huiwen2();??
  97. ????????cout<<endl;??
  98. ??
  99. ????????m_str="asdfie";??
  100. ????????cout<<"測試數據為:asdfie"<<endl;??
  101. ????????show_all_substr_of_huiwen1();??
  102. ????????cout<<endl;??
  103. ????????show_all_substr_of_huiwen2();??
  104. ????????cout<<endl;??
  105. ??
  106. ????????m_str="aabaa";??
  107. ????????cout<<"測試數據為:aabaa"<<endl;??
  108. ????????show_all_substr_of_huiwen1();??
  109. ????????cout<<endl;??
  110. ????????show_all_substr_of_huiwen2();??
  111. ????????cout<<endl;??
  112. ??
  113. ????????//時間比較//??
  114. ????????m_str="this?is?a?string?for?testing.?aabaa?alskjdfkljasdjflasdflkajsldkjfsjlakjsdlfjwoekjlakjlsdkjflsajlkdjfowieuoriuq?aaddbb?sldjfalkjsdlfkjasldjflajsldfjalskdjflakjsdlfkjaslkdjflajsdlfkjaslkdjflkajsdlkjflkasjdlfjaklsjdkfljaklsdjfklsajdflkjslkdjflaskjdlfkjalsdjlfkajsldfkjlaksjdfljasldjflaskjdfkasjdflaksjdkfljaskldfjlaksjdfljasldjflaksjdkljfkalsjdlkfjasldjflasjdlfjasldjfklsajdfljaskldfjlsakjdflkasjdfkl?this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?is?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.this?is?a?string?for?testing.make?-k?make?-k?make?-k?make?-k?make?-k?make?-k?end";??
  115. ??
  116. ??
  117. ????????time_t?start,record1;??
  118. ??
  119. ??
  120. ????????cout<<"show?all?substr?of?huiwen?1:";??
  121. ????????system("pause");??
  122. ????????start?=?time(NULL);??????
  123. ????????show_all_substr_of_huiwen1();??
  124. ????????record1?=?time(NULL);??
  125. ??
  126. ????????cout<<endl;??
  127. ????????cout<<"time:"<<record1-start;??
  128. ????????cout<<endl;??
  129. ??
  130. ??
  131. ????????cout<<"show?all?substr?of?huiwen?2:";??
  132. ????????system("pause");??
  133. ????????start?=?time(NULL);??
  134. ????????show_all_substr_of_huiwen2();??
  135. ????????record1?=?time(NULL);??
  136. ??
  137. ????????cout<<endl;??
  138. ????????cout<<"time:"<<record1-start;??
  139. ????????cout<<endl;??
  140. ??
  141. ????????cout<<"(可以看到打印速度明顯快于上一次)";??
  142. ????}??
  143. };??
  144. ??
  145. int?main()??
  146. {??
  147. ????App?myapp;??
  148. ????myapp.run();??
  149. ??
  150. ????system("pause");??
  151. ????return?0;??
  152. }??

測試結果:

各測試數據下的一二行為打印出來的所有對稱字串。

按下任意鍵后:

以上是使用方法1打印出來的結果。

按下任意鍵后:

以上便是用方法2打印出來的結果。





假設你有一個用1001個整數組成的數組,這些整數是任意排列的,但是你知道所有的整數都在1到1000(包括1000)之間。此外,除一個數字出現兩次外,其他所有數字只出現一次。假設你只能對這個數組做一次處理,用一種算法找出重復的那個數字。如果你在運算中使用了輔助的存儲方式,那么你能找到不用這種方式的算法嗎?

分析

方法一使用輔助的存儲方式該選擇何種存儲方式呢可使用hash的存儲方式,以1到1000作為hash表的索引,遍歷原數組,統計各數字出現的個數并存儲到以該數字為索引值的hash表中,若某個hash[x]的值為2則退出循環,x就是重復出現兩次的數字。時間復雜度最壞是O(n)。優點:高效率,缺點:消耗的內存空間過大。代碼如下:

[cpp] view plaincopy
  1. int?fun1(const?int?a[])??
  2. {??
  3. ??int?hash[1002]={0};??
  4. ??int?x=0;??
  5. ??for(int?i?=?0;?i<1001;?i++)??
  6. ????{??
  7. ??????if((++hash[a[i]])?==?2)??
  8. ????????{??
  9. ??????????x?=?a[i];??
  10. ??????????break;??
  11. ????????}??
  12. ????}??
  13. ??return?x;??
  14. }??

方法二若不使用輔助的存儲方式呢1001個整數組成的數組只有一個數字出現了兩次,且整數都在1到1000之間,所以可推得數組里面包含了1到1000之間的所有數字為[1,2,3……1000]和一個出現兩次的x為1到1000中的任一個數字。這樣就可以計算原數組里的所有數字之和S1和等差數列[1,2,3……1000]的和S2,再計算S1與S2之差,該差就是原數組中出現兩次的數字x。時間復雜度是固定的O(n)。優缺點:內存空間消耗幾乎沒有,但是效率要輸于使用hash表的存儲方式。代碼如下:

[cpp] view plaincopy
  1. int?fun2(const?int?a[])??
  2. {??
  3. ??int?s1=0,s2;??
  4. ??s2?=?1001*1000/2;???
  5. ??for(int?i?=?0;?i<1001;?i++)??
  6. ????{??
  7. ??????s1+=a[i];??
  8. ????}??
  9. ??return?s1-s2;??
  10. }?





本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/453283.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/453283.shtml
英文地址,請注明出處:http://en.pswp.cn/news/453283.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

第八章 多態

第八章 多態1. 重寫一個類通過繼承來產生一個新類&#xff0c;繼承了父類的所有變量和方法&#xff0c;在繼承這些變量和方法的時候&#xff0c;子類也可以具有自己獨特的特征和行為。Public class fruit{Public void print(){System.out.println(“這是超類的方法”);}}Clas…

Ionic Angular自動捕獲錯誤 配置Angular2.x +

配置app.module.ts import { Pro } from ionic/pro;// These are the imports required for the code below, // feel free to merge into existing imports. import { Injectable, Injector } from angular/core; import { IonicErrorHandler } from ionic-angular;const Ioni…

信道和物理媒體的區別

一個信道可以包含很多的物理媒體嗎&#xff0c;同時一個物理媒體也可以包含很多的信道。 信道借助于物理媒體實現數據傳輸&#xff0c;在比較遠的數據傳輸過程中可能會使用多個不同的物理媒體實現數據的傳輸。 而一個物理媒體也可以借助于多路復用技術實現多條信道

c語言刪除尾部空格函數,新人提問:如何將輸出時每行最后一個空格刪除

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓如何將每行最后一個空格刪除&#xff0c;使矩陣只有數字間有空格&#xff0c;沒有多余空格&#xff1f;#include#includeint main(){int i,j,k,m,n,x,h,y;int a[15][15]{0};while(scanf("%d",&i)){k1;for(n1;n<i;…

jsonArray與 jsonObject區別與js取值

一、JSONObject和JSONArray的數據表示形式 JSONObject的數據是用 { } 來表示的&#xff0c; 例如&#xff1a; { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業", "content" : nu…

計劃任務 at,cron

示例&#xff1a;每3小時echo和wall命令 轉載于:https://www.cnblogs.com/momenglin/p/8551618.html

代碼疑云

代碼疑云(1)-掌握初始化列表 代碼&#xff1a; [cpp] view plaincopy#include<iostream> using namespace std; class A { private: int x1; int x2; public: A():x2(1),x1(x2){} //初始化列表 void print() { cout<<"x1"<&…

網絡擁塞

擁塞&#xff08;Congestion&#xff09;指的是在包交換網絡中由于傳送的包數目太多&#xff0c;而存貯轉發節點的資源有限而造成網絡傳輸性能下降的情況。擁塞的一種極端情況是死鎖&#xff08;Deadlock&#xff09;&#xff0c;退出死鎖往往需要網絡復位操作。

android 多線程future,多線程FutureTask的使用方法和使用實例

FutureTask是一種可以取消的異步的計算任務。它的計算是通過Callable實現的&#xff0c;它等價于可以攜帶結果的Runnable&#xff0c;并且有三個狀態&#xff1a;等待、運行和完成。完成包括所有計算以任意的方式結束&#xff0c;包括正常結束、取消和異常。Future有個get方法而…

2017.12.26

轉載于:https://www.cnblogs.com/dyh-air/p/8118961.html

mac 下安裝pip

pip是常用的python包管理工具&#xff0c;類似于java的maven。用python的同學&#xff0c;都離不開pip。 在新mac中想用home-brew安裝pip時&#xff0c;遇到了一些小問題&#xff1a; bogon:~ wanglei$ brew install pip Error: No available formula with the name "pip&…

IT職場人生系列

IT職場人生系列之一&#xff1a;序言及找誰占卜 本文是IT職場人生系列的第一篇。 時間流逝&#xff0c;漸漸從之前在公司里邊的小弟變成大哥了&#xff0c;當年身邊比我大的程序員們都不見了&#xff0c;既沒有當領導也沒有去創業&#xff0c;就這么消失了。 年輕的程序員或…

RS-232協議

計算機與計算機或計算機與終端之間的數據傳送可以采用串行通訊和并行通訊二種方式。由于串行通訊方式具有使用線路少、成本低&#xff0c;特別是在遠程傳輸時&#xff0c;避免了多條線路特性的不一致而被廣泛采用。 在串行通訊時&#xff0c;要求通訊雙方都采用一個標準接口&am…

linux sed 找出前后三行,Linux Sed 使用示例

環境&#xff1a;CentOS鑒于語句描述蒼白無力&#xff0c;用例子直接說明。mytxt文件內容&#xff1a;zilzhang 19881110 jiangxi 18 filmzhagnsan 21321 sichuan 100 cardlisi 3435 hunan 65 TV1. 找出文件第二行$ sed -n ‘2p‘ mytxtzhagnsan 21321 sichua…

MessageBox 彈框

模擬系統的消息提示框而實現的一套模態對話框組件&#xff0c;用于消息提示、確認消息和提交內容。 從場景上說&#xff0c;MessageBox 的作用是美化系統自帶的 alert、confirm 和 prompt&#xff0c;因此適合展示較為簡單的內容。如果需要彈出較為復雜的內容&#xff0c;請使用…

什么是同軸電纜

同軸電纜從用途上分可分為基帶同軸電纜和寬帶同軸電纜&#xff08;即網絡同軸電纜和視頻同軸電纜&#xff09;。同軸電纜分50Ω 基帶電纜和75Ω寬帶電纜兩類。基帶電纜又分細同軸電纜和粗同軸電纜。基帶電纜僅僅用于數字傳輸&#xff0c;數據率可達10Mbps。同軸電纜(Coaxial Ca…

android textview表情,Android開發(16)-TextView顯示表情圖像和文字

從這個案例中我們可以學到當我們美化圖片美化界面的時候可以在某一區域輸入圖片和文字混搭信息,第三張圖片按比例縮小&#xff0c;第四張圖像有超鏈接布局文件MainActivity.javapackage com.example.textview3;import java.lang.reflect.Field;import android.os.Bundle;import…

Rating

題目鏈接 題意&#xff1a; 起始狀態是&#xff08;0。0&#xff09;&#xff0c;每次轉移的時候都是對兩個數中的較小的數操作。1&#xff09;以概率p轉向&#xff08;min&#xff08;a 50&#xff0c;1000&#xff09;。b&#xff09; 2&#xff09;以概率1-p轉向&#x…

linux的apache2.4限定某個目錄禁止解析PHP及user_agent與PHP相關配置

限定某個目錄禁止解析PHP 對于使用PHP語言編寫的網站&#xff0c;有一些目錄是有需求上傳文件的&#xff0c;比如服務器可以上傳圖片&#xff0c;并且沒有做防盜鏈&#xff0c;所以就會被人家當成了一個圖片存儲服務器&#xff0c;并且盜用帶寬流量。如果網站代碼有漏洞&#x…

什么是光纜

光纜(optical fiber cable)是為了滿足光學、機械或環境的性能規范而制造的&#xff0c;它是利用置于包復護套中的一根或多根光纖作為傳輸媒質并可以單獨或成組使用的通信線纜組件。光纜主要是由光導纖維&#xff08;細如頭發的玻璃絲&#xff09;和塑料保護套管及塑料外皮構成&…