剑指offer第二章
1.二维数组中的查找
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数
1 class Solution { 2 public: 3 bool Find(int target, vector<vector<int> > array) 4 { 5 /*二维数组的行数和列数*/ 6 int rows = array.size(); 7 int columns = array[0].size(); 8 9 int row; 10 int column; 11 for (row = rows - 1, column = 0; row >= 0 && column<columns;) 12 { 13 if (target == array[row][column]) 14 return true; 15 if (target<array[row][column]) 16 { 17 row--; 18 continue; 19 } 20 if (target>array[row][column]) 21 { 22 column++; 23 continue; 24 } 25 } 26 return false; 27 } 28 };
2.替换空格
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
1 class Solution { 2 public: 3 void replaceSpace(char *str,int length) 4 { 5 if(str==NULL||length<0) 6 { 7 return; 8 } 9 int originalLength=0; 10 int numberOfBlank=0; 11 int i=0; 12 while(str[i]!='