剑指offer-笔记

【LeetCode & 剑指offer 刷题笔记】汇总(已完成) 前言   不知不觉就结束了自己的秋招之路,虽感觉有些艰辛但是收获很多。找工作那段时间做了很多学习笔记,这是数据结构与算法相关的一部分笔记,这一块除了复习相关教科书,还有就是刷leetcode和《剑指offer》了。这段时间稍微空闲些,打算把自己之前在印象笔记上做的笔记迁移到优快云上(优快云解析html源码的功能还没加上去,已经反馈了这个问题,我要再等等这个功能,免得要过多的二次编辑),供大家学... 阅读详情

里面挺多细节的东西,每隔一段时间还要来复习一下。


1.求链表中的倒数第k个节点
用两个指针,第一个指针先走k-1步。第二个指针从头开始走,两个指针一起走,这样当第一个指针走向尾节点时第二个指针指向的就是倒数第k个节点。
代码安全性:

ListNode* FingKthToTail(ListNode* pListHead,unsigned int k)
{
    if(pListHead==NULL)
        return NULL;
    ListNode *pAhead=pListHead;
    ListNode *pBehind=NULL;
    for(unsigned int i=0;i<k-1;i++)
    {
        pAhead=pAhead->m_pNext;
    }
    pBehind=pListHead;
    while(pAhead->m_pNext!=NULL)
    {
        pAhead=pAhead->m_pNext;
        pBehind=pBehind->m_pNext;
    }
    return pBehind;
}

2.将字符串转化成整数:

int StrToInt(char* string)
{
    int number=0;
    while(*string!=0)
    {
        number=number*10+*string-'0';
        ++string;
    }
    return number;
}

sizeof(空类型\空类型+构造&析构函数)值为1。
sizeof(虚函数)值为:32位指针为4,64位指针为8。

3.构造函数:

class A
{
private:
    int value;
public:
    A(int n){value=n;}
    A(const A& other){value=other.value;}//就这样写才可以实现复制构造
}
int main()
{
    A a=10;
    A b=a;//复制构造
}

4.如何实现一个赋值构造函数:

class CMyString
{
public:
    CMyString(char* pData=NULL);
    CMyString(const CMyString& str);
    ~CMyString(void);
private:
    char* m_pData;
};


//是否把返回值的类型声明为该类型的引用

CMyString& CMyString::operator=(const CMyString &str)
{
    if(this==&str)
        return *this;//需要判断两者是不是属于同一个实例,如果是同一个那么释放的话就会释放掉再也找不回来
    delete []m_pData;//在分配新内存前要释放自身已有的空间,否则程序将出现内存泄露。
    m_pData=NULL;
    m_pData=new char[strlen(str.m_pData)+1];
    strcpy(m_pData,str.m_pData);
    return *this;//是否返回实例自身的引用(*this),只有返回一个引用,才可以允许连续赋值。
}

CMyString& CMyString::operator=(const CMyString &str)
{
    if(this!=&str)
    {
        CMyString strTemp(str);

        char* pTemp=strTemp.m_pData;
        strTemp.m_pData=m_pData;
        m_pData=pTemp;//使用实例再与原实例交换。
    }
    return *this;
}

int GetSize(int data[])//在这里数组会退化为指针,返回的值是一个指针,为4字节。
{
    return sizeof(data);
}
int _tmain(int argc,_TCHAR* argv[])
{
    int data1[]={1,2,3,4,5};
    int size1=GetSize(data1);
}


当你声明一个数组的时候5.Singleton模式

public sealed class Singleton4
{
    private Singleton4()
    {
    }

    private static Sinleton4 instance=new Singleton4();//静态构造函数能够确保只调用一次
    public static Singleton4 Instance
    {
        get
        {
            return instance;
        }
    }
}

6.规律二维数组中的数字查找

bool Find(char* matrix,int rows,int columns,int number)
{
    bool found=false;
    if(columns>0&&rows>0&&matrix!=NULL)
    {
        int row=0;
        int column=columns-1;
        while(row<rows&&column>=0){
        if(matrix[row*columns+column]==number)
        {    
            found=true;
            break;
        }
        else if(matrix[row*columns+column]>number)
            --column;
        else
            ++row;}
    }
    return found;
}

7.
char str1[]和char str2[]指向的是不同的地址
char *str3和char *str4指向的是同一个地址8.替换空格:

while(string[i]!='\0')
{
    ++originalLength;

    if(string[i]==' ')
        ++numberOfBlank;

    ++i;
}


We Are Happy改为We%20Are%20Happy
做法是计算出新的长度,然后从末尾开始粘贴过去。

while(){
if(string[indexOfOrigin]==' ')
{
    string[indexOfNew--]='0';
    string[indexOfNew--]='2';
    string[indexOfNew--]='%';    
}
else
{
    string[indexOfNew--]=string[indexOfOrigin];
}
--indexOfOrigin;
}

9.反向打印列表
链表:

struct ListNode
{
    int m_nkey;
    ListNode* m_pNext;
};
a.用栈存储输出
void PrintListReversingly(ListNode *pHead)
{
    std::stack<ListNode*> nodes;
    ListNode* pNode=pHead;
    while(pNode!=NULL)
    {
        nodes.push(pNode);
        pNode=pNode->m_pNext;
    }
    while(!nodes.empty())
    {
        pNode=nodes.top();
        printf("%d\t",pNode->m_nValue);
        nodes.pop();
    }
}

//递归在本质上就是一个栈结构
b.递归调用输出
void PrintListReversingly(ListNode* pHead)
{
    if(pHead!=NULL)
    {    
        if(pHead->m_pNext!=NULL)
        {    
            PrintListReversingly(pHead->m_pNext);
        }
        printf("%d\t",pHead->m_nValue);
    }
}

c.链表反向

10.二叉树一个特例是二叉搜索树,其他的特例是堆和红黑树。
已知两个搜索顺序(前序和中序),求重建的二叉树。
struct BinaryTreeNode
{
    int m_nValue;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;
};
????????

