电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本
换一换
首页 金锄头文库 > 资源分类 > DOCX文档下载
分享到微信 分享到微博 分享到QQ空间

Leetcode数组类题目总结(Java版本-20193)

  • 资源ID:86699519       资源大小:177.16KB        全文页数:39页
  • 资源格式: DOCX        下载积分:1金贝
快捷下载 游客一键下载
账号登录下载
微信登录下载
三方登录下载: 微信开放平台登录   支付宝登录   QQ登录  
二维码
微信扫一扫登录
下载资源需要1金贝
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
支付方式: 支付宝    微信支付   
验证码:   换一换

 
账号:
密码:
验证码:   换一换
  忘记密码?
    
1、金锄头文库是“C2C”交易模式,即卖家上传的文档直接由买家下载,本站只是中间服务平台,本站所有文档下载所得的收益全部归上传人(卖家)所有,作为网络服务商,若您的权利被侵害请及时联系右侧客服;
2、如你看到网页展示的文档有jinchutou.com水印,是因预览和防盗链等技术需要对部份页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有jinchutou.com水印标识,下载后原文更清晰;
3、所有的PPT和DOC文档都被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;下载前须认真查看,确认无误后再购买;
4、文档大部份都是可以预览的,金锄头文库作为内容存储提供商,无法对各卖家所售文档的真实性、完整性、准确性以及专业性等问题提供审核和保证,请慎重购买;
5、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据;
6、如果您还有什么不清楚的或需要我们协助,可以点击右侧栏的客服。
下载须知 | 常见问题汇总

Leetcode数组类题目总结(Java版本-20193)

Leetcode151题目详解1 第一章线性表此类题目考察线性表的操作,例如数组,单链表,双向链表1.1 Remove Duplicates from Sorted Array描述Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example, Given input array A = 1,1,2,Your function should return length = 2, and A is now 1,2.分析无代码:public class Solution public int removeDuplicates(int A) if(A=null|A.length=0) return 0; int index=0; for(int i=1;i<A.length;i+) if(Aindex!=Ai) A+index=Ai; return index+1;/爱辅助网:www.aifuzhu.top 相关题:Remove Duplicates from Sorted Array II 见1.21.2 Remove Duplicates from Sorted Array II 描述Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array A = 1,1,1,2,2,3,Your function should return length = 5, and A is now 1,1,2,2,3分析可以加一个变量来记录重复元素的个数能很好的解决问题。由于此题已经是排序的了,如果是没有排序的,可以使用hashmap来求元素的个数。代码public class Solution public int removeDuplicates(int A) if(A=null|A.length=0) return 0; int index=0,cnt=0; for(int i=1;i<A.length;i+) if(Aindex!=Ai) A+index=Ai; cnt=0; else if(Aindex=Ai&&cnt<1) A+index=Ai; cnt+; return index+1; 相关题扩展该题到可重复n次的场景,只需要将cnt的上限设为<n-1即可。1.3 Search in Rotated Sorted Array描述Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.分析二分查找,此题要特别注意边界值。此题的边界比较多。(1) mid的位置判定,mid可能在左边区域,也可能在右边区域,需求通过(Amid与Afirst大小关系进行判定)(2) 在判定边界时,要注意考虑大于,小于,等于三种情况,在写代码时,本题我开始忘记了一个等号,如代码中标红的地方。(3) 二分的思想是一步一步将区域缩小,并且要充分考虑缩小的正确性,不能放松对边界的警惕(即要注意等于的情况)。如图所示:FirstmidlastFirstmidlast要注意mid落在哪个区域,通过Amid与Afirst大小比较可以确定,同时要注意边界条件的判断,当Amid=Afirst应该是将其归类Amid>Afirst的情况。代码public int search(int A, int target) if(A=null|A.length=0) return -1; int first=0,last=A.length-1; while(first<=last) int mid=(first+last)/2; if(Amid=target) return mid; else if(Amid>=Afirst) if(target>=Afirst&&target<Amid) last=mid-1; else first=mid+1; else if(target>Amid&&target<=Alast) first=mid+1; else last=mid-1; return -1; 相关题目Search in Rotated Sorted Array II,见 §1.41.4 Search in Rotated Sorted Array II描述Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.分析问题就出在边界上,即Amid=Afirst的情况,如果这样,那么无法确定mid的位置属于上题的图一还是图二。因此这时判断Afirst=target,如果不成立则first+缩小一个范围。代码public class Solution public boolean search(int A, int target) if(A=null|A.length=0) return false; int first=0,last=A.length-1; while(first<=last) int mid=(first+last)/2; if(Amid=target) return true; else if(Amid>Afirst) if(target>=Afirst&&target<Amid) last=mid-1; else first=mid+1; else if(Amid<Afirst) if(target>Amid&&target<=Alast) first=mid+1; else last=mid-1; else if(Afirst=target) return true; else first+; return false; 相关题目1.3 Search in Rotated Sorted Array1.5 Median of Two Sorted Arrays描述There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log(m + n).分析看到本题对算法的复杂度要求非常的高,是对数级别的,因此一定要运用二分查找的思想才行。本题更通用的问法是求第k个数。现在需要我们求的是中位数即第(m+n)/2个数。但也不完全准确,如果数组的个数是奇数,那么应该是(m+n)/2,如果是偶数,那么是(m+n)/2+(m+n)/2+1)/2.现在问题转换成了求第K+1个数。(此处的K从1开始计算),从1开始是为了方便。思路是两个数组都查找弟k/2个数,如果Ak/2=Bk/2 return Ak/2Ak/2>Bk/2 递归找k-k/2个数,且b的start位置为k/2+1Ak/2<Bk/2 同样递归找k-k/2,且a的start位置为k/2+1同时要注意如果两个数组长度悬殊太大,那么段数组可能不存在第k/2个元素,因此这是的K为short.length。此题的边界条件判断比较繁琐,当k=1时还需要单独判断,这个在考虑的时候没考虑到是在用例测试的时候发现的问题。代码public class Solution public double findMedianSortedArrays(int A, int B) int lena=A.length; int lenb=B.length; int len=lena+lenb; if(len%2=0)

注意事项

本文(Leetcode数组类题目总结(Java版本-20193))为本站会员(天下****21)主动上传,金锄头文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即阅读金锄头文库的“版权提示”【网址:https://www.jinchutou.com/h-59.html】,按提示上传提交保证函及证明材料,经审查核实后我们立即给予删除!

温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.