1、折半查找
#include <iostream>
using namespace std;
int binarySearch(int* arr, int low, int high, int target)
{
while(low <= high)
{
int mid = (low + high)/2;
if(arr[mid] == target)
{
return mid;
}
if(arr[mid] > target)
{
high = mid -1;
}
if(arr[mid] < target)
{
low = mid + 1;
}
}
return -1;
}
int main(int argc, char const *argv[])
{
int* arr = new int[10];
for(int i=0; i<10;++i)
{
arr[i] = i;
}
int ret = binarySearch(arr, 0, 9, 4);
cout << ret <<endl;
return 0;
}
2、单链表反转
#include <cstdio>
#include <iostream>
using namespace std;
class Node{
public:
Node(int i):m_data(i){}
int m_data;
Node* next;
};
Node* InitList()
{
Node* node = new Node(0);
Node* head = node;
for(int i=1;i<10;i++){
Node* tmp = new Node(i);
node->next = tmp;
node = node->next;
}
node->next = NULL;
return head;
}
Node* ReverseList(Node* head){
Node* p = head;
while(p->next)
{
Node* node = p->next;
p->next = node->next;
node->next = head;
head = node;
}
return head;
}
int main(int argc, char const *argv[])
{
Node* head = InitList();
Node* pNode = head;
while(pNode){
cout << pNode->m_data << endl;
pNode = pNode->next;
}
cout << "============reverse=========="<<endl;
head = ReverseList(head);
while(head){
cout << head->m_data << endl;
head = head->next;
}
return 0;
}
3、最长子字符串
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <ctime>
#include <vector>
using namespace std;
void getmaxstring(char* word, char* sentence){
if(sentence == nullptr)
return;
char* s = sentence;
char* e = sentence;
int d = 0; //字符串长度
char* p;
for(;;){
if(*e == ' ' || *e == '\0'){
int tmp = e -s;
if(tmp > d){
p = s;
d = tmp;
}
if(*e == '\0'){
break;
}
if(*e == ' '){
s = e+1;
}
}
e++;
}
int i = 0;
for(i=0;i<=d;++i)
{
word[i] = *p++;
}
}
void Test_getmaxstring(){
char word[100];
memset(word, '\0', sizeof(word));
char* sentence = "hello world hahaaaa";
getmaxstring(word, sentence);
cout << word << endl;
}
int main(int argc, char const *argv[])
{
Test_getmaxstring();
return 0;
}
4、最长回文字符串
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
string s = "aaadfrfdraaaaa";
string res = "";
int n = s.size();
int** bFlag;
bFlag = new int*[n+1]; //0 false 1 true
for(int i=0;i<=n;++i)
{
bFlag[i] = new int[n+1];
}
int max_len = 0;
int ns = 0;
for(int j=0;j<n; ++j)
{
bFlag[j][j] = 1;
int i = 0;
for(;i<j;++i)
{
bFlag[i][j] =(s[i] == s[j]&&(j-i<2||j>0&&bFlag[i+1][j-1]))?1:0;
if(bFlag[i][j])
{
if(max_len < j-i+1)
{
max_len = j-i+1;
res = s.substr(i, max_len);
}
}
}
}
cout<<max_len<<endl;
cout << res<<endl;
for(int i=0;i<=n;++i)
{
delete[] bFlag[i];
}
delete[] bFlag;
return 0;
}
5、字符串转整数
int myAtoi(std::string str) {
// 合法性判断
if (str.isEmpty()) return 0;
// 正负号标记
int sign = 1;
// 转换值
int base = 0;
// 索引位数
int i = 0;
// 剔除开始空白字符
while (str.charAt(i) == ' ')
i++;
// 判断正负号
if (str.charAt(i) == '-' || str.charAt(i) == '+')
sign = str.charAt(i++) == '-' ? -1 : 1;
// 索引有效数字字符
while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
if (base > MAX_VALUE / 10 || (base == MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
return (sign == 1) ? MAX_VALUE : MIN_VALUE;
}
// 计算转换值
base = 10 * base + (str.charAt(i++) - '0');
}
// 计算结果值
return base * sign;
}
6、反转二叉树
BinaryTreeNode * reverseBT(BinaryTreeNode *myBT)//反转二叉树
{
if(myBT == nullptr)
return nullptr;
reverseBT(myBT->m_pLeft);
reverseBT(myBT->m_pRight);
BinaryTreeNode *temp = myBT->m_pLeft;
myBT->m_pLeft = myBT->m_pRight;
myBT->m_pRight = temp;
return myBT;
}