11.双栈实现队列

template<typename T>void CQueue<T>::appendTail(const T& element){stack1.push(element);}
template<typename T>T CQueue<T>::deleteHead(){
if(stack2.size()<=0)
{
    while(stack1.size()>0)
    {
        T& data=stack1.top();
        stack1.pop();
        stack2.push(data);
    }
}
if(stack2.size()==0)
    throw new exception("queue is empty");
T head=stack2.top();
stack2.pop();
return head;
}


12.很多算法都可以使用递归和循环两种不同的方式进行实现,基于递归的代码会比较简洁,但性能不如基于循环的实现方式。
位运算只有与、或、异或、左移和右移5种位运算。
查找无外乎顺序查找、二分查找、哈希表查找和二叉排序树查找。
哈希表的优点在于能够利用O(1)时间查找某一元素,是效率最高的查找方式。但其缺点是需要额外的空间来实现哈希表,是空间换时间的方式。类似题目:第一个只出现一次的字符就是使用哈希表来查找。与二叉排序树查找算法对应的是二叉搜索树。
计数排序时间复杂度为O(n),空间复杂度也为O(n),大致思路是两个数组,第一个循环为一个数组timesOfAge[age]用来存每个age的次数,也就是int age=ages[i];++timesOfAge[age];第二个循环为双重循环,正式对age排序,ages[index]=i;++index;第一层循环为所有年龄,第二层循环为timesOfAge[i]。

void SortAges(int ages[],int length)
{
    if(ages==NULL||length<=0)
        return;
    const int oldestAge=99;
    int timesOfAge[oldestAge+1];
    for(int i=0;i<oldestAge;i++)
        timesOfAge[i]=0;
    for(int i=0;i<length;i++)
    {
        int age=ages[i];
        if(age<0||age>oldestAge)
            throw new std::exception("age out of range");
        ++timesOfAge[i];
    }
    for(int i=0;i<oldestAge;i++)
    {
        for(int j=0;j<timesOfAge[i];j++)
        {
            ages[index]=i;
            ++index;
        }
    }
}

13.我们注意到旋转之后的数组实际上可以划分的两个排序的子数组,数组在一定程度上是排序的,因此我们可以试着用二分查找法的思路寻找最小的元素。

int Min(int *numbers,int length)
{
    if(numbers==NULL||length<=0)
        throw new std::exception("Invalid parameter");
    int index1=0;
    int index2=length-1;
    int indexMid=index1;
    while(numbers[index1]>=numbers[index2])
    {
        /*if(index2-index1==1)
        {
            indexMid=index2;
            break;
        }*/
        /*
        if(numbers[index1]==numbers[index2]&&numbers[indexMid]==numbers[index1])
        int result=numbers[index1];
        for(int i=index+1;i<=index2;i++)
        {
            if(result>numbers[i])
                result=numbers[i];
        }*/
        indexMid=(index1+index2)/2;
        if(numbers[indexMid]>=numbers[index1])
            index1=indexMid;
        else if(numbers[indexMid]<=numbers[index2])
            index2=indexMid;
    }
    return numbers[indexMid];
}

14. 

int AddFromlToN_Recursive(int n)
{
    return n<=0?0:n+AddFromlToN_Recursive(n-1);
}


但由于是函数调用自身,而函数调用是有时间和空间消耗的:每一次函数调用,都需要在内存中分配空间以保存参数、返回地址和临时变量,而往栈里压入数据和弹出数据都需要时间,另外递归中很多计算是重复的。
递归还有可能引起调用栈溢出的问题。从上往下的递归计算会有很多重复的节点,一个解决方法是存储中间项,但其实更简单的方法是从下往上计算。且时间复杂度为O(n)。
常见:

long long Fibonacci(unsigned int n)
{
    if(n<=0)
        return 0;
    if(n==1)
        return 1;
    return Fibonacci(n-1)+Fibonacci(n-2);
} 


从下往上思路:

long long Fibonacci(unsigned int n)
{
    int result[2]={0,1};
    if(n<2)
        return result[n];
    long long fibNMinusOne=1;
    long long fibNMinusTwo=0;
    long long fibN=0;
    for(unsigned int i=2;i<=n;i++)
    {
        fibN=fibNMinusOne+fibNMinusTwo;
        fibNMinusTwo=fibNMinusOne;
        fibNMinusOne=fibN;
    }
    return fibN;
}


一只青蛙一次可以跳1级台阶也可以跳2级台阶,求该青蛙跳上一个n级的台阶总共有多少种跳法。
公式:f(n)=f(n-1)+f(n-2);

15.
n=(n-1)&n;//该公式用于求二进制数据中1的个数。
return (x&(x-1)==0)&&(x!=0)//该公式说明x为2的幂,因为所有位上只有一个数为1。
位与相当于是取余
a&0x1==1(a为奇数)
a&0x1==0(a为偶数)
右移>>,意为除以2。
将a的第k位清零:a=a&~(1<<k)
将a的第k位置一:a=a|(1<<k)
x的相反数是(~x+1)
不用temp来交换两个整数:

void swap(int x,int y)
{
    x^=y;
    y^=x;
    x^=y;
}//^被称为位异或,常用于交换。
清零用&,置一用|。
求int型中1的个数:
int NumberOf1(int n)
{
    int count=0;
    while(n)
    {
        ++count;
        n=(n-1)&n;
    }
    reurn count;
}

16.常用数组前后交换法:

void ReorderOddEvent(int *pData,int length,bool (*func)(int))//注意这里:bool isEven(int n){return (n&1)==0;}
{
    if(pData=NULL||length=0)
        return;
    int *pBegin=pData;
    int *pEnd=pData+length-1;
    while(pBegin<pEnd)
    {
        while(*pBegin&(0x1)!=0&&pBegin<pEnd)
        {
            pBegin++;
        }
        while(*pEnd&(0x1)==0&&pBegin<pEnd)
        {    
            pEnd--;
        }
        if(pBegin<pEnd)
        {
            int temp=*pBegin;
            *pBegin=*pEnd;
            *pEnd=temp;
        }
    }
}

