基础算法
1.二分查找
int binary_search(int* array,int n,int key){
//array 一定要是有序的 n为数组的长度 key查找的目标值
int low,high,mid;
low = 1;
high = n;
while(low <= high){
mid = (low + high)/2;
if (key < mid){ //在左
high = mid - 1;
}else if (key > mid){ //在右
low = mid + 1;
}else{ //命中
return mid;
}
}
return -1;
}
2.快速排序
def quick_sort(array,start,end):
if start < end:
i,j,base = start,end,array[start]
while i < j:
#从右向左寻找第一个小于base的元素
while i<j and array[j] >= base:
j -= 1
if i < j:
array [i] = array[j]
i += 1
#从左向右寻找第一个大于base的元素
while i<j and array[i] < base:
i += 1
#将大于base的元素移到右边
if i < j:
array[j] = array[i]
j -= 1
#此时i=j,将base换到正确的位置,且左边的值都小于base 右边的值都大于base
array[i] = base
#对左右两半分别递归
quick_sort(array, start, i-1)
quick_sort(array, i+1, end)
3.链表的反转
struct ListNode{
int val; 节点的值
struct ListNode* next; 纸箱下个节点的指针
ListNode((int x): val(x), pNext(NULL)){}
};
class Solution{
public:
void reverse_list(ListNode* head){
//反转指针
ListNode* pNode = head;
ListNode* pPre = nullptr;
ListNode* pNext = nullptr;
ListNode* pReverseHead = nullptr; //反转后链表的头指针
while(pNode != nullptr){
pNext = pNode->next;
if (pNext == NULL){
pReverseHead = pNode;
}
pNode->next = pPre;
pPre = pNode;
pNode = pNext;
}
return pReverseHead;
}
};
4.链表合并
(1)递归
ListNode* merge_list(ListNode* pHead1, ListNode* pHead2){
if(pHead1 == NULL){
return pHead2;
}
if(pHead2 == NULL){
return pHead1;
}
ListNode* pMergeHead = nullptr;
if (pHead1->val < pHead2->val){
pMergeHead = pHead1;
pMergeHead->next = mergeList(pHead1->next, pHead2);
}else{
pMergeHead = pHead2;
pMergeHead->next = mergeList(pHead1, pHead2->next);
}
return pMergeHead;
}
(2)非递归
ListNode* merge_list(ListNode* pHead1, ListNode* pHead2){
if(pHead1 == NULL){
return pHead2;
}
if(pHead2 == NULL){
return pHead1;
}
ListNode* current = new ListNode(-1,NULL); //当前指针
ListNode* root = current; //合并后的头结点
while (pHead1 != nullptr && pHead2 != nullptr){
if (pHead2->val >= pHead1->val){ //如果第二个链表的节点不小于第一个节点
current->next = pHead1; //将第一链表的当前节点加入合并后的链表中
current = pHead1; //当前指针后移
pHead1 = pHead1 -> next; //指针在链表以上后移一个
}else{
current->next = pHead2;
current = pHead2;
pHead2 = pHead2->next;
}
}
if (pHead1 != nullptr){
current->next = pHead1;
}
if (pHead2 != nullptr){
current->next = pHead2;
}
return root->next;
}
5.字符串的反转
static string reverse_string_by_stack(){
string original = "hello"
stack<char> my_stack;
foreach (char ch in original)
{
my_stack.Push(ch);
}
char[] c = new char[original.length];
for (int i = 0; i < original.length; i++)
{
c[i] = stack.Pop();
}
return new string(c);
}
6.合并两个有序数组
class Solution{
public:
void merge_array (vector<int>* nums1,int m, vector<int>* nums2, int n){
//nums1 数组1 vector[int] 长度为m
//nums2 数组2 list[int] 长度n 两个有序数组
int k = m + n;
while (m > 0 && n > 0){
if (nums1[m-1] >= nums2[n-1]){
nums1 [k-1] = nums1[m-1];
--k;
--m;
}else{
nums1 [k-1] = nums2[n-1];
--k;
--n;
}
}
while(n > 0){
nums1 [k-1] = nums2[n-1];
--k;
--n;
}
}
};
7.单词倒序
void reverseWords(){
String s = "I am Tom";
String[] array = s.split(s);
StringBuilder sbd = new StringBuilder(s.length());
for (i = array.lenth -1 ;i >= 1; i--){
sbd.append(array[i] + " ");
}
sbd.append(array[0]);
System.ot.print(sb.toString());
}
8.二叉树的最大深度
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x),left(NULL),right(NULL){}
};
class Solution{
public:
int maxDepth(TreeNode* root){
if (root == NULL){
return 0;
}
int l = maxDepth(root->left);
int r = maxDepth(root-> right);
return max(l,r) + 1;
}
};
9.平衡二叉树
bool is_balance_tree(TreeNode* root){
if (!root == NULL) return ture;
return is_balance_tree(root->left, root->right);
}
bool is_symeetric(TreeNode* left, TreeNode* right){
if(!left && !right) return ture;
//如果左子树为空,右子树不为空 或 左子树不为空,右子树为空,不平衡
if(left && !right || !left && right || left->val != right-> val) return false;
return is_balance_tree(left->left,left->right) && is_balance_tree(right->left,right->right);
}
10."1000......101010010" %3 的值
void main(){
string s = "10...1010001011...";
int i;
int flag=0;
for (i = 0; i<s.length; i++){
if (s[i] == '0'){
if (flag == 0){
flag = 0;
}else if (flag == 1){
flag = 2;
}else if (flag == 2){
flag = 1;
}
} else if (s[i] == '1'){
if (flag == 0){
flag = 1;
}else if (flag == 1){
flag = 0;
}else if (flag = 2){
flag = 2;
}
}
}
cout<< "二进制数"<<s<<"除以3的余数是:"<<flag<<endl;
}
11.爬楼梯
class Solution{
public:
int climb_stairs (int n){
vector<int> s;
s.push_back(1); //爬一阶
s.push_back(2); //爬两阶
if(n == 1) return 1;
if(n == 2) return 2;
for(int i=2; i<n; i++){
s.push_back(s[i-1]+s[i-2]);
}
return s[n-1];
}
};
12.统计素数的数量
int countPrimes(int n){
if (n <= 1) return 0;
int[] index = new int[n];
index[0] = index[1] = 1;
int a = 2;
while (n > a){
while (n > a && index[a] == 1) a++;
for (int temp = a*2; temp < n; temp = temp +a){
index [temp] = 1;
}
a++;
}
int result = 0;
for (int i=2; i< n; i++){
if (index[i] == 0){
result ++;
}
}
return result;
}
13.只出现一次的数字
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
num = 0
for i in range(0,len(nums)):
num = num ^ nums[i]
return num