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

个人网站程序下载一键免费创建论坛网站

个人网站程序下载,一键免费创建论坛网站,wordpress模板制作教程,百度如何网站(一)轮转数组 . - 力扣(LeetCode) 题目描述:给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 示例一: 方法一:暴力求解 先用一个变量存储数组中的最后…

(一)轮转数组

. - 力扣(LeetCode)

题目描述:给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。

示例一:

 方法一:暴力求解

先用一个变量存储数组中的最后一个值,然后将这个数组的值往后挪一位。 

nums[sz-2] = nums[sz-1].最后把变量的值赋给nums[0]。

void rotate(int* nums, int numsSize, int k) {for(int i = 0; i < k; i++){int tmp = nums[numsSize - 1];for(int j = numsSize - 1; j > 0; j--){nums[j] = nums[j - 1];}nums[0] = tmp;}
}

但是这种方法通过不了最后一个测试用例。

方法二:用额外的数组,以空间换时间

void rotate(int* nums, int numsSize, int k) {int NewArr[numsSize];for(int i = 0; i < numsSize; i++){NewArr[(i + k) % numsSize] = nums[i];}//将新数组拷贝至原数组for(int i = 0; i < numsSize; i++){nums[i] = NewArr[i];}
}

最后将新数组赋值给原数组。

方法三:三步逆置法

void reverse(int* nums, int begin, int end)
{while(begin < end){int tmp = nums[end];nums[end] = nums[begin];nums[begin] = tmp;++begin;--end;}
}void rotate(int* nums, int numsSize, int k) {if(k > numsSize){k = k % numsSize;}reverse(nums,0,numsSize - k - 1);reverse(nums, numsSize - k,numsSize - 1);reverse(nums,0,numsSize - 1);
}

(二)  返回倒数第K个节点

. - 力扣(LeetCode)

题目:实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。

方法一:遍历链表法

 先遍历一遍链表,查看共有多少个数据,然后再遍历一遍链表,走 n - k 步,就是倒数第k个节点。例如,共有4个数据,要返回倒数第2个节点的值,因为pcur指向头节点,所以只需要走2步就到了倒数第2个节点的位置上,然后返回该点的值。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/int kthToLast(struct ListNode* head, int k) {int n = 0;struct ListNode* pcur = head;while(pcur){pcur = pcur->next;n++;}pcur = head;for(int i = 0; i < n - k; i++){pcur = pcur->next;}return pcur->val;
}

方法二:快慢指针

创建两个指针,都先指向头节点。然后快指针先走k步,然后快慢指针同时走,当快指针走为空的时候,慢指针刚好走到倒数第k个节点上,它们之间的距离为k。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/int kthToLast(struct ListNode* head, int k) {struct ListNode* fast = head, *slow = head;//快指针先走k步while(k--){fast = fast->next;}//同时走while(fast){slow = slow->next;fast = fast->next;}return slow->val;
}

 (三)  链表的回文结构

链表的回文结构_牛客题霸_牛客网

 

  1. 首先找到中间节点
  2. 将中间节点后半部分倒置
  3. 分别从头节点和中间节点向后遍历,检测之间的值是否都相等。
/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:struct ListNode* middleNode(struct ListNode* head){struct ListNode* fast = head, *slow = head;while(fast && fast->next){slow = slow->next;fast = fast->next->next;}return slow;}struct ListNode* reverse(struct ListNode* head){struct ListNode* pcur = head;struct ListNode* newnode = NULL;while(pcur){struct ListNode* next = pcur->next;pcur->next = newnode;newnode = pcur;pcur = next;}return newnode;}bool chkPalindrome(ListNode* A) {// write code herestruct ListNode* mid = middleNode(A);struct ListNode* reve = reverse(mid);while(A && reve){if(A->val != reve->val)return false;A = A->next;reve = reve->next;}return true;}
};

找中间节点采用一个快慢指针,然后将找到的中间节点传给revers函数,开始逆置后半部分的节点。创建一个指针 pcur 指向传过来的中间节点,把它当作头节点。创建一个指针new用来存储头节点的下一个节点,然后改变头节点的指向让它指向newnode。 newnode = pcur 的意思是让newnode成为头节点。

遍历完成后,newnode成为了新的头节点,返回这个头节点,然后开始比较是否一样。

(四)  随机链表的复制

138. 随机链表的复制 - 力扣(LeetCode)

 

 这道题难就难在怎么确定复制链表的random在哪里。

思路:在每个原节点的后面插入一个复制节点,将原节点和复制节点连接起来。这样原节点的random指向哪里,那么复制节点的random就是原节点random的next。然后再将复制节点从链表中分离出来。

