1.找出数组中重复的数字
bool duplicate(int numbers[], int length, int *duplication)
{
if(numbers == nullptr || length <= 0)
return false;
for(int i = 0; i < length; ++i)
{
if(numbers[i] < 0 || numbers[i] > length - 1)
return false;
}
for(int i = 0; i < length; ++i)
{
while(numbers[i] != i)
{
if(numbers[i] == numbers[numbers[i]])
{
*duplication = numbers[i];
return true;
}
int temp = numbers[i];
numbers[i] = numbers[numbers[i]];
numbers[numbers[i]] = temp;
}
}
return false;
}
2.不修改数组找到重复的数字
int getDuplication(const int* numbers,int length){
//校验输入参数的有效性
if(numbers==nullptr||length<0){
return -1;
}
for(int i=0;i<length;i++){
if(numbers[i]<1||numbers[i]>length-1){
return -1;
}
}
//二分查找重复元素
int start=1;
int end=length-1;
while(end>=start){
int middle=((end-start)>>1)+start;
int count=countRange(numbers,length,start,middle);
//区间上下限相等
if(end==start){
//元素数量大于1,成功查找
if(count>1)
return start;
else
break;
}
//区间上下限不等,继续二分查找
if(count>(middle-start+1))
end=middle;
else
start=middle+1;
}
return -1;
}
//获取区间元素个数
int countRange(const int* numbers,int length,int start,int middle){
if(numbers==nullptr||length<0){
return -1;
}
int count=0;
for(int i=0;i<length;i++){
if(numbers[i]>=start&&numbers[i]<=middle)
count++;
}
return count;
}
3.二维数组中的查找
#include
#include
using namespace std;
bool Find(int target, vector<vector > array) {
int row = array.size(); //行数
int column = array[0].size(); //列数
int i = 0, j = column - 1;//右上角
while (i < row && j >= 0)
{
if (array[i][j] == target) //从右上角第一个找起,大于target向左查找,小于target则向下查找
{
return true;
}
else if (array[i][j] > target)
{
j–; //向左查找
}
else
{
i++; //向下查找
}
}
return false;
}
int main()
{
vector vec1{ 3, 7, 9, 12, 19, 23 };
vector vec2{ 4, 17, 19, 31, 32, 33 };
vector<vector > array;
array.push_back(vec1);
array.push_back(vec2);
bool result = Find(32, array);
cout << "result = " << result << endl;
system(“pause”);
}
4.链表反转
/*
struct ListNode {
int val;
struct ListNode next;
ListNode(int x) :
val(x), next(NULL) {
}
};/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead)
{
// 反转指针
ListNode* pNode=pHead; // 当前节点
ListNode* pPrev=nullptr;// 当前节点的上一个节点
ListNode* pNext=nullptr;// 当前节点的下一个节点
ListNode* pReverseHead=nullptr;//新链表的头指针
// 反转链表
while(pNode!=nullptr)
{
pNext=pNode->next; // 建立链接
if(pNext==NULL) // 判断pNode是否是最后一个节点
pReverseHead=pNode;
pNode->next=pPrev; // 指针反转
pPrev=pNode;
pNode=pNext;
}
return pReverseHead;
}
};
参考:https://www.cnblogs.com/wanglei5205/p/8572458.html
5.快排
快排从小到大
void quicksort(int array[], int start, int last)
{
int i = start;
int j = last;
int temp = array[i];
if(i<j)//先检查左右条件
{
while(i<j)
{
while(i<j&&array[j]>=temp)//从右向左
j--;
if(i<j)
{
array[i]=array[j];
i++;
}
while(i<j&&temp>array[i])
i++;
if(i<j)
{
array[j]=array[i];
j--;
}
}
array[i]= temp; //把基准放到i处
quicksort(array,start,i-1);//以i为中间值,左右两边递归调用
quicksort(array,i+1,last);
}
}
6.判断字符串的括号是否成对出现
思路:可用栈来解决;遍历字符串,如果遇到左括号,则将左括号入栈,如果遇到右括号,则判断栈顶的元素是否为左括号,如果为左括号则弹出栈顶元素,然后继续字符串遍历,遍历结束后,如果栈为空,则认为括号是成对出现。
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string sss = “((1+(2+3))+6))”;
stack st;
bool bFlag = true;
for (auto ch : sss)
{
if (ch == ‘(’)
{
st.push(ch);
}
else
{
if (ch == ‘)’)
{
if (st.empty())
{
bFlag = false;
break;
}
else
{
st.pop();
}
}
}
}
if (st.empty() && bFlag)
{
cout << “pipei” << endl;
}
else
{
cout << “bu pipei” << endl;
}
system("PAUSE");
return 0;
}
7.选择排序
void sort(int *arr, int length)
{
int index;//定义最小值的下标
int temp;
for(int i = 0; i < length - 1; i++)//一共需要n-1轮
{
index = i;
for(int j = i + 1; j < length; j++)//从第i个元素开始到最后一个元素结束
{
if(arr[j] < arr[index])//找到最小元素并记录下标
index = j;
}
if(arr[index] < arr[i])//找到比较小的元素就交换
{
temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
8.链表的遍历和插入操作
递归,直接或间接的调用自己
链表:有数据域和地址域
单链表的遍历:
void bianli(node* head)
{
node* pNode = head;
while(pNode->next!=NULL)
{
pNode = pNode->next;
}
}
单链表的插入:
node* addnode(nodehead, int addvaule)
{
node newNode = new node();
newNode->next = NULL;
newNode->value = addvaule;
node* p =new node();
p=head;
if(head==NULL)
{
head=newNode;
}else
{
while(p->next!=NULL)
p=p->next;
p->next=newNode;
}
return head;
}
9.二分查找
int binarysearch(int *arr, int low, int high, int target)
{
int middle = (low+high)/2;
if(low > high)
return -1;
if(arr[middle]==target)
return middle;
if(arr[middle]>target)
return binaryserach(arr,low,middle-1,target);
if(arr[middle]<target)
return binarysearch(arr,middle+1,high,target);
}
10.两个栈生成一个队列
<分析>:
入队:将元素进栈A
出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;如果不为空,栈B直接出栈。
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
int a;
if(stack2.empty())
{
while(!stack1.empty()){
a = stack1.top();
stack2.push(a);
stack1.pop();
}
}
a = stack2.top();
stack2.pop();
return a;
}
private:
stack<int> stack1;
stack<int> stack2;
};
11.斐波那契数列迭代版本
class Solution {
public:
int Fibonacci(int n) {
if(n==0) return 0;
if(n==1) return 1;
int l=0;int r=1;int m=0;
for(int i=2;i<=n;i++)
{
m = l+r;
l = r;
r = m;
}
return m;
}
};
12.从尾到头打印链表
用库函数,每次扫描一个节点,将该结点数据存入vector中,如果该节点有下一节点,将下一节点数据直接插入vector最前面,直至遍历完,或者直接加在最后,最后调用reverse
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(struct ListNode* head) {
vector<int> value;
if(head != NULL)
{
value.insert(value.begin(),head->val);
while(head->next != NULL)
{
value.insert(value.begin(),head->next->val);
head = head->next;
}
}
return value;
}
};
13.两个链表第一个公共节点