17.链表合并

ListNode* Merge(ListNode* pHead1,ListNode* pHead2)
{
    if(pHead1==NULL)
        return pHead2;
    if(pHead2==NULL)
        return pHead1;
    ListNode* pMergedHead=NULL;
    if(pHead1->m_nValue>pHead2->m_nValue)
    {
        pMergedHead=pHead2;
        pMergedHead->m_pNext=Merge(pHead1,pHead2->m_pNext);
    }
    else
    {
        pMergedHead=pHead1;
        pMergedHead->m_pNext=Merge(pHead1->m_pNext,pHead2);//注意此处的递归
    }//使用递归的时候不需要走循环,pMergedHead->m_pNext就可以不断递归
    return pMergedHead;
}

18.链表翻转

ListNode* Reverse(ListNode* pHead)
{
    ListNode* pReverseHead=NULL;
    ListNode* pNode=pHead;
    ListNode* pPrev=NULL;
    while(pNode!=NULL)
    {
        pNext=pNode->m_pNext;
        if(pNode->m_pNext==NULL)
            pReverseHead=pNode;
        pNode->m_pNext=pPrev;
        pPrev=pNode;
        pNode=pNext;
    }
    return pReverseHead;
}
pNext=pNode->m_pNext=pPrev
pPrev=pNode=pNext


19.二叉树的镜像

struct BinaryTreeNode
{
    int m_nValue;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;
};
树的子节点翻转:
void MirrorRecursively(BinaryTreeNode *pNode)
{
    if(pNode==NULL||pNode->m_pLeft==NULL&&pNode->m_pRight)
        return;
    BinaryTreeNode* temp=pNode->m_pLeft;
    pNode->m_pLeft=pNode->m_pRight;
    pNode->m_pRight=temp;
    if(pNode->m_pLeft)
        MirrorRecursively(pNode->m_pLeft);
    if(pNode->m_pRight)
        MirrorRecursively(pNode->m_pRight);
}


//递归也就是先确定跳转的条件以及要做的操作,然后进行递归,不断实现定义的操作。递归的条件由pNode转变为pNode->m_pLeft和pNode->m_pRight。

20.树的子结构

21.很多时候我们需要一个辅助结构来帮助我们快速进行算法。
找出当前栈中的最小值,我们需要一个辅助栈,这个栈与数据栈共同更新,只是存储侧重点不一样。22.广度优先遍历一个有向图是基于队列实现的,树是图的一种特殊退化形式,从上到下按层遍历二叉树本质上来说就是广度优先遍历二叉树。需要基于队列来实现。
deque使用,用于通过第一个节点去不断往深处遍历。

void PrintFromTopToBottom(BinaryTreeNode* pTreeRoot)
{
    if(!pTreeRoot)
        return;
    std::deque<BinaryTreeNode*> dequeTreeNode;
    dequeTreeNode.push_back(pTreeRoot);
    while(dequeueTreeNode.size())
    {
        BinaryTreeNode *pNode=dequeueTreeNode.front();
        dequeueTreeNode.pop_front();
        printf("%d",pNode->m_nValue);
        if(pNode->m_pLeft)//要理解队列与按层遍历的关系,中左右(左)左右(右)左右
            dequeueTreeNode.push_back(pNode->m_pLeft);
        if(pNode->m_pRight)
            dequeueTreeNode.push_back(pNode->m_pRight);
    }
}

std::deque<BinaryTreeNode*> dequeBinaryTree;
std::vector<int> path;
std::vector<int>::iterator iter=path.begin();
for(;iter!=path.end();++iter)
    printf("%d\t",*iter);

23.复杂链表的复制:

struct ComplexListNode
{
    int m_nValue;
    ComlpexListNode* m_pNext;
    ComplexListNode* m_pSibling;
};
void CloneNodes(ComplexListNode* pHead)//先复制链表,再将复制的节点与原链表相连
{
    ComplexListNode* pNode=pHead;
    while(pNode!=NULL)
    {
        ComplexListNode* pCloneNode=new ComplexListNode();
        pCloneNode->m_nValue=pNode->m_nValue;
        pCloneNode->m_pNext=pNode->m_pNext;
        pCloneNode->m_pSibling=NULL;
        pNode->m_pNext=pCloneNode;
        pNode=pCloneNode->m_pNext;
    }        
}
void ConnectSibling(ComplexListNode* pHead)//进行Sibling的连接
{
    ComplexListNode* pNode=pHead;
    while(pNode!=NULL)
    {
        ComplexListNode* pCloneNode=pNode->m_pNext;
        if(pNode->m_pSibling)
        {
            pCloneNode->m_pSibling=pNode->m_pSibling->m_pNext;
        }
        pNode=pCloneNode->m_pNext;
    }
}
ComlpexListNode* ReconnectNodes(ComplexListNode* pHead)
{
    ComplexListNode* pNode=pHead;
    ComplexListNode* pCloneNode=NULL;
    ComplexListNode* pCloneHead=NULL;
    if(pNode!=NULL)//先设置好每个链表的头结点指针
    {
        pCloneNode=pCloneHead=pNode->m_pNext;
        pNode->m_pNext=pCloneNode->m_pNext;
        pNode=pNode->m_pNext;//也就是链表指针前进两步        
    }
    while(pNode!=NULL)//设置每个链表的下一步,先设置pCloneNode再设置pNode。
    {
        pCloneNode->m_pNext=pNode->m_pNext;
        pCloneNode=pCloneNode->m_pNext;
        pNode->m_pNext=pCloneNode->m_pNext;
        pNode=pNode->m_pNext;
    }
    return pCloneHead;
}

24.二叉搜索树与双向链表
???????25.字符串的排列
将字符串分为第一个字符与其他所有字符,将第一个字符与其他所有字符中的字符交换并进行递归,就可以实现全排列的情况,也就是next_permutation

