Leetcode
fjqcyq2
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode-- Majority Elemeny
Majority Element 原题: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty an原创 2015-02-08 21:26:10 · 439 阅读 · 0 评论 -
数组中只出现一次的数字
数组中只出现一次的数字题目:一个整形数组中除了两个数字之外,其他的数字都出现了两次。请写程序找出只出现一次的数字。要求时间复杂度O(n),空间复杂度O(1)。 - 思路: 原来有个题目是所有的数字都是两个,只有一个数字只出现了一次。而这个题目的思路就很容易了,让数组中的每个数字进行异或。异或能够满足交换律,并且a^a = 0, 0^a = a, 所以,很明显所有成对相同的数字经过异或之后都变成原创 2015-07-23 10:30:09 · 1327 阅读 · 0 评论 -
最长重复子串,最长公共子序列, 最长公共子串
最长重复子串,最长公共子序列, 最长公共子串原题:首先这是一个单字符串问题。子字符串R 在字符串L 中至少出现两次,则称R 是L 的重复子串。重复子串又分为可重叠重复子串和不可重叠重复子串,这里只是简单讨论最长可重叠的重复子串,给出基本算法。最长重复子串 用最笨的方法,逐个扫描,时间复杂度O(N^2), 代码如下,亲测可用:int LCS::comLen(const char*p, const c原创 2015-08-11 20:26:33 · 1206 阅读 · 0 评论 -
Leetcode--Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 000010100101000001111010011100), return 964176192 (represented in binary as 00111001011原创 2015-03-16 21:39:02 · 383 阅读 · 0 评论 -
Leetcode--Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 00000000000原创 2015-03-16 20:14:56 · 396 阅读 · 0 评论 -
Leetcode--Find Peak Element
Find Peak Number: A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain mu原创 2015-02-12 05:46:10 · 382 阅读 · 0 评论 -
Leetcode-- Sort Colors
Sort Colors 原题:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use原创 2015-02-09 23:16:28 · 417 阅读 · 0 评论 -
Leetcode--Find Minimum in Rotated Sorted Array
Find Minimum 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).Find the minimum element.You原创 2015-02-09 20:17:43 · 340 阅读 · 0 评论 -
Leetcode: Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. leetcode上的一道给定一个整形数组然后求其怎样进行组合才能得到一个最大的值,由于可能太大,会导致溢出,要求返回string类型而非整形。下面是我的刚开始的想法: 拿例题来说,给定一个数组[3,30,34,5转载 2015-01-30 13:28:37 · 426 阅读 · 0 评论 -
leetcode Single Number以及扩展
Single Number 给定的数组中所有元素均出现两次除了某一个元素为一次,在时间复杂度限定线性O(n)以及空间复杂度为O(1)的情况下,找出出现次数为1的元素。 在解题过程中,主要用到了位运算的异或运算:任何数字异或本身为0,且0^a == a.异或运算还满足交换律,即a^b == b^a; 在了解这些原理之后就可以顺利解决该题。具体代码如下:int singleNumber(int原创 2015-01-30 15:59:11 · 394 阅读 · 0 评论 -
Leetcode--Maximum Subarray
Maximum Subarray 原题:Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 解决思路:设置两个变量,sum和maxSum。令sum和maxSum开始都为A[0]. 逐个扫描数组,不断累加到sum中,若sum clas原创 2015-02-07 21:18:37 · 360 阅读 · 0 评论 -
Search Insert Position Leetcode 非二分查找法
Search Insert Position--Leetcode 开始没想到使用二分查找,就直接用的if else比较 low的做法,看运行时间还不算是最慢的,贴出来供以后查看。 class Solution { public: int searchInsert(int A[], int n, int target) { for(int i=0;i<n;++i)原创 2015-02-06 22:39:43 · 403 阅读 · 0 评论 -
KMP小计
KMP之前一直困扰了好久,说抽时间搞明白也是一直没时间,现在有点明白,但又时间太紧,所以仅记个大概来,方便我自己以后回顾。KMP之所以效率比较高,是因为模式串中有重复的元素。设S为主串,则要匹配的模式串就是T串。也就是说T串中有重复的元素,比如: T = “abababc” 可以看出abababc中有重复的串,即前四位abab与从第2-5位的串是一样的(从0开始) 则根据其他参考资料,T的nex原创 2015-08-12 15:27:35 · 409 阅读 · 0 评论
分享