哈希hash
i-Blue
抱平常心走平常路
展开
-
POJ 2406 Power Strings--字符串哈希(bkdhash)-KMP(next)
我用字符串哈希(bkdhash)写的,C++代码如下:#includeusing namespace std;typedef unsigned long long ull; char arr[1000001];ull nbase[1000001];ull Hash[1000001];int base = 31;void main(){ nbase[0] = 1; for原创 2016-05-09 16:15:15 · 1953 阅读 · 1 评论 -
349. Intersection of Two Arrays
Tags- binary search, hash table, two pointers, sort方法一:sort+binary search先对nums1、nums2从小到大排序,然后,对于nums2中不同的元素,我们在nums1中二分搜索,看看是否存在。这里注意,nums2中的元素是排过序的,所以没有必要每个元素都在(0, nums1.size()-1)中二分搜索。比如,num原创 2016-08-19 15:33:29 · 277 阅读 · 0 评论 -
4Sum
tags: hash table、two pointersk-sum问题都是一类问题。最经典的是2-sum,用two pointers在线性时间内解决。至于3sum、4sum问题,都可以先将问题分解,然后在用2sum的方法解决。2sum时间复杂度O(N^2),3sum时间复杂度O(N^3)。具体见:http://www.sigmainfy.com/blog/summary-of-k原创 2016-09-03 18:58:08 · 521 阅读 · 0 评论 -
Isomorphic Strings
tags:hash table两种情况下返回false:1)s中一个字符对应t中两个不同的字符。2)t中一个字符对应s中两个不同的字符。其他情况下都返回true。建两个hash表,m1、m2。代码:class Solution {public: bool isIsomorphic(string s, string t) { int size = s.size原创 2016-08-25 16:21:57 · 209 阅读 · 0 评论 -
389. Find the Difference-hash map/XOR
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was原创 2016-10-10 20:56:01 · 328 阅读 · 0 评论 -
387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note:原创 2016-10-13 19:57:16 · 277 阅读 · 0 评论 -
437. Path Sum III--dfs + hash + 连续序列的和等于给定的数num
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it原创 2016-10-27 19:48:22 · 1617 阅读 · 0 评论 -
找出数组中只出现1(2)次的n个数字
转:http://blog.csdn.net/wujingjing_crystal/article/details/52792286 http://www.cnblogs.com/youxin/p/3349834.html1. 给定一个数组,其中只有一个数出现一次,别的数都出现3次,找出这个数题目描述给定一个数组,其中只有一个数x出现一次,别的数都出现3次,找出这转载 2016-11-21 21:19:25 · 490 阅读 · 0 评论 -
204. Count Primes_找n以内的质数_hash
题目以及解法思路答案在原网页上都有。https://leetcode.com/problems/count-primes/Description:Count the number of prime numbers less than a non-negative number, n.Hint:Let's start with a isPrime function.原创 2016-12-08 20:03:58 · 347 阅读 · 0 评论