ZZ15道简单算法题

Posted 浩然

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZZ15道简单算法题相关的知识,希望对你有一定的参考价值。

15道简单算法题

http://www.cnblogs.com/hlxs/archive/2014/06/06/3772333.html

(●—●) | 剑指Offer_编程题_牛客网

http://www.nowcoder.com/ta/coding-interviews?page=1

source code

https://github.com/haotang923/interview/tree/master/15%20simple%20algorithm%20problems

 

1:合并排序,将两个已经排序的数组合并成一个数组,其中一个数组能容下两个数组的所有元素;

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Solution 
 5 {
 6 public:
 7     void MergeArray(int a[],int alen,int b[],int blen)
 8     {
 9          int len = alen + blen - 1;
10          alen --;
11          blen --;
12          
13          // 用归并排序的思想把两个数组合并 
14          while (alen >= 0 && blen >= 0)
15          {
16                if (a[alen] > b[blen])
17                   a[len --] = a[alen --];
18                else
19                   a[len --] = b[blen --];
20          }
21          
22          while (blen >= 0)
23                a[len --] = b[blen --];
24     }
25 };
26 
27 int main ()
28 {
29     Solution testSolution;
30     int arrA[] = {1, 2, 4, 6, 8, 0, 0, 0, 0, 0};
31     int arrB[] = {1, 3, 5, 7, 9};
32       
33     testSolution.MergeArray(arrA, 5, arrB, 5);
34     
35     for (int i = 0; i < 10; i ++)
36         cout << arrA[i] << endl;
37         
38     getchar();
39     
40     return 0;
41 }
View Code

 

2:合并两个单链表;

  • C++11最好用nullptr初始化指针得到空指针,同时尽量避免使用NULL。
    • https://msdn.microsoft.com/zh-cn/library/4ex65770.aspx
  • 复习struct,链表也不熟悉了,用当前节点还是next节点的时候出了几次错。
  • struct_百度百科
    • http://baike.baidu.com/link?url=PxVQxXElF_rtYMqYhfNBzsjvTl9cG2PSKTqFZGV3IBAEHosImlkuaSMqCIxnShEqtDD4xA8gISTtP7rS7TENj_
  • struct (C++)
    • https://msdn.microsoft.com/zh-cn/library/64973255.aspx
  1 #include <iostream>
  2 using namespace std;
  3 
  4 struct ListNode 
  5 {
  6        int val;
  7        ListNode *next;
  8        ListNode(int x) : val(x), next(NULL) {}
  9 };
 10  
 11 class Solution 
 12 {
 13 public:
 14     ListNode* MergeList(ListNode* aListNode, ListNode* bListNode)
 15     {
 16          // NULL case
 17          if (aListNode == NULL)
 18             return bListNode;
 19          if (bListNode == NULL)
 20             return aListNode;
 21 
 22          ListNode* resHead = NULL;         
 23 
 24          if (aListNode->val < bListNode->val) {
 25             resHead = aListNode;
 26             aListNode = aListNode->next;
 27          } else {
 28             resHead = bListNode;
 29             bListNode = bListNode->next;
 30          }
 31          
 32          // Reserve the head point         
 33          ListNode* tmpHead = resHead;
 34 
 35 //         PrintListNode(tmpHead);
 36          
 37          // Merge two LinkList
 38          // Remember to check if the current node is NULL
 39          // assign ListNode to next resHead->next
 40          // move resHead to the next ptr
 41          while (aListNode != NULL && bListNode != NULL)
 42          {
 43                if (aListNode->val < bListNode->val) {
 44                   resHead->next = aListNode;
 45                   aListNode = aListNode->next;
 46                } else {
 47                   resHead->next= bListNode;
 48                   bListNode = bListNode->next;
 49                }
 50                resHead = resHead->next;
 51          }
 52 
 53 //         PrintListNode(tmpHead);
 54 
 55          if (aListNode != NULL)
 56             resHead->next= aListNode;
 57             
 58          if (bListNode != NULL)
 59             resHead->next= bListNode;
 60          
 61 //         PrintListNode(tmpHead);
 62 
 63          return tmpHead;
 64     }
 65     
 66     void PrintListNode(ListNode* inListNode)
 67     {
 68          ListNode* tmpLN = inListNode;
 69          
 70          while (tmpLN != NULL)
 71          {
 72                cout << tmpLN->val << endl;
 73                tmpLN = tmpLN->next;
 74          }         
 75          cout << endl;
 76     }
 77 };
 78 
 79 int main ()
 80 {
 81     Solution testSolution;
 82     ListNode* aHead = new ListNode(1);
 83     ListNode* cur = aHead;
 84     
 85     for (int i = 2; i < 10; i += 2)
 86     {
 87         ListNode* tmpLN = new ListNode(i);
 88         cur->next = tmpLN;
 89         cur = tmpLN;        
 90     }
 91     
 92 //    testSolution.PrintListNode(aHead);
 93     
 94     ListNode* bHead = new ListNode(1);
 95     cur = bHead;
 96     
 97     for (int i = 3; i < 10; i += 2)
 98     {
 99         ListNode* tmpLN = new ListNode(i);
100         cur->next = tmpLN;
101         cur = tmpLN;
102     }
103       
104 //    testSolution.PrintListNode(bHead);
105 
106     ListNode* head = testSolution.MergeList(aHead, bHead);
107 
108 //    testSolution.PrintListNode(head);
109 
110     while (head != NULL)
111     {
112           cout << head->val << endl;
113           head = head->next;
114     }
115         
116     getchar();
117     
118     return 0;
119 }
View Code

 