void permutation(char *pStr)
{
    if(str==NULL)
        return;
    permutation(pStr,pStr);
}
void permutation(char* str,char* begin)
{
    char temp=*str;
    *str=*begin;
    *begin=temp;
    
    permutation(str,begin+1);

    temp=*str;
    *str=*begin;
    *begin=temp;
}//也就是最外层循环是a和bcd换,第二层循环是b和cd换,第三层循环是c和d换。


全排列对于求一些特定排序的数组值特别有用,我们可以先列出所有的排列然后选取特定的符合条件的排列。这里涉及到数值数组的全排序,还有去重的组合排列。八皇后:

void Permutation()
{
    const int queens=8;
    int Column[queens]={0};
    for(int i=0;i<queens;i++)
        Column[i]=i;
    permutation(int Column,int length,0);
}
void Permutation(int Column[],int length,int index)
{
    if(index==length)
    {
        if(Check(Column,length))
        {
            ++g_number;
            PrintQueen(Column,length);
        }
    }
    else
    {
        for(int i=index;i<length;i++)
        {
            int temp=Column[i];
            Column[i]=Column[index];
            Column[index]=temp;

            Permutation(Column,length,index+1);

            int temp=Column[i];
            Column[i]=Column[index];
            Column[index]=temp;
        }
    }
}
bool Check(int Column[],int length)
{
    for(int i=0;i<length;i++)
    {    
        for(int j=i+1;j<length;j++)
        {
            if((i-j==Column[i]-Column[j])||(j-i==Column[i]-Column[j]))
                return false;
        }
    }
    return true;
}


注意:
用void swap(int *a,int *b){int m=*a;*a=*b;*b=m;}时
调用方法为swap(&list[k],&list[i])

26.partition()函数的使用O(n)

int Partition(int number[],int length,int start,int end)
{
    int i=start;
    int j=end;     
    int pivot=number[left];
    while(i<j)
    {
        while((i<j)&&number[i]<number[pivot])
            i++;
        while((i<j)&&number[j]>number[pivot])
            j--;
        if(i<j)
        {
            int temp=number[i];
            number[i]=number[j];
            number[j]=temp;
        }
    }
    int temp=number[i];
    number[i]=number[left];
    number[left]=temp;
    return i;
}


使用1:
求数组中出现次数超过一半的数字:
特点在于,如果对该数组进行排序这个数字一定位于排序后数组的中位数上。

int SearchMiddle(int number[],int length)
{
    int middle=length>>1;
    int start=0;
    int end=length-1;
    int index=Partition(number,length,start,end);
    while(index!=middle)
    {
        if(index>middle)
        {
            end=index-1;
            index=Partition(number,length,start,end);
        }
        else
        {
            start=index+1;
            index=Partition(number,length,start,end);
        }
    }
    return number[middle];
}

使用2:
最小的k个数:

void FindLessKth(int number[],int length,int *output,int k)
{
    int start=0;
    int end=length-1;
    int index=Partition(number,int length,int start,int end);
    while(index!=k-1)
    {
        if(index<k-1)
        {
            start=index+1;
            index=Partition(number,int length,int start,int end);
        }
        else
        {
            end=index-1;
            index=Partition(number,int length,int start,int end);
        }
    }
    for(int i=0;i<k;i++)
    {
        output[i]=input[i];
    }
    return output;
}


但是使用Partition方法会修改数组并且不适用于海量数据。
用multiset实现的最大堆解法:insert、begin、erase(iter)

typedef multiset<int,greater<int>> intSet;
typedef multiset<int,greater<int>>::iterator setIterator;
void FindLeastKth(const vector<int>& data,intSet& leastNumbers,int k)
{
    leastNumber.clear();
    if(k<1||data.size()<k)
        return;
    vector<int>::const_iterator iter=data.begin();
    for(;iter!=data.end();iter++)
    {
        if((leastNumber.size())<k)
        {
            leastNumber.insert(*iter);    
        }
        else
        {
            setIterator iterGreatest=leastNumber.begin();
            if(*(leastNumber.begin())>*iter)
            {
                leastNumber.erase(iterGreatest);
                leastNumber.insert(*iter);    
            }
        }
    }
}

用priority_queue实现的最大堆与最小堆
最大堆是默认的,通过less实现,构建时只需要priority_queue<int> q;然后push、pop、top、empty等。
最小堆是priority_queue<int,vector<int>,greater<int>> q;
如果是用外部定义的结构体则:

struct Node
{    
    int x,y;
    Node(int a=0,int b=0):x(a),y(b){};
    friend bool operator<(const Node &a,const Node &b)
};
inline bool operator<(const Node &a,const Node &b){
    if(a.x!=b.x)
        return a.x<b.x;
    return a.y>b.y;
}

在map和set中insert操作返回的是pair,第一个参数为值,第二个参数为bool函数,用来验证该值是否已经存在于map和set中。在multiset和multimap中返回的应该是该值存在的个数。

还有一个问题在于copy和unique_copy.

copy(coll.begin(),coll.end(),ostream_iterator<string>(cout,"\t"));
unique_copy(coll.begin().coll.end(),ostream_iterator<string>(cout,"\t"));如果重复则只输出一个


27.字符串问题

char a[]="hello";//sizeof(a)=6,sizeof(s)=4。
char *s=a;//这里的s、a、hello中所代表的意思都是h所在的地址,也就是存储字符串首字符的地址。所以用char*来存储会比较省空间一点。
for(int i=0;i<strlen(a);i++)
    printf("%c",s[i]);
    printf("%c",*s++);
    printf("%c",*(s+i));

char** 与char *a[]
char *a[]={"China","French","America","German"};//字符串常量的本质是地址。这里的sizeof(a)为16,因为存储的都是char*指针
char **s=a; 

char **s;
*s="hello world";//错误
char **s;
s=(char **)malloc(sizeof(char**));
*s="hello world";//正确
二级指针保存的是指针变量,一级指针保存的是内存单元的地址。虽然都是地址但是类型不一样。
char* g_StrCombine1=new char[g_MaxNumberLength*2+1];
char** strNumbers=(char**)(new int[iLen]);
qsort函数中compare的形参一定是const void*

