1.替换空格
2.二维数组的查找
3.替换字符串、删除字符串
4.冒泡排序
5.数组倒序
6.二分查找
1.替换空格
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
class Solution {
public:
void replaceSpace(char *str, int length) {
vector <char> vec;
int i, j;
for (int i; str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
vec.push_back('%');
vec.push_back('2');
vec.push_back('0');
}
else
vec.push_back(str[i]);
}
for (j = 0; j < vec.size(); j++)
{
str[j] = vec[j];
}
str[j] = '\0';
}
};
思路:构建一个容器vector,压入想要的字符。
2.二维数组中的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
class Solution {
public:
bool Find(vector<vector<int> > array,int target) {
int rowCount = array.size();
int colCount = array[0].size();
int i,j;
for(i=rowCount-1,j=0;i>=0&&j<colCount;)
{
if(target == array[i][j])
return true;
if(target < array[i][j])
{
i--;
continue;
}
if(target > array[i][j])
{
j++;
continue;
}
}
return false;
}
};
思路
矩阵是有序的,从左下角来看,向上数字递减,向右数字递增,
因此从左下角开始查找,当要查找数字比左下角数字大时。右移
要查找数字比左下角数字小时,上移
3.替换字符串、删除字符串
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string find[4] = { "ctve","cTvE","CTVE","ctVE" };
string str;
string c = "****";
cin >> str;
int val=0,num=0;
val = str.find(find[num]);
while (num != 4) {
while (val != -1) {
str.replace(val, find[num].length(), c);
val = str.find(find[num]);
}
val = 0;
num++;
}
cout << str << endl;
system("Pause");
return 0;
}
用到了find函数和replace函数
find返回找到匹配的字符串的指针
删除指定字符串:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string find[4] = { "ctve","cTvE","CTVE","ctVE" };
string str;
string c = "****";
cin >> str;
int val=0,num=0;
while (num != 4) {
val = str.find(find[num]);
while (val != -1) {
str=str.erase(val, 4);
val = str.find(find[num]);
}
val = 0;
num++;
}
cout << str << endl;
system("Pause");
return 0;
}
basic_string & erase(size_type pos=0, size_type n=npos);
即从给定起始位置pos处开始删除, 要删除字符的长度为n, 返回值修改后的string对象引用
4.冒泡排序(小-大)
#include <iostream>
using namespace std;
int main()
{
int a[11] = { 1,3,9,4,5,10,2,6,3,9,4 };
int i,j,temp;
for(j=0;j<10;j++)
for (i = 0; i < 10; i++)
{
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
for (i = 0; i < 11; i++)
{
cout << a[i] << " ";
}
system("Pause");
return 0;
}
5.数组倒序
#include <iostream>
using namespace std;
int main()
{
int a[11] = { 1,3,9,4,5,10,2,6,3,9,4 };
int i,temp;
for (i = 0; i < 11/2; i++)
{
temp = a[i];
a[i] = a[10-i];
a[10-i] = temp;
}
for (i = 0; i < 11; i++)
{
cout << a[i] << " ";
}
system("Pause");
return 0;
}
6.二分查找
#include <iostream>
using namespace std;
int bansearch(int *p, int key)
{
int low = 0,high = 7,mid=0;
while (key != p[mid])
{
mid = (low + high) / 2;
if (key > p[mid])low = mid + 1;
if (key < p[mid])high = mid - 1;
if (low > high)return 0;
}
return mid + 1;
}
int main()
{
int a[7] = { 1,3,9,4,5,10,2 };
int i, j, temp,k;
cin>>k;
//排序 小--大
for (j = 0; j < 6; j++)
for (i = 0; i < 6; i++)
{
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
for (i = 0; i < 7; i++)
{
cout << a[i] << " ";
}
i=bansearch(a, k);
cout << "成功,位置在: " << i;
system("Pause");
return 0;
}