- 博客(5)
- 收藏
- 关注
原创 leetcode之majority element
题目大意是:给一个数组a[n],求他的主要元素,设某个元素在数组中出现了k次,且这个k>n/2,那么这个数称为这个数组的主要元素(majority element),题目假设给的数组都不为空且存在主要元素。算法:1。首先想到遍历数组,同时打表纪录每个元素的出现次数,但是这个算法太不优化,显然不是题目想要的,然后一直也没想到好办法,就一直空着没做。2。昨天看书突然看到了这题,上面给了一个O(
2015-07-20 10:42:57 254
原创 leetcode之single number
题意很简单,就是一个数组中除了一个数只出现一次,其他的数都出现了两次,找出这个单个的数。要求不使用额外内存,复杂度为线型时间复杂度。解法就是用“异或”操作源码:int singleNumber(int* nums, int numsSize) { int i,k=nums[0]; for(i=1;i k=k^nums[i]; return
2015-06-25 16:21:11 238
原创 leetcode之plus one
题目:plus oneGiven a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.大概意
2015-06-24 16:20:06 330
原创 leetcode之remove element
Remove Element Total Accepted: 58916 Total Submissions: 182885My SubmissionsQuestion Solution Given an array and a value, remove all instances of that value in place and return t
2015-06-14 21:18:55 261
原创 leetcode之rotate array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as y
2015-06-14 20:08:06 274
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人