1 .在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
class Solution{ public: bool Find(int target,vector<vector<int>>& matrix){ if(matrix.empty()) return false; bool found = false; int m = matrix.size(); int n = matrix[0].size(); int row = 0; int col = n -1; while(row < m && col >= 0){ if(matrix[row][col] == target){ found = true; break; }else if (target > matrix[row][col]){ row++; }else { col--; } } return found; } };
2.请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
class Solution { public: void replaceSpace(char *str, int length) { int spaceCount = 0; for (int i = 0; i < length; i++) { if (str[i] == ' ') { spaceCount++; } } if (spaceCount == 0) return; int p = length - 1; int q = length + spaceCount * 2 - 1; while (p < q) { if (str[p] != ' ') { str[q--]=str[p--]; }else{ p--; str[q--] = '0'; str[q--] = '2'; str[q--] = '%'; } } str[length+spaceCount*2] = '