char *g_Combination1=new char[g_MaxNumberLength*2+1];
char *g_Combination2=new char[g_MaxNumberLength*2+1];

int compare(const void* strNumber1,const void* strNumber2)
{
    strcpy(g_Combination1,*(const char**)strNumber1);//也就是char**的数组格式,拿快递的时候突然想通,其实也就是char *(*(const char**)strNumber1)的意思。
    strcat(g_Combination1,*(const char**)strNumber2);
    
    strcpy(g_Combination2,*(const char**)strNumber2);
    strcat(g_Combination2,*(const char**)strNumber1);
    
    strcmp(g_Combination1,g_Combination2);
}
int Transfer(int *numbers,int length)
{
    if(numbers==NULL||length<=0)
        return;

    char **strNumber=(char **)(new int[length]);//这里需要注意的是char**数组的初始化,也就是二维数组的初始化,
    for(int i=0;i<length;i++)
    {
        strNumber[i]=(char *)new char[g_MaxNumber+1];
        sprintf(strNumbers[i],"%d",numbers[i]);//这里只能使用sprintf来控制,普通的赋值会报无法从int转为char*,sprintf_s(buffer,length,format,[argument]);
    }
    qsort(strNumber,length,sizeof(char*),compare);
    for(int i=0;i<length;i++)
    {    
        printf("%s",strNumber[i]);
    }
    for(int i=0;i<length;i++)
        delete[] strNumbers[i];
    delete[] strNumbers;
}

int **数组的使用int[10][20]。
int *data与int data[][3]平级。
int **x;
x=(int**)malloc(sizeof(int*)*10);//先分配二级指针再分配一级指针
for(int i=0;i<10;i++)
    x[i]=(int*)malloc(sizeof(int)*20);
for(int i=0;i<10;i++)
    free(x[i]);//先释放一级指针再释放二级指针
free(x);

strcpy常用于复制字符串而memcpy用于复制任意内容如字符数组结构体整形和类等。
strcpy不指定长度遇到"\0"自动结束,而memcpy由第三个长度决定复制的长度。
void *memcpy(void *memTo,const void *memFrom,size_t size)主要步骤
while(size-->0) *tempTo++=*tempFor++; return memTo;
char *strcpy(char* dest,const char*src)主要步骤
while((*strDest++=*strSrc++)!='\0');


28.二叉树的深度

int TreeDepth(BinaryTreeNode *pRoot)
{
    if(pRoot==NULL)
        return;
    int nLeft=TreeDepth(pRoot->m_pLeft);
    int nRight=TreeDepth(pRoot->m_pRight);

    return (nLeft>nRight)?(nLeft+1):(nRight+1);
}

判断一个树是否为平衡二叉树

bool IsBalanced(BinaryTreeNode* pRoot);
{
    if(pRoot==NULL)
        return;
    int nLeft=TreeDepth(pRoot->m_pLeft);
    int nRight=TreeDepth(pRoot->m_pRight);
    int diff=nLeft-nRight;
    if(diff>1||diff<-1)
        return false;
    return IsBalanced(pRoot->m_pLeft&&pRoot->m_pRight);
}


29.哈希表构造用于存储每个字符出现的次数

char wordCount(char *pString)
{
    if(pString==NULL)
        return '\0';
    int hashTable[256];
    for(int i=0;i<256;i++)
    {
        hashTable[i]=0;
    }
    char *pHashKey=pString;
    while(*pHashKey!='\0')
    {
        hashTable[*(pHashKey++)]++;
    }
    pHashKey=pString;//注意这里将指针归位,进行下一步操作
    while(*pHashKey!='\0')
    {
        if(hashTable[*pHashKey]==1)
        {
            return *pHashKey;
        }
        pHashKey++;
    }
    return '\0';
}


30.二分查找法的使用去寻找一个排序的数组中k出现了几次。

int FindFirstK(int *data,int length,int k)
{
    if(data==NULL||length<1)
        return;
    int middle=length>>1;
    int temp=data[middle];
    int start=end=middle;
    if(temp==k)
    {
        if(data[middle-1]!=k)
            return middle;
        else
            end=middle-1;
    }
    else if(temp>k)
    {
        end=middle-1;
    }
    else
        start=middle+1;
    return FindFirstK(data,kength,k);
}

31.翻转单词顺序
大致思路,分两个指针分别指向字符串头和尾。

void Reverse(char *pBegin,char *pEnd)
{
    if(pBegin==NULL||pEnd==NULL)
        return;

    while(pBegin<pEnd)
    {
        char temp=*pBegin;
        *pBegin=*pEnd;
        *pEnd=temp;
        pBegin++;pEnd--;
    } 
}

char *ReverseSentence(char *pData)
{
    if(pData==NULL)
        return;
    while(*pBegin!='\0')
        pEnd++;
    pEnd--;
    Reverse(pBegin,pEnd);
    pBegin=pEnd=pData;
    while(*pBegin!='\0')
    {
        if(*pBegin==' ')
        {
            pBegin++;
            pEnd++;
        }
        else if(*pEnd==' '||*pEnd!='\0')
        {
            Reverse(pBegin,--pEnd);//注意这里的逻辑关系,如何计算得到下一个字符
            pBegin=++pEnd;
        }
        else
        {
            pEnd++;
        }
    }
    return pData;
}

32.连续子数组的最大和

int GetSum(int *pData,int length)
{
    if(numbers==NULL||length<1)
        return;
    int currentSum=0;
     int GreatestSum=0x80000000;//0x7FFFFFFF是int型的最大值,0x80000000是int型的最小值
    //动规的话这里是int *dp=new int[length];
    for(int i=0;i<length;i++)
    {
        if(currentSum<0)//动规的话这里是if(dp[i-1]<=0) i从1开始,dp[0]为array[0]
            currentSum=pData[i];//动规的话这里是dp[i]=array[i];
        else
            currentSum+=pData[i];//动规的话这里为dp[i]=array[i]+dp[i-1];
            
        if(currentSum>GreatestSum)
            GreatestSum=currentSum;//此操作用于记录所遇到的最大值,也就是添加了一个变量来存储,这操作很优秀,如果比它小就不存储,比它大就每次更新。
    }
    return GreatestSum;
}