3:倒序打印一个单链表;

  • 递归
  • 链表的题目还得多练,又弄错了打印head还是cur节点。。。
 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct ListNode 
 5 {
 6        int val;
 7        ListNode *next;
 8        ListNode(int x) : val(x), next(NULL) {}
 9 };
10  
11 class Solution 
12 {
13 public:
14     void PrintListNodeReversely(ListNode* inListNode)
15     {
16          if (inListNode != NULL)
17          {
18             PrintListNodeReversely(inListNode->next);
19             cout << inListNode->val << endl;
20          }         
21     }
22 };
23 
24 int main ()
25 {
26     Solution testSolution;
27     ListNode* head = new ListNode(0);
28     ListNode* cur = head;
29     
30     for (int i = 1; i < 10; i ++)
31     {
32         ListNode* tmpLN = new ListNode(i);
33         cur->next = tmpLN;
34         cur = cur->next; // cur->next == tmpLN        
35     }
36 
37     // Print head node        
38     testSolution.PrintListNodeReversely(head);
39         
40     getchar();
41     
42     return 0;
43 }
View Code

 

4:给定一个单链表的头指针和一个指定节点的指针,在O(1)时间删除该节点;

  • http://www.cnblogs.com/pegasus923/archive/2010/10/04/1841891.html
  • 重温链表插入删除操作,注意原文代码删除头结点处有问题,函数对头指针只是值传递,如果要删除需要传入头指针的指针。
  1 #include <iostream>
  2 using namespace std;
  3 
  4 struct ListNode 
  5 {
  6        int val;
  7        ListNode *next;
  8        ListNode(int x) : val(x), next(NULL) {}
  9 };
 10  
 11 class Solution 
 12 {
 13 public:
 14     void PrintListNode(ListNode* inListNode)
 15     {
 16          if (inListNode != NULL) {
 17             cout << inListNode->val << endl;
 18             PrintListNode(inListNode->next);
 19          } else
 20             cout << endl;
 21     }
 22 
 23     void Test(ListNode* inListNode)
 24     {
 25          delete inListNode;
 26          inListNode = NULL;
 27          cout << inListNode << endl;
 28 
 29          PrintListNode(inListNode);
 30     }
 31     
 32     void DeleteNode(ListNode* pInHead, ListNode* pToBeDeleted)
 33     {
 34          cout << "Entry void DeleteNode(ListNode* pInHead, ListNode* pToBeDeleted)\\n" << endl;
 35 
 36          // Check NULL always
 37          if (pInHead == NULL || pToBeDeleted == NULL)
 38             return;
 39          
 40 //         PrintListNode(pInHead);
 41          
 42          // Delete non-tail node including head node
 43          if (pToBeDeleted->next != NULL) {
 44             ListNode* pNext = pToBeDeleted->next;
 45             pToBeDeleted->val = pNext->val;
 46             pToBeDeleted->next = pNext->next;
 47             
 48             delete pNext;
 49             pNext = NULL;
 50          } else { // Delete tail
 51             ListNode* pPre = pInHead;
 52          
 53             while (pPre->next != pToBeDeleted && pPre != NULL) 
 54                   pPre = pPre->next;
 55          
 56             if (pPre == NULL)
 57                return;
 58          
 59             pPre->next = NULL;
 60             delete pToBeDeleted;
 61             pToBeDeleted = NULL;
 62          }                  
 63     }
 64 };
 65 
 66 int main ()
 67 {
 68     Solution testSolution;
 69     int count = 5;
 70  
 71     for (int k = 0; k <= count; k ++)
 72     {
 73         ListNode* pHead = NULL;
 74         ListNode* pCur = NULL;
 75         ListNode* pDel = NULL;
 76         
 77         for (int i = 0; i < count; i ++)
 78         {
 79             ListNode* pTemp = new ListNode(i);
 80             
 81             if (i == 0)
 82                 pHead = pCur = pTemp;
 83             else {
 84                 pCur->next = pTemp;
 85                 pCur = pCur->next; // pCur->next == pTemp
 86             }
 87             
 88             if (i == k) 
 89                pDel = pTemp;
 90         }
 91            
 92         cout << "pHead = " << pHead << endl;
 93         if (pHead != NULL)
 94            cout << "pHead->val = " << pHead->val << endl;
 95         cout << "pDel = " << pDel << endl;
 96         if (pDel != NULL)
 97            cout << "pDel->val = " << pDel->val << endl;
 98         cout << ((pHead == pDel) ? "pHead == pDel" : "pHead != pDel") << endl;
 99         cout << endl;
100 
101        // Print Linkedlist
102         testSolution.PrintListNode(pHead);        
103          
104         testSolution.DeleteNode(pHead, pDel);
105 //        testSolution.Test(pHead);
106 
107 /*
108         delete pDel;
109         pDel = NULL;
110         pHead = NULL;
111 */        
112         cout << "pHead = " << pHead << endl;
113         if (pHead != NULL)
114            cout << "pHead->val = " << pHead->val << endl;
115         cout << "pDel = " << pDel << endl;
116         if (pDel != NULL)
117            cout << "pDel->val = " << pDel->val << endl;
118         cout << endl;
119 
120         // Print Linkedlist
121         testSolution.PrintListNode(pHead);        
122     }   
123         
124     getchar();
125     
126     return 0;
127 }
View Code

 

