剑指offer

柔情痞子 提交于 2020-02-15 17:23:04

1、在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

 1 class Solution {
 2 public:
 3     bool Find(int target, vector<vector<int> > array) {
 4  
 5         int row = (int)array.size();
 6         int col = (int)array[0].size();
 7         if (row == 0 || col == 0)
 8             return false;
 9         if (target < array[0][0] || target > array[row - 1][col - 1])
10             return false;
11         int i = row - 1;
12         int j = 0;
13         while (i >=0 && j <col)
14         {
15             if (array[i][j] > target)
16             {
17                 i--;
18             }
19             else if (array[i][j] < target)
20             {
21                 j++;
22             }
23  
24             else
25             
26                 return true;
27             
28         }
29          return false;
30     }
31 };

2、请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

 1 class Solution {
 2 
 3 public:
 4     void replaceSpace(char *str,int length) {
 5         int i=length-1,j,count=0;
 6         while(i>=0){
 7             if(str[i]==' '){
 8                 for(j=length;j>=i;j--){
 9                     str[j+2]=str[j];
10                 }
11                 str[i]='%';
12                 str[i+1]='2';
13                 str[i+2]='0';
14                 length+=2;
15             }
16              i--;
17         }
18     }
19 
20     
21 };

3、输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

 1 class Solution {
 2 public:
 3     vector<int> printListFromTailToHead(ListNode* head) {
 4            vector <int>  result;
 5       stack<int> arr;
 6       ListNode* p=head;
 7       while(p!=NULL)
 8       {
 9        arr.push(p->val);
10        p=p->next;
11       }
12      int len=arr.size();
13      for(int i=0;i<len;i++)
14       {
15        result.push_back(arr.top());   
16        arr.pop();
17      }
18            return  result;
19         
20         
21     }
22 };

 4、输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

 1 class Solution {
 2 public:
 3     TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
 4        int s = vin.size();
 5         if(s==0)
 6             return NULL;
 7         vector<int> pre_left,pre_right,vin_left,vin_right;
 8         TreeNode* node = new TreeNode(pre[0]);
 9         int index = 0;
10         for(int i=0;i<s;i++){
11             if(vin[i]==pre[0]){
12                 index = i;
13                 break;
14             }
15          }
16        for(int i=0;i<index;i++){
17             pre_left.push_back(pre[i+1]);
18             vin_left.push_back(vin[i]);
19         }
20         for(int i=0;i<s-index-1;i++){
21             pre_right.push_back(pre[i+index+1]);
22             vin_right.push_back(vin[i+index+1]);
23         }
24         
25  
26 /**for(int i = 0; i < s; ++i){          
27     if(i < index){              
28         vin_left.push_back(vin[i]);//Construct the left pre and in       
29         pre_left.push_back(pre[i+1]);    
30     }            
31     else if(i > index){            
32         vin_right.push_back(vin[i]);//Construct the right pre and in           
33         pre_right.push_back(pre[i]);           
34     }       
35 }*/
36         node->left = reConstructBinaryTree(pre_left,vin_left);
37         node->right = reConstructBinaryTree(pre_right,vin_right);
38         return node;
39 
40     }
41 };

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!