33.数值的整数次方

double PowerWithUnsignedExponent(double base,unsigned int exponent)//复杂度为O(log(n))
{
    if(exponent==0)
        result=1;
    if(exponent==1)
        result=base;
    double result=PowerWithUnsignedExponent(base,exponent>>1);//求解n/2然后平方一下
    result*=result;
    if(exponent&0x1==1)
        result*=base;
    return result;
}
自己实现一个pow函数://复杂度为O(n)
double Power(double base,int exponent)
{
    double result=1.0;
    for(int i=1;i<exponent;i++)
        result*=base;
    return result;
}

34.树的子结构

BinaryTreeNode* HasSubTree(BinaryTreeNode* pRoot1,BinaryTreeNode* pRoot2)
{
    bool result=false;
    if(pRoot1!=NULL&&pRoot2!=NULL)
    {
        if(pRoot1->m_nValue==pRoot2->m_nValue)
            result=DoesTree1HasTree2(pRoot1,pRoot2)
        if(!result)
            result=HasSubTree(pRoot1->m_pLeft,pRoot2);
        if(!result)
            result=HasSubTree(pRoot1->m_pRight,pRoot2);
    }
}
BinaryTreeNode* DoesTree1HasTree2(BinaryTreeNode* pRoot1,BinaryTreeNode* pRoot2)
{
    if(pRoot1==NULL)
        return false;
    if(pRoot2==NULL)
        return true;
    if(pRoot1->m_nValue!=pRoot2->m_nValue)
        return false;
    return DoesTree1HasTree2(pRoot1->m_pLeft,pRoot2->m_pLeft)&&DoesTree1HasTree2(pRoot1->m_pRight,pRoot2->m_pRight)
}


35.二叉搜索树的后序遍历序列

bool VerifyPostOrder(int sequence[],int length)
{
    if(sequence==NULL||length<1)
        return;
    int root=sequence[length-1];
    int i=0;
    for(;i<length-1;i++)
    {
        if(sequence[i]>root)
            break;
    }
    int j=i;
    for(;j<length-1;j++)
    {
        if(sequence[j]<root)
            return false;    
    }
    bool left=true;
    if(i>0)    
        left=VerifyPostOrder(sequence,i);

    bool right=true;
    if(i<length-1)
    right=VerifyPostOrder(sequence+i,length-i-1);
    return (left&&right);
}


36.二叉树的路径//需要添加一个vector数组存路径以及一个变量存当前和

void FindPath(BinaryTreeNode* pRoot,int expectedSum)
{
    if(pRoot==NULL||expectedSum<0)
        return;
    vector<int> path;
    int currentSum=0;
    FindPath(pRoot,expectedSum,path,currentSum);
}
void FindPath(BinaryTreeNode* pRoot,int expectedSum,vector<int>& path,int &currentSum)
{
    currentSum+=pRoot->m_nValue;//这里的path和currentSum参数注意:引用是绑定到
    path.push(pRoot->m_nValue);
    bool isLeaf==(pRoot->m_pLeft==NULL&&pRoot->m_pRight);
    if(currentSum==expectedSum&&isLeaf)
    {
        vector<int>::iterator iter=path.begin();
        for(;iter!=path.end();iter++)
            printf("%d",*iter);
    }
    if(pRoot->m_pLeft!=NULL)
        FindPath(pRoot->m_pLeft,expectedSum,path,currentSum);    
    if(pRoot->m_pRight!=NULL)
        FindPath(pRoot->m_pRight,expectedSum,path,currentSum);
    
    currentSum-=pRoot->m_nValue;
    path.pop_back();    
}


37.链表删除增加练习

struct ListNode
{
    int m_nValue;
    ListNode* m_pNext;
};
void AddToTail(ListNode** pHead,int value)
{
    ListNode* pNew=new ListNode();
    pNew->m_nValue=value;
    pNew->m_pNext=NULL;
    if(*pHead==NULL)
    {
        *pHead=pNew;    
    }
    else
    {
        ListNode* pNode=*pHead;
        while(pNode->m_pNext!=NULL)
            pNode=pNode->m_pNext;
        *pNode->m_pNext=pNew;
    }
}
void DeleteNode(ListNode** pHead,int Value)
{
    ListNode* pDeleteNode=NULL;
    if(*pHead->m_nValue==value)
    {
        pDeleteNode=*pHead;
        *pHead=*pHead->m_pNext;
    }
    else
    {
        ListNode* pNode=*pHead;
        while(pNode->m_pNext!=NULL&&pNode->m_pNext->m_nValue!=value)
        {
            pNode=pNode->m_pNext;    
        }
        if(pNode->m_pNext!=NULL&&pNode->m_pNext->m_nValue==value)
        {
            pDeleteHead=pNode->m_pNext;
            pNode->m_pNext=pNode->m_pNext->m_pNext;
        }
    }
    if(pDeleteNode!=NULL)
    {
        delete pDeleteNode;
        pDeleteNode=NULL;    
    }
}


38.要形成一个环形链表,即在链表走到尾结点时将指针指向头结点。

int Joseph(int n,int m)
{
    if(numbers==NULL||length<1)
        return;
    list<int> lists;
    for(int i=0;i<n;i++)
    {
        lists.push_back(i);    
    }
    list<int>::iterator iter=lists.begin();
    while(lists.size()>1)
    {
        for(int i=1;i<m;i++)
        {
            iter++;
            if(iter==lists.end())
                iter=lists.begin();
        }
        list<int>::iterator next=++current;
        if(next==lists.end())
            next=lists.begin();
        
    }
}
    

BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree)
{
    BinaryTreeNpde* pLastNodeInList=NULL;
    ConvertNode(pRootOfTree,&pLastNodeInList;)    
}
void ConvertNode(BinaryTreeNode* pNode,BinaryTreeNode** pLasrNodeInList)
{
    
}
BinaryTreeNode* pLastOfNode;