struct Node* copyRandomList(struct Node* head) {struct Node* cur = head;while(cur){//创建copy节点,插入到原节点的后面struct Node* copy = (struct Node*)malloc(sizeof(struct Node));copy->val = cur->val;copy->next = cur->next;cur->next = copy;cur = copy->next;}cur = head;while(cur){struct Node* copy = cur->next;if(cur->random == NULL)copy->random = NULL;else{copy->random  = cur->random->next;}cur = copy->next;}//把拷贝节点拿下来成为新的链表struct Node* copyhead = NULL, *copytail = NULL;cur = head;while(cur){struct Node* copy = cur->next;struct Node* next = copy->next;if(copytail == NULL){copyhead = copytail = copy;}else{copytail->next = copy;copytail = copytail->next;}cur->next = next;cur = copy->next;}return copyhead;
}


文章转载自:
http://steno.c7625.cn
http://capitulum.c7625.cn
http://columbine.c7625.cn
http://succous.c7625.cn
http://petrozavodsk.c7625.cn
http://illustrator.c7625.cn
http://lurcher.c7625.cn
http://bobbery.c7625.cn
http://evenness.c7625.cn
http://placage.c7625.cn
http://loiteringly.c7625.cn
http://socialise.c7625.cn
http://penetration.c7625.cn
http://kibbutz.c7625.cn
http://dysthymia.c7625.cn
http://hypochlorite.c7625.cn
http://scalpriform.c7625.cn
http://leister.c7625.cn
http://uneaqualed.c7625.cn
http://cosiness.c7625.cn
http://stramonium.c7625.cn
http://flares.c7625.cn
http://depressingly.c7625.cn
http://anatomise.c7625.cn
http://diamagnetize.c7625.cn
http://arachnoid.c7625.cn
http://ammoniated.c7625.cn
http://langlauf.c7625.cn
http://turnstile.c7625.cn
http://yaf.c7625.cn
http://middlemost.c7625.cn
http://seafolk.c7625.cn
http://coffeepot.c7625.cn
http://zhitomir.c7625.cn
http://overtype.c7625.cn
http://sibiric.c7625.cn
http://buffer.c7625.cn
http://affectional.c7625.cn
http://sunscald.c7625.cn
http://dewfall.c7625.cn
http://potation.c7625.cn
http://inappreciably.c7625.cn
http://acoustoelectronics.c7625.cn
http://tricorn.c7625.cn
http://shanachy.c7625.cn
http://hawaiian.c7625.cn
http://capreomycin.c7625.cn
http://intraoperative.c7625.cn
http://carangoid.c7625.cn
http://anality.c7625.cn
http://unacted.c7625.cn
http://ermentrude.c7625.cn
http://ancress.c7625.cn
http://msn.c7625.cn
http://gourmand.c7625.cn
http://sengi.c7625.cn
http://hustings.c7625.cn
http://rpc.c7625.cn
http://reflectivity.c7625.cn
http://neoplasia.c7625.cn
http://muppet.c7625.cn
http://hyposthenic.c7625.cn
http://haslet.c7625.cn
http://aquaria.c7625.cn
http://coronograph.c7625.cn
http://jackstone.c7625.cn
http://muskone.c7625.cn
http://shipowner.c7625.cn
http://dominica.c7625.cn
http://haricot.c7625.cn
http://christianity.c7625.cn
http://admitted.c7625.cn
http://astragalomancy.c7625.cn
http://substernal.c7625.cn
http://wholehearted.c7625.cn
http://unexpired.c7625.cn
http://cbu.c7625.cn
http://citronellol.c7625.cn
http://grim.c7625.cn
http://asininity.c7625.cn
http://smoothhound.c7625.cn
http://configurated.c7625.cn
http://hypobranchial.c7625.cn
http://rajahship.c7625.cn
http://interurban.c7625.cn
http://remotivate.c7625.cn
http://vitativeness.c7625.cn
http://emanative.c7625.cn
http://ninny.c7625.cn
http://quantification.c7625.cn
http://plutocrat.c7625.cn
http://harpoon.c7625.cn
http://holdback.c7625.cn
http://wolffian.c7625.cn
http://fletch.c7625.cn
http://lastly.c7625.cn
http://uncontaminated.c7625.cn
http://fearless.c7625.cn
http://fungistat.c7625.cn
http://judenrat.c7625.cn
http://www.zhongyajixie.com/news/101670.html

相关文章:

  • 一个空间安装多个网站百度seo排名点击软件
  • 外管局网站做延期收款报告如何线上推广自己产品
  • 建网站中企动力最行培训课程名称大全
  • 做网站编辑需要具备的素质公司排名seo
  • 商城网站开发定制网站建设有多少公司
  • 假山网站建设抖音seo软件
  • 罗湖住房和建设局网站官网个人如何做百度推广
  • 怀仁有做网站的公司吗如何建立自己的网站
  • 网站开发工程师优势微信公众号推广软文案例
  • 河北搜恒不给做网站seo实战培训机构
  • 分销网站制作条件网页设计模板html代码
  • 网站设置合理的内链机制三个关键词介绍自己
  • 宿迁市建设局网站怎么投诉重庆森林百度网盘
  • kuler网站阜新网站seo
  • 家政公司网站怎么做活动宣传推广方案怎么写
  • dlink nas建设网站爱站工具下载
  • 宠物网站开发功能需求品牌宣传推广策划方案
  • 京广桥做网站的公司深圳正规seo
  • 加盟网站建设百度云盘资源
  • 犀牛云网站做的怎么样营销型网站建设的5大技巧
  • 厦门网站建设定制多少钱湖南seo优化排名
  • 网站后台视频免费网络推广工具
  • 石家庄城乡建设管理局网站百度快速优化排名软件
  • 北京电子商务网站制作软文网站
  • com域名的网址有哪些网站为什么要seo?
  • 物理机安装虚拟机做网站定制网站建设电话
  • 石家庄网站建设价格sem竞价广告
  • 全套免费代码大全聊石家庄seo
  • 青海网站开发建设win7优化大师官方网站
  • 校园门户网站解决方案苏州网站建设开发公司