我在接受采访时收到了这个问题,问题是
给定两个整数,返回它们共享的位数.
例如129和431将返回1 - 因为它们都共享数字1,但没有其他数字.95和780将返回0,因为没有整数重叠.
我的想法是遍历数字,将它们存储在哈希表中并检查 .containsKey.
我的Java解决方案:
public int commonDigits(int x, int y) {
int count = 0;
HashTable<Integer, String> ht = new HashTable<Integer, String>();
while (x != 0) {
ht.put(x % 10, "x");
x /= 10;
}
while (y != 0) {
if ((ht.containsKey(y % 10)) {
count++;
}
y /= 10;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
但是这会占用O(n)空间和O(n + m)时间,无论如何我可以改进这个?
我有一个查询,该查询扫描名称相同但ID不同的人。表结构为Staff(name,id)
我想找到的是具有相同名称但具有不同ID的人(他们是不同的人)。
我确实有两个人的名字和差异ID相同。
+---------+-----+
| NAME | ID |
+---------+-----+
| John S. | 138 |
| John S. | 491 |
+---------+-----+
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有
select a.name, b.name, a.id, b.id
from staff a, staff b
where a.name = b.name and a.id != b.id
Run Code Online (Sandbox Code Playgroud)
但是,当我运行这段代码时,它给出两次输出,分别是
+---------+-----+
| NAME | ID |
+---------+-----+
| John S. | 138 |
| John S. | 491 |
| John S. | 491 |
| John S. | 138 |
+---------+-----+
Run Code Online (Sandbox Code Playgroud)
我知道为什么会这样,因为这两个输出都满足检查条件,但是无论如何我可以抑制已经输出的输出吗?我可以运行一个选择表,并且WHERE ROWNUM <= 2,但是当我有更多同名的人时,这不是最佳选择。
谢谢!
说我有一个函数,它接受某种Option [] ...即:
def help(x: Int,
y : Option[BigInteger],
ec: ExecutionContext,
sc: SecurityContext): Future[Long] = { ... }
Run Code Online (Sandbox Code Playgroud)
我有一个用地图调用它的对象,比方说
val answerList: List[Future[Long]] = random.getPersons
.map(p => help(x , myY, ec, sc))
.collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
我说"myY"就是这么说的
类型不匹配,预期Option [BigInteger],实际:BigInteger.
当我的帮助方法选择类型时,我会看到它的来源.
我尝试通过选择[myY]来投射myY,但这似乎没有帮助.假设帮助方法正确实施,有人可以帮助我或指出正确的方向吗?谢谢!
说我有一个sql,目前返回所有在每年玩过的足球运动员.像这样:
name year goals
john 2010 1
john 2006 2
john 2006 8
fred 2006 1
Run Code Online (Sandbox Code Playgroud)
但我希望结果按照他们玩的年份进行分组,但如果来自不同年份,则不要压缩玩家名称,如下所示:
name year goals
john 2010 1
john 2006 10 <--- This is compressed, but there are still 2 johns
fred 2006 1 since they are from different years
Run Code Online (Sandbox Code Playgroud)
说到目前为止我已经这样做了.
(select name, year, goals
from table) as T
Run Code Online (Sandbox Code Playgroud)
如果我这样做
select *
from
(select name, year, goals
from table) as T
group by year;
Run Code Online (Sandbox Code Playgroud)
弗雷德将消失,但如果我按"名字分组",只剩下一个约翰.有帮助吗?