5:找到链表倒数第K个节点; K=0时,返回链表尾元素。

  • 两个指针同时往后走,之间相隔K个元素。一个到达尾部时,另一个正好为倒数第K个。
  • 注意边界情况,头指针为NULL,K为0,K大于链表长度。
  1 #include <iostream>
  2 using namespace std;
  3 
  4 struct ListNode 
  5 {
  6        int val;
  7        ListNode *next;
  8        ListNode(int x) : val(x), next(NULL) {}
  9 };
 10  
 11 class Solution 
 12 {
 13 public:
 14     void PrintListNode(ListNode* inListNode)
 15     {
 16          if (inListNode != NULL) {
 17             cout << inListNode->val << endl;
 18             PrintListNode(inListNode->next);
 19          } else
 20             cout << endl;
 21     }
 22     
 23     ListNode *findMToLastElement(ListNode* pInHead, int m)
 24     {
 25          cout << "Entry ListNode *findMToLastElement(ListNode* pInHead, int m)\\n" << endl;
 26          
 27          ListNode *pCur = NULL, *pMPre = NULL;
 28          
 29          // Corner case
 30          if (pInHead == NULL) return NULL;
 31          
 32          pCur = pMPre = pInHead;
 33 
 34          // Move pCur forward m elements
 35          for (int i = 0; i < m; i ++)
 36          {
 37              if (pCur->next != NULL)
 38                 pCur = pCur->next;
 39              else
 40                 return NULL;
 41          }
 42 
 43          // Move forward together until pCur reach the tail
 44          while (pCur->next != NULL)
 45          {
 46                pCur = pCur->next;
 47                pMPre = pMPre->next;               
 48          }
 49          
 50          // Now pMPre points to the M to last element
 51          return pMPre;
 52     }
 53 };
 54 
 55 int main ()
 56 {
 57     Solution testSolution;
 58     int count = 10;
 59     
 60     ListNode *pHead = NULL;
 61     ListNode *pCur = NULL;
 62     
 63     for (int i = 0; i < count; i ++)
 64     {
 65         ListNode *pTemp = new ListNode(i);
 66         
 67         if (i == 0)
 68             pHead = pCur = pTemp;
 69         else {
 70             pCur->next = pTemp;
 71             pCur = pCur->next; // pCur->next == pTemp
 72         }
 73     }
 74     
 75     // Print Head       
 76     cout << "pHead = " << pHead << endl;
 77     if (pHead != NULL)
 78        cout << "pHead->val = " << pHead->val << endl;
 79     cout << endl;
 80     
 81     // Print Linkedlist
 82     cout << "PrintListNode(pHead)" << endl;
 83     testSolution.PrintListNode(pHead);        
 84 
 85     cout << "Test findMToLastElement(pHead, 0)\\n" << endl;     
 86     ListNode *pMToLastElement = testSolution.findMToLastElement(pHead, 0);
 87     
 88     // Print Head       
 89     cout << "pHead = " << pHead << endl;
 90     if (pHead != NULL)
 91        cout << "pHead->val = " << pHead->val << endl;
 92     cout << endl;
 93     
 94     // Print M to last element
 95     cout << "pMToLastElement = " << pMToLast

以上是关于ZZ15道简单算法题的主要内容,如果未能解决你的问题,请参考以下文章

给秋招加点料——Hot15道高频算法面试题!

给秋招加点料——Hot15道高频算法面试题!

每日刷题3.135道算法+15道面试 - 阿V

经典算法题 :递归快速排序复杂度等5选择题道题

LeetCode刷题计划

LeetCode刷题计划