&
**

FlashBootloader01-基于UDS的Flash Bootloader【架构篇】 文章目录1 Flash Bootloader的依赖项2 Flash Bootloader中使用的UDS的主要功能3 Flash Bootloader更新固件3.1 初始化MCU3.2 刷写流程 1 Flash Bootloader的依赖项 通常Flash Bootloader由以下四方面组成: UDS协议 上位机「编写Bootloader Sequence」 通信驱动(如CAN ) Flash Driver 2 Flash Bootloader中使用的UDS的主要功能 1. 10 02服务 客户端 阅读详情

相关推荐

OpenHarmony系统参数权限配置避坑指南:从param get/set失败到SELinux标签实战

本文深入解析OpenHarmony系统参数权限配置的常见问题与解决方案,涵盖DAC和SELinux双重权限控制机制。通过实战案例演示如何配置`param get/set`权限,解决'Permission denied'错误,并提供三方应用访问参数的安全方案,帮助开发者高效完成系统调试与优化。

weixin_27013291的博客 450

C++知识小记

输入输出: iostream库 标准库定义了 4 个 IO 对象。 cin、cout、cerr、clog 默认的,它们将内容输入或输出到当前程序执行的窗口。 可以通过重定向将它们与其它文件等联系起来,然后输入或输出到哪里。 std::cout &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;amp;amp;a

hn_lgc的专栏 415

车联网ECU、TSP与TBOX通信流程

对比维度ECU(电子控制单元)BMS(电池管理系统)应用场景传统燃油车 + 新能源汽车仅限新能源汽车核心作用控制整车各系统运行管理动力电池安全与寿命车联网角色数据采集与传输的核心载体提供电池相关的核心数据。

tecoes的博客 1236

剑指Offer系列刷题笔记汇总

本文转自:个人网站:点击查看 一前言 二总结 链表-8道 二叉树12道 二叉搜索树3道 数组11道 字符串8道 栈3道 递归4道 回溯法2道 其他15道 一、前言 本系列文章为《剑指Offer》刷题笔记。 刷题平台:牛客网 书籍下载:共享资源 刷题刷的比较慢,花费了两个多月,终于将所有题目过了一遍,牛客网一共有66道题,这次刷题主要使用C++,接下来会使用Python...

Jack-Cui 5万+

SORT算法在遮挡下总跟丢?深入OC-SORT源码,拆解其三大创新点如何解决卡尔曼滤波的顽疾

本文深入解析OC-SORT算法如何通过三大创新点解决多目标跟踪中的卡尔曼滤波顽疾。OC-SORT引入在线平滑虚拟轨迹技术(OOS)、观测中心动量匹配机制(OCM)和基于最后观测的轨迹恢复(OCR),显著提升了遮挡场景下的跟踪性能。实验数据显示,相比传统SORT算法,OC-SORT在MOT17数据集上MOTA提升5.7%,遮挡恢复成功率提高64.2%,为Multi-Object Tracking领域带来突破性进展。

weixin_42547431的博客 242

Matlab之fdesign的应用说明

fdesign 是 MATLAB 中用于数字滤波器设计的工具(属于),它提供了一种系统化的方式来定义滤波器参数并生成相应的滤波器。指定滤波器的频率、幅度等参数。例如,设计一个低通滤波器:Fs = 1000;% 采样率 (Hz)% 通带截止频率% 阻带截止频率Apass = 1;% 通带衰减 (dB)Astop = 60;% 阻带衰减 (dB)

嵌入式老牛的博客 1054

剑指offer刷题笔记

剑指offer,4-5章

夏尔的小木屋 474

网易数智CoreAgent实战:5步教你打造企业级AI数字员工(附避坑指南)

本文以网易数智CoreAgent智能体协作中枢为核心,提供了一份企业级AI数字员工的实战部署指南。文章详细拆解了从环境准备、智能体配置、业务集成、安全合规到效果评估的五个关键步骤,并分享了部署过程中的常见“避坑”经验,旨在帮助企业技术团队高效、安全地将AI智能体落地于实际业务场景,解决AI落地难题。

rice5的博客 957

《剑指offer》解题笔记

《剑指offer》解题笔记 《剑指offer》共50题,这两周使用C++花时间做了一遍,谨在此把一些很巧妙的方法、写代码遇到的难点、易犯错的细节等做一个简单的标注,但不会太过详细,具体算法还是请参考书本。 做的比较急,代码还有很多不完善的地方,如有错误,欢迎改正,地址为:github:sword_of_offer。可以使用git clone https://github.com/heL

Focustc 8149

33、密码学中的选择密文攻击与Cramer - Shoup加密方案

本文详细介绍了密码学中选择密文攻击的背景及其对Cramer-Shoup公钥加密方案的影响。该方案在决策Diffie-Hellman假设和抗碰撞哈希函数的基础上,能够有效抵御自适应选择密文攻击,并且其安全性无需依赖随机预言机模型。文章还分析了Cramer-Shoup方案的设计原理、加密与解密过程,以及其在标准假设下的安全性证明方法。此外,通过一系列相关练习,帮助读者进一步理解密码学概念如密文不可区分性、ElGamal加密、Goldwasser-Micali方案等。最后,文章展望了密码学的未来发展趋势,包括量子

vscode6remote的博客 100

【GlobalMapper精品教程】073:像素到点(Pixels-to-Points)从无人机图像轻松生成点云

Global Mapper v19引入的新的像素到点工具使用摄影测量原理,从重叠图像生成高密度点云、正射影像及三维模型。它使LiDAR模块成为已经功能很强大的的必备Global Mapper扩展功能。打开GlobalmapperV25.1中文版,点击【Lidar分析】→【像素到点云】,如下所示:V25之前的版本点击工具条上的【像素到点】工具:打开工具,可以【使用向导选】,或者选择【使用主窗口】,都很简单。

智慧空间实验室 1979

