1.替换空格
- *str='a’不能改变str执行的地址的值
- str[3]:返回Index为3之后的子串
- char指针作为数组结束符有\0,len中不包含,故结尾下标为length
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
/*1.不能新定义字符串,因为题目要求为替换
*2.*str='a'不能改变str执行的地址的值
*3.str[3]:返回Index为3之后的子串
**思路:从前往后遍历记录空字符的个数;从后往前插入(后面的往后移动)
*/
void replaceSpace(char *str, int length) {
int count = 0;//记录空字符的个数
for (int i = 0; i < length; i++) {
if (str[i] == ' ')
count++;
}
//所以需要向后移动2*count位
/*char指针作为数组结束符有\0,len中不包含,故结尾下标为length*/
for (int i = length ; i--; i >=0 ) {
if (str[i] == ' ') {
count--;
str[i+2*count] = '%';
str[i+2*count + 1] = '2';
str[i+2*count + 2] = '0';
}
else {
str[i + 2*count] = str[i];
}
}
}
int main()
{
char str[30] = "i love china";
cout << str << "\n";
cout << strlen(str) << "\n";
replaceSpace(str, strlen(str));
cout << str << endl;
return 0;
}
2.旋转数组的最小数字
- (1)驼峰数组:利用性质-判断是否被旋转+二分法找到pivot,否则超出计算
- **(2)注意判断当数组为空和长度为1的情况 **
- **(3)end和start的更新不能用pivot+1/-1;因为循环是用start<=end判断,±1会错过最优值 **
- (4)不能写小于!!因为数组可能有重复
#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
/*驼峰数组-leetcode
int minNumberInRotateArray(vector<int> rotateArray) {
int re;
if (rotateArray.size() == 0) {
return 0;
}
else {
int pre = rotateArray[0];
for (int i = 1; i<= rotateArray.size()-1; i++) {
if(pre>=rotateArray[i]){
re = rotateArray[i];
return re;
}
}
return 0;
}
}*/
/*
*(1)驼峰数组:利用性质-判断是否被旋转+二分法找到pivot,否则超出计算
*(2)注意判断当数组为空和长度为1的情况
*(3)end和start的更新不能用pivot+1/-1;因为循环是用start<=end判断,+-1会错过最优值
*(4)不能写小于!!因为数组可能有重复
*/
int minNumberInRotateArray(vector<int> rotateArray) {
if (rotateArray.empty() || rotateArray.size() == 0)
return 0;
if (rotateArray.size() == 1)//先判断是否被旋转
return rotateArray[0];
if (rotateArray[0] < rotateArray[rotateArray.size() - 1])//未被旋转已经排好序了,不能写小于!!
return rotateArray[0];
//找pivot
else {
int start = 0, end = rotateArray.size() - 1, pivot;
while (start <= end) {
pivot = (start + end) / 2;
if (pivot == start || pivot == end)
break;
if (rotateArray[pivot] > rotateArray[end])//大于后面最大,pivot在mid和end之间
start = pivot;
if (rotateArray[pivot] < rotateArray[start])//小于前面最小,pivot在start和mid之间
end = pivot;
}
if (rotateArray[start] <= rotateArray[end])
return rotateArray[start];
return rotateArray[end];
}
}
int main()
{
vector<int> rote = {1};
cout << minNumberInRotateArray(rote);
return 0;
}
3.整数n的二进制中1的个数
- bitset<32> bits(n):将n以二进制形式存储
- n=n&(n-1):将n的最右边第一位1改为0
class Solution {
public:
int NumberOf1(int n) {
//方法1:直接用bitsit
//bitset<32> bits(n);//将n存成二进制
//return bits.count();
//方法二 n=n&(n-1) 把n中右边第一位1变为0
int count = 0;
while (n!=0) {
count++;
n = n & (n - 1);
}
return count;
}
};
4.调整数组顺序,使得奇数位于偶数前面
- 相对位置不变:排序的稳定性,冒泡排序(倒着的)
class Solution {
public:
//相对位置不变:排序的稳定性,冒泡排序(倒着的)
void reOrderArray(vector<int> &array) {
for (int i = 0; i < array.size(); i++) {
for (int j = array.size()-1; j>i ; j--) {
if (array[j] % 2 != 0 && array[j-1]%2==0) { //奇数
swap(array[j], array[j-1]);
}
}
}
}
};
5.从外围开始打印矩阵
- 多画图,理清楚逻辑关系
class Solution {
public:
//逆时针打印矩阵--遇到vector一定要考虑大小!
vector<int> printMatrix(vector<vector<int> > matrix) {
int row = matrix.size();
int col = matrix[0].size();
vector<int> results;
int k=0,count=row*col;//第k次
//确定外围的长度
while (count>0) {
for (int i = k; i < col-k; i++) {
results.push_back(matrix[k][i]);
count--;
if(count<=0)
return results;
}
for (int j = k+1; j < row - k; j++) {
results.push_back(matrix[j][col - k-1]);
count--;
if(count<=0)
return results;
}
for (int i = col - k-2; i >= k; i--) {
results.push_back(matrix[row - k-1][i]);
count--;
if(count<=0)
return results;
}
for (int i = row-k-2; i > k; i--) {
results.push_back(matrix[i][k]);
count--;
if(count<=0)
return results;
}
k=k+1;
}
return results;
}
};