86. Partition List

题目

分析:题目要求将链表划分为两部分,前半部分小于x,后半部分大于等于x,并且各个数之间的相对顺序不变。

解题思路是:从头开始扫描链表,找打第一个大于等于x的数current,然后从这个数开始,把current之后的小于x的数依次插在current前,大于等于x的数不变;为了实现插入操作,可以新建一个带头节点的链表。

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* partition(ListNode* head, int x) {
12         if(NULL == head)
13             return NULL;
14         
15         ListNode *pHead = new ListNode(0);//新创建一个链表
16         ListNode *pre = pHead;//添加一个头节点
17         
18         ListNode *current = head;
19         while(current != NULL)//找到第一个大于等于x的节点
20         {
21             if(current->val <x)
22             {
23                 pre->next = new ListNode(current->val);//小于x的节点,原封不动的copy到新链表中
24                 pre = pre->next;
25             }
26             else
27                 break;
28             current = current->next;
29                 
30         }
31         ListNode *temp = current;
32         
33         while(temp!=NULL)//处理current之后小于x的节点
34         {
35             if(temp->val <x)
36             {
37                 pre->next = new ListNode(temp->val);
38                 pre = pre->next;
39             }
40             temp = temp->next;
41         }
42         temp = current;
43         while(temp!=NULL)//处理current之后大于等于x的节点
44         {
45             if(temp->val >=x)
46             {
47                 pre->next = new ListNode(temp->val);
48                 pre = pre->next;
49             }
50             temp = temp->next;
51         }
52         
53         return pHead->next;
54         
55         
56     }
57 };

 ----------------------------------------------------------------------分割线----------------------------------------------------------------------------

88. Merge Sorted Array

题目

分析:注意题目的空间复杂度的要求

代码:

 1 class Solution {
 2   public:
 3       void merge(int A[], int m, int B[], int n) {
 4 
 5           int i,j;
 6           int index = m+n-1;
 7 
 8           if (0 == n)
 9           {
10               return ;
11           }
12 
13           if (0 == m && 0 != n)
14           {
15               for (i=0;i<n;i++)
16               {
17                   A[i]=B[i];
18               }
19               return ;
20           }
21 
22           for(i=m-1,j=n-1;index>=0;)
23           {
24               if (A[i]>B[j])
25               {
26                   A[index]=A[i];
27                   i--;
28               }
29               else
30               {
31                   A[index]=B[j];
32                   j--;
33               }
34               index--;
35               if (j<0||i<0)
36               {
37                   break;
38               }
39 
40 
41           }
42            if (i<0)
43           {
44               for (i=j;i>=0;i--)
45               {
46                   A[index]=B[i];
47                   index--;
48               }
49           }
50 
51       }
52   };

 -----------------------------------------------------------------------分割线------------------------------------------------------------

89. Gray Code

题目

分析:主要理解格雷码的镜像构造法

代码

 1 class Solution {
 2 public:
 3     vector<int> grayCode(int n) {
 4         //if(n<=)
 5         vector<int> res;
 6         res.push_back(0);
 7         int count=1;
 8         int index;
 9         int temp;
10         unsigned int x;
11         while(count<=n)
12         {
13             x = (1 << (count-1));
14             index = res.size()-1;
15             while(index>=0)
16             {
17                 temp = res[index];
18                 temp = temp|x;
19                 res.push_back(temp);
20                 index--;
21             }
22             count++;
23         }
24         return res;
25         
26     }
27 };