剑指offer系列刷题笔记汇总

剑指Offer系列刷题笔记汇总 flyingsen 2018-06-04 09:37:29 352 收藏 分类专栏: 剑指offer 本文转自:个人网站:点击查看 一前言 二总结 链表-8道 二叉树12道 二叉搜索树3道 数组11道 字符串8道 栈3道 递归4道 回溯法2道 其他15道 一、前言 本系列文章为《剑指Offer》刷题笔记。 刷题平台:牛客网 书籍下载:共享资源 刷题刷的比较慢,花费了两个多月,终于将所有题目过了一遍,牛客

好想去看海 323

如何获取不同分区模板的基因表达矩阵,abagen: Allen 大脑图谱遗传数据工具箱的使用笔记

基因表达从根本上塑造了人类大脑的结构和功能结构。像Allen人脑图谱这样的开放获取转录组数据集提供了前所未有的能力来检查这些机制。abagen工具箱,这是一个用于处理转录组学数据的开放获取软件包,并使用它来检查方法可变性如何影响使用Allen人脑图谱的研究结果。

从事脑科学核磁共振方法学研究,在Nature communications等权威期刊发表研究论文,熟练掌握磁共振处理方法和统计学方法,欢迎大家和我交流。 3341

刷《剑指offer》笔记

  本文是刷《剑指offer》代码中的学习笔记,学习ing。。 衡量时间和空间。 递归的代码较为简洁,但性能不如基于循环的实现方法。 转载于:https://www.cnblogs.com/missidiot/p/10757736.html...

weixin_30763455的博客 82

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

《剑指offer》所有面试题的笔记总结

*建议:还是以书为主,然后以我的笔记为辅助。 C#不太了解,所以没放书上的代码。博文https://www.cnblogs.com/james111/p/7003100.html对这个问题分析得很全面。 思路很简单,每个元素的值就是ta应该所处的数组下标,然后不断地归位就可以了。我的笔记如下: 这个题目相比上一个题目,难在“不能修改数组”上。所以思路是这样的: 以题目中的数组为例,长度为8,范围是1~7。从中间将其范围划分成两组:1~4一组,5~7一组。然后对值为1~4和值为5~7的数字进行计

zxc120389574的博客 780

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

剑指offer刷题笔记目录

本章全文为剑指off刷题笔记和心得 1. 前言: 计划两个月之内刷完,每日刷完后更新超链接 2.笔记目录: 数组(11道): 剑指Offer(一):二维数组中的查找 剑指Offer(六):旋转数组的最小数字 剑指Offer(十三):调整数组顺序使奇数位于偶数前面 剑指Offer(二十八):数组中出现次数超过一半的数字 剑指Offer(三十):连续子数组的最大和 剑指Offer(三十二):把...

weixin_43841801的博客 165

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

剑指Offer系列刷题笔记

剑指Offer系列刷题笔记汇总 文章目录剑指Offer系列刷题笔记汇总前言一、目录如下(持续更新) 前言 本系列为《剑指Offer》第二版全集 使用语言:Java 记录自己学习过程 从第一题开始 开始时间2021年10月27日21:00; 一、目录如下(持续更新) ...

qq_53183211的博客 478

《剑指Offer》代码笔记目录

《剑指Offer》代码笔记目录第01-10题面试题3 二维数组中的查找面试题4 替换空格面试题5 从尾到头打印链表面试题6 重建二叉树面试题7 用两个栈实现队列面试题8 旋转数组的最小数字面试题9 斐波那契数列面试题10 二进制中 1 的个数第11-20题面试题11 数值的整数次方面试题12 打印 1 到最大的 n 位整数面试题13 在 o(1) 时间删除链表结点面试题14 调整数组顺序使奇数位于偶

两鬓已不能斑白的专栏 1031

《剑指OFFER》刷题笔记

《剑指OFFER》刷题笔记 未完待更新 1.二维数组中的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 暴力查找 复杂度O(nm) 对每行二分查找O(nlog(m)) 二维数组中的一个数,小于它的数一定在其左边,...

Zeolim的博客 479

【学习笔记】剑指offer学习笔记

寒假到了,趁着春节时间,准备看一下《剑指offer》,学习一下笔试技巧。

weixin_43319685的博客 577

leetcode:剑指Offer第二版 C++个人笔记分享,Git代码分享

leetcode:剑指Offer第二版 C++个人笔记分享,Git代码分享引言正文后记说明 引言 之前在刷剑指Offer时,有做一些题解记录和分析,但是其实想起做学习笔记的时候,题已经刷的差不多了。 后来,本来打算每天刷题的时候都做一下笔记,但是刷了两天,又被拉去干活了,于是我的学习笔记就断了。 没办法,该干活还是得干活,笔记只能往后再找时间补上来。但是做了的题还是得分享一下,于是打算把自己的做题代码分享到Git,然后做一下Git的分享笔记。 正文 剑指Offer第二版个人做题记录:Git纯享版/其实我就是

weixin_42329812的博客 315

【剑指offer】剑指offer 练习笔记

剑指offer练习 1. 赋值运算符 class CMyString { char* m_pData;  int size;  } 写一个CMyString类的赋值运算符。 CMyString& operator = (const CMyString& s) { if( this == &s) return * this;  if(m_pData)  delete[]

csdn_chai的博客 616

《剑指Offer》从零单刷个人笔记整理(66题全)

github:https://github.com/ChopinXBP/JZOffer_Babel/ 《剑指Offer》以面试题为主,代码大都不长。比较适合两种人,第一种是新手入门,第二种是面前备战。 我就是以新手的姿态来刷这本书的,每一道题先按自己的思路写一遍,然后尽可能尝试所有的方法实现并调优,然后再把感想和总结以博客的形式进行记录和分享。 由于本人学习语言主Java,辅C++,所以每题...

NJU_ChopinXBP的博客 1881
上一篇: linux网络设备和一些知识点总结
下一篇: 最近遇到的部分知识点总结
Parallel2333
博客等级 码龄10年 40粉丝 51原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值