上一页 1 ··· 23 24 25 26 27 28 29 30 31 ··· 33 下一页
摘要: 什么是前缀和、前缀积?前缀和、前缀积也称前缀和数组,前缀积数组。给一数组A,前缀和:新建一数组B,数组中每一项B[i]保存A中[0…i]的和;后缀和:新建一数组B,数组中每一项B[i]保存A中[i…n-1]的和;前缀积:新建一数组B,数组中每一项B[i]保存A中[0…i]的积;后缀积:新建一数组B,... 阅读全文
posted @ 2015-05-03 15:32 AndyJee 阅读(13281) 评论(4) 推荐(5)
摘要: 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 ... 阅读全文
posted @ 2015-05-03 11:40 AndyJee 阅读(305) 评论(0) 推荐(0)
摘要: 题目:给一数组,如果存在众数,找出众数,即超过一半的数,如果不存在,返回-1.思路:众数:众数出现的次数大于其他所有数出现次数之和方法1:hashmap通过遍历数组,将数组每个数都通过hashmap来统计其出现的个数,如果某个数个数超过一半,则为众数。时间空间复杂度均为O(n)方法2:Moore V... 阅读全文
posted @ 2015-05-03 11:34 AndyJee 阅读(2728) 评论(0) 推荐(0)
摘要: 题目:1、给一数组,所有的数都出现了两次,只有一个数出现了一次,求这个数。2、给一数组,所有的数都出现了两次,只有两个数出现了一次, 求这两个数。思路:位运算1、将数组里所有的数都做异或操作,因为相同的数异或之后就等于0,所以结果就是只出现一次的那个数。2、同样将数组里所有的数做异或操作,相同的数异... 阅读全文
posted @ 2015-05-03 11:02 AndyJee 阅读(513) 评论(0) 推荐(0)
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm sho... 阅读全文
posted @ 2015-05-02 16:03 AndyJee 阅读(404) 评论(0) 推荐(0)
摘要: 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.Yo... 阅读全文
posted @ 2015-05-02 11:46 AndyJee 阅读(821) 评论(0) 推荐(0)
摘要: 题目:翻转句子中全部的单词,单词内容不变,例如I’m a student. ---->student. a I’am思路:与前面数组循环移动或翻转是一样的思路。1、每个单词单独翻转,如m’I a .tneduts2、翻转整个句子,如student. a I’m由于这里是全部翻转,所以先单独翻转,后整... 阅读全文
posted @ 2015-05-01 16:15 AndyJee 阅读(505) 评论(0) 推荐(0)
摘要: 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].题目:给一... 阅读全文
posted @ 2015-05-01 15:37 AndyJee 阅读(193) 评论(0) 推荐(0)
摘要: 题目:给定两个串a和b,问b是否是a的子串的变位词,例如输入a=hello,b=lel,lle,ello都是true,但b=elo是false。(字串是连续的)思路:滑动窗口思想:动态维护一个“窗口”,比如b的长度是3,考察a[0..2],a[1..3],a[2..4]是否是b的变位词,关键在于如何... 阅读全文
posted @ 2015-05-01 13:55 AndyJee 阅读(760) 评论(0) 推荐(0)
摘要: 题目:给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa]。二者的最长公共子串为[aba],长度为3。子序列是不要求连续的,字串必须是连续的。思路与代码:1、简单思想:遍历两个字符串X、Y,分别比较X的字串与Y的字串,求出最长的公共字串。设X长度为m,Y长度为n,最长... 阅读全文
posted @ 2015-05-01 10:37 AndyJee 阅读(644) 评论(0) 推荐(0)
上一页 1 ··· 23 24 25 26 27 28 29 30 31 ··· 33 下一页