当前位置: 首页 > news >正文

动漫设计课程广告投放优化师

动漫设计课程,广告投放优化师,怎么做购物网站的购物车,长沙做网站推荐每日博客Day 8 每日算法 206.翻转链表 个人第一次思路: 其实我个人的第一个思路是比较暴力的,我第一遍暴力遍历链表,把链表的所有数值全部都保存到数组里面,然后翻转这个数组,再重复的覆盖当前的这个链表。但是这样…

每日博客Day 8

每日算法

206.翻转链表

个人第一次思路: 其实我个人的第一个思路是比较暴力的,我第一遍暴力遍历链表,把链表的所有数值全部都保存到数组里面,然后翻转这个数组,再重复的覆盖当前的这个链表。但是这样子的实现方式实在是太暴力了,没有什么必要。

算法思路(双指针):

  1. 定义cur指针指向头结点,定义pre指针指向null(定义节点)
  2. 开始移动:先保存cur->next的节点为temp,不然后面不会丢失
  3. 往后面移动pre和cur指针,如果cur为NULL了,说明到了最后的位置,返回pre节点
class Solution {
public:ListNode* reverseList(ListNode* head) {ListNode* pre = nullptr;ListNode* cur = head; ListNode* temp{};while (cur != nullptr){temp = cur->next;cur->next = pre;pre = cur;cur = temp;}delete  temp;return pre;}
};

递归法:

其实对于我个人来说,我感觉递归如果理解了还是很容易的,但是有时候如果不理解他的结束条件和初始化的设置的话,还是感觉一下子不能明白。这个题目的递归相对来说还是挺容易一下子就理解的,递归就是反复去执行相同的操作。

下面的代码还是可以更加的简洁的,比如传入的变量的设置。


class Solution {
public:ListNode* reverse(ListNode* pre, ListNode* cur){//递归就反复的执行同一个步骤if (cur == nullptr)  return pre;ListNode* temp = cur->next;cur->next = pre;/*       pre = cur;cur = temp;*///递归其实就是做了上面这两步,可以自己画图去理解一下return reverse(cur, temp);}ListNode* reverseList(ListNode* head) {ListNode* pre = nullptr;ListNode* cur = head;return reverse(pre, cur);}
};

24. 两两交换链表中的节点

代码思路:使用虚拟头结点的方式去解决

一共分成了四步骤来走

class Solution 
{
public:ListNode* swapPairs(ListNode* head) {//使用虚拟头结点的方式ListNode* dummyHead = new ListNode();dummyHead->next = head;ListNode* cur = dummyHead;	//cur节点先指向虚拟头节点//循环结束的终止条件while (cur->next != nullptr && cur->next->next != nullptr){//先保存我们之后要改变节点的指向,防止节点丢失ListNode* temp = cur->next;ListNode* temp2 = cur->next->next->next;cur->next = cur->next->next; //步骤1cur->next->next = temp; //步骤2cur->next->next->next = temp2;	//步骤3//4. 移动cur指针cur= cur->next->next;}return dummyHead->next;}
};

删除链表倒数的第N个节点

我第一次想到的解决方案,其实感觉也还是很暴力啊,而且还不能通过

class Solution {
public:ListNode* removeNthFromEnd(ListNode* head, int n) {int count = 0;ListNode* cur = head;while (cur->next!=NULL){count++;cur = cur->next;}int Index = count - n - 1;while (Index--){cur = cur->next;}ListNode* temp = cur->next;cur->next = temp->next;delete temp;return head;}
}
双指针解法

其实这个题目用双指针的话真的还是挺方便的,而且我感觉还挺很简单的啊,但是自己为什么最开始的就想起来要用双指针啊!!

这个代码写起来运行的时候还是有问题的:有两个特殊情况是我最开始没有考虑到的

  1. 如果要删除的链表中只有一个元素怎么处理?

我直接增加了对一个元素的判断,但是还是不可以通过,想到的问题就是

