1.实现一个动态扩容的数组
#ifndef __ARRAY_H__
#define __ARRAY_H__
using namespace std;
const int initialLen = 10;
template <class T>
class Array{
public:
Array(int len = initialLen){
T *p = new T[len];
m_data = p;
m_capacity = len;
m_size = 0;
}
int capacity(){
return m_capacity;
}
int size(){
return m_size;
}
void resize(int len){
T *p = new T[len];
for (int i = 0; i < m_size; ++i){
p[i] = m_data[i];
}
delete[] m_data;
m_data = p;
m_capacity = len;
}
bool isEmpty(){
return m_size == 0;
}
void print(){
cout << "Array: ";
cout << "Capacity = " << m_capacity << ", " << "Size = " << m_size << endl;
cout << '[';
for (int i = 0; i < m_size; ++i){
cout << m_data[i];
if (i != m_size - 1){
cout << ',';
}
}
cout << ']' << endl;
}
//增加操作
void add(int index, T num);
void addFirst(T num);
void addLast(T num);
//删除操作
T remove(int index);
T removeFirst();
T removeLast();
void removeElement(T num);
//修改操作
void set(int index, T num);
//查找操作
T get(int index);
int find(T num);
bool contains(T num);
private:
T *m_data; //数组数据
int m_capacity; //数组容量
int m_size; //数组大小
};
template <typename T>
void Array<T>::add(int index, T num){
if (index < 0 || index > m_size){
cout << "添加位置非法!" << endl;
return;
//throw 0; //这里可以使用抛异常,下面也可以
}
if (m_size >= m_capacity){
resize(2 * m_capacity);
}
for (int i = m_size; i > index; --i){
m_data[i] = m_data[i - 1];
}
m_data[index] = num;
m_size++;
}
template <class T>
void Array<T>::addFirst(T num){
add(0, num);
}
template <class T>
void Array<T>::addLast(T num){
if (m_size >= m_capacity){
resize(2 * m_capacity);
}
m_data[m_size] = num;
m_size++;
}
template <class T>
T Array<T>::remove(int index){
if (index < 0 || index >= m_size){
cout << "删除位置非法!" << endl;
return NULL;
}
T res = m_data[index];
m_size--;
for (int i = index; i < m_size; ++i){
m_data[i] = m_data[i + 1];
}
if (m_size < m_capacity / 4){
resize(m_capacity / 2);
}
return res;
}
template <class T>
T Array<T>::removeFirst(){
T res = m_data[0];
remove(0);
return res;
}
template <class T>
T Array<T>::removeLast(){
if (m_size == 0){
cout << "删除位置非法!" << endl;
return NULL;
}
m_size--;
if (m_size < m_capacity / 4){
resize(m_capacity / 2);
}
return m_data[m_size];
}
template <class T>
void Array<T>::removeElement(T num){
int flag = find(num);
if (flag != -1){
remove(flag);
}
}
template <class T>
void Array<T>::set(int index, T num){
if (index < 0 || index >= m_size){
cout << "修改位置非法!" << endl;
return;
}
m_data[index] = num;
}
template <class T>
T Array<T>::get(int index){
if (index < 0 || index >= m_size){
cout << "访问位置非法!" << endl;
return NULL;
}
return m_data[index];
}
template <class T>
int Array<T>::find(T num){
for (int i = 0; i < m_size; ++i){
if (m_data[i] == num){
return i;
}
}
return -1;
}
template <class T>
bool Array<T>::contains(T num){
for (int i = 0; i < m_size; ++i){
if (m_data[i] == num){
return true;
}
}
return false;
}
#endif
2.两个有序数组合并一个有序数组
void merge(vector<int>& nums1,int m,vector<int>& nums2,int n)
{
vector<int> nums;
int i=0,j=0;
while(i<m && j<n){
if(nums1[i]<nums2[j]){
nums.push_back(nums1[i]);
i++;
}else{
nums.push_back(nums1[i]);
j++;
}
}
while(i<m)
nums.push_back(nums1[i]);
while(j<n)
nums.push_back(nums1[i]);
for(int k=0;k<nums.size();k++)
nums1[k]=nums[k];
}
4.快乐数,哈希实现
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> mySet;
while(n!=1){
int t=0;
while(n){
t+=(n%10)*(n%10);
n/=10;
}
n=t;
if(mySet.count(n)) break;
else mySet.insert(n);
}
return n==1;
}
};
4.两个有序链表合并一个有序链表
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1==NULL && pHead2==NULL)
return NULL;
if(pHead1==NULL)
return pHead2;
if(pHead2==NULL)
return pHead1;
if(pHead1->val<pHead2->val){
pHead1->next=Merge(pHead1->next,pHead2);
return pHead1;
}else{
pHead2->next=Merge(pHead1,pHead2->next);
return pHead2;
}
}
};
5.单链表的翻转
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL)
return NULL;
//结点初始化
ListNode *pCur,*pPre,*pNext;
pPre=head;
pCur=pPre->next;
//翻转链表
while(pCur){
pNext=pCur->next;
pCur->next=pPre;
pPre=pCur;
pCur=pNext;
}
//返回头指针
head->next=NULL;
head=pPre;
return head;
}
};