小编Arv*_*ind的帖子

给定连续的单词流,删除重复项

我最近被问到这个问题.

给定连续的单词流,在读取输入时删除重复项.

例:

输入: This is next stream of question see it is a question

输出: This next stream of see it is a question

从结束开始,question以及is已经出现过一次,所以第二次被忽略了.

我的解决方案

  1. 对于通过流传输的每个单词,在此方案中使用散列.

  2. 如果发生碰撞,则忽略该单词.

这绝对不是一个好的解决方案.我被要求优化它.

解决这个问题的最佳方法是什么?

algorithm

3
推荐指数
1
解决办法
1760
查看次数

素数可以写成两个数字x和y的平方和

问题是:

给定一系列数字(x,y),找到所有素数(仅计数),它们是两个数字的平方和,具有限制0<=x<y<=2*(10^8)

根据费马定理:

Fermat's theorem on sums of two squares asserts that an odd prime number p can be   
expressed as p = x^2 + y^2 with integer x and y if and only if p is congruent to 
1 (mod4).
Run Code Online (Sandbox Code Playgroud)

我做过这样的事情:

import math
def is_prime(n):
    if n % 2 == 0 and n > 2:
        return False
    return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))

a,b=map(int,raw_input().split())
count=0
for i in …
Run Code Online (Sandbox Code Playgroud)

python algorithm primes

2
推荐指数
1
解决办法
1054
查看次数

标签 统计

algorithm ×2

primes ×1

python ×1