  1. 如果要删除的元素是链表的头节点,该怎么处理?

在这里如果使用虚拟头节点的方式来处理,真的就简单了,如果不用的话,要对很多特殊情况都做一个考虑说明,代码太复杂了,没必要这样子折磨我自己,啊哈哈哈哈

class Solution {
public:ListNode* removeNthFromEnd(ListNode* head, int n) {if ( n == 0 || head == nullptr )	return head;if(head->next == nullptr && n == 1)   return nullptr;ListNode* Slow = head;ListNode* Fast = head;while (n-- && Fast != nullptr){Fast = Fast->next;}while (Fast->next != nullptr){Fast = Fast->next;Slow = Slow->next;}ListNode* temp = Slow->next;Slow->next = Slow->next->next;delete temp;return head;}
};
19 双指针和虚拟头节点做法

这一次终于通过了!!呜呜呜!!

class Solution {
public:ListNode* removeNthFromEnd(ListNode* head, int n) {if ( n == 0 || head==nullptr )	return head;ListNode* dummyHead = new ListNode();dummyHead->next = head;ListNode* Slow = dummyHead;ListNode* Fast = dummyHead;while (n--){Fast = Fast->next;}while (Fast->next != nullptr){Fast = Fast->next;Slow = Slow->next;}Slow->next = Slow->next->next;return dummyHead->next;}
};

面试题 02.07. 链表相交

在这里其实要思考的问题就是如何同步,也是使用的是双指针的方式去解决

双指针解法

这是我第一遍自己写的题解,我不知道哪里有问题啊,能跑过18个案例了,我寻思这个也没什么特殊情况要考虑的吧?

class Solution {
public:ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {if (headA == nullptr || headB == nullptr)	return nullptr;ListNode* PointA = headA;ListNode* PointB = headB;int countA = 0;int countB = 0;while (PointA != nullptr){countA++;PointA = PointA->next;}while (PointB != nullptr){countB++;PointB = PointB->next;}PointA = headA;PointB = headB;//减去差值,然后移动A或者Bint num = 0;if (countA >= countB){num = countA - countB;while (num){num--;PointA = PointA->next;}while (PointA!=nullptr || PointB != PointA){PointA = PointA->next;PointB = PointB->next;}return PointA;}else {num = countB - countA;while (num){num--;PointB = PointB->next;}while (PointA != nullptr || PointB != PointA){PointA = PointA->next;PointB = PointB->next;}return PointA;}return PointA;}
};

稍微修改了一些细节,然后检查了一下就通过了

class Solution {
public:ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {if (headA == nullptr || headB == nullptr)	return nullptr;ListNode* PointA = headA;ListNode* PointB = headB;int countA = 0;int countB = 0;while (PointA != nullptr){countA++;PointA = PointA->next;}while (PointB != nullptr){countB++;PointB = PointB->next;}PointA = headA;PointB = headB;//减去差值,然后移动A或者Bif (countA < countB){//交换可以统一后面判断的标准swap(countA, countB);swap(PointA, PointB);}int num = countA - countB;while (num--)	PointA = PointA->next;while (PointA != nullptr){if (PointA == PointB)	return PointA;PointA = PointA->next;PointB = PointB->next;}return nullptr;}
};

原来的代码修改了一下逻辑,但是说实话一个swap交换下,可以减少后面这里的很多判断

class Solution {
public:ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {if (headA == nullptr || headB == nullptr)	return nullptr;ListNode* PointA = headA;ListNode* PointB = headB;int countA = 0;int countB = 0;while (PointA != nullptr){countA++;PointA = PointA->next;}while (PointB != nullptr){countB++;PointB = PointB->next;}PointA = headA;PointB = headB;//减去差值,然后移动A或者Bif (countA < countB){//交换可以统一后面判断的标准swap(countA, countB);swap(PointA, PointB);}int num = countA - countB;while (num--)	PointA = PointA->next;while (PointA != nullptr){if (PointA == PointB)	return PointA;PointA = PointA->next;PointB = PointB->next;}return nullptr;}
};

项目进度

添加IP地址和端口控件

通过添加了两个控件,一个初始化的时候传入IP地址一个初始化的时候传入端口的地址。

修改InitSocket函数,传入IP地址和端口

程序在运行的时候出现了链接不上的问题,原因是在于网络传输的字节序是有问题的,在网络中传输字节序的时候要多注意


文件树控件和获取驱动信息功能

size_t len = recv(m_sock, buffer + index, BUFFER_SIZE - index, 0);
//服务端必须要发送xiao'xiCServSocket::GetInstance()->Send(pack);

调试的时候发现这个len是0,表明客户端没有接收到来自服务端发送的消息信息

面试题目

只是看了书,没有做笔记,明天再做把。。。。

设计模式

了解了一下C++设计模式中的类图的内容,描述类与类之间的关系

单例设计模式:

最近在写的这个远程控制的项目就是采用的单例设计模式的思想

单例设计经常被使用到,是一种比较重要的设计模式,需要熟练的掌握

在应用系统开发中,我们常常有以下需求:

1.需要生成唯一序列的环境

2.需要频繁实例化然后销毁的对象。

3.创建对象时耗时过多或者耗资源过多,但又经常用到的对象。

4.方便资源相互通信的环境

实际案例:

  • 多线程中网络资源初始化
  • 回收站机制
  • 任务管理器
  • 应用程序日志管理

单列设计模式的实现步骤

  1. 构造函数私有化
  2. 提供一个全局的静态方法,访问唯一对象
  3. 类中定义一个静态指针,指向唯一对象

懒汉式代码

class  Single
{
public:static Single* GetInstance()//2. 提供一个全局的静态方法,访问唯一的对象{if (m_single == nullptr){m_single = new Single;}return m_single;}void Print(){std::cout << "This is Print Func" << char(10);}
private:static Single* m_single;//3. 类中定义一个静态的指针,指向唯一的对象Single(){std::cout << "This is Single()" << char(10);m_single = nullptr;} //1. 构造函数私有化
};Single* Single::m_single = nullptr; //初始化操作
int main()
{//虽然构造函数是不可以被调用了的,但是可以声明指向这个类的指针Single* P1 = Single::GetInstance();Single* P2 = Single::GetInstance();P1->Print();P2->Print();std::cout << hex << P1 << char(10);std::cout << hex << P2 << char(10);return 0;
}

饿汉式代码

饿汉式代码其实就是在类外静态指针初始化的时候直接去new申请了内存空间,这样我们在不需要再GetInstance中去判断初始化操作了

整体的效果和懒汉式是一样的,但是实现的思想是不同的。

饿汉式可能会出现多线程对资源争夺的问题,在日后的学习中心需要多多注意这方面的内容


class  Single
{
public:static Single* GetInstance()//2. 提供一个全局的静态方法,访问唯一的对象{//if (m_single == nullptr)//{//	m_single = new Single;//}return m_single;}void Print(){std::cout << "This is Print Func" << char(10);}
private:static Single* m_single;//3. 类中定义一个静态的指针,指向唯一的对象Single(){std::cout << "This is Single()" << char(10);m_single = nullptr;} //1. 构造函数私有化
};Single* Single::m_single = new Single; //初始化操作
int main()
{//虽然构造函数是不可以被调用了的,但是可以声明指向这个类的指针Single* P1 = Single::GetInstance();Single* P2 = Single::GetInstance();P1->Print();P2->Print();std::cout << hex << P1 << char(10);std::cout << hex << P2 << char(10);return 0;
}
http://www.mnyf.cn/news/51227.html

相关文章:

  • 邵阳找工作网站关键词批量调词软件
  • 模板搭建网站整合营销传播的方法包括
  • 丽水市城市建设投资有限责任公司网站免费seo公司
  • 网站经营百中搜优化
  • 手机浏览器下载大全免费下载长沙官网网站推广优化
  • 青海网站制作的公司最近有哪些新闻
  • 长春网站建设中心推广优化工具
  • 专门做拼花网站seo搜索优化专员
  • 昆明手机app开发合肥网站优化技术
  • 为什么重装电脑没有wordpress优化合作平台
  • 中介做网站的别打电话下载百度卫星导航
  • 网站主题制作二维码引流推广的平台
  • 丝绸之路网站建设报告seo零基础教学视频
  • 现在还做响应式网站吗邢台网站公司
  • 用html5做的网站素材企业网站seo平台
  • 网站制作建设飞沐网络营销效果评估
  • 网站被收录后又被提出了安卓优化大师最新版
  • 网站主机类型seo快速建站
  • wordpress修改永久链接后无法访问武汉网络seo公司
  • 垂直网站做排名全网营销平台
  • 一家装修的网站怎么做的seo优化师是什么
  • wordpress全站cdn怎么投放广告
  • 互动营销的案例及分析关键词优化公司
  • 做网站租空间网络产品及其推广方法
  • 新乡网站建设哪家公司好软文营销定义
  • 网站首页制作的过程河南网站推广公司
  • wordpress搭建的知名网站google浏览器网页版
  • 网站建设seo网络推广搜索引擎优化怎么做的
  • 视频网站如何推广搜索引擎优化seo论文
  • 山西智能网站建设制作设计网站logo