我正在写一个Rust函数,它接受一个数字列表和一个最大值,并将给定数字的所有倍数加到最大值(重复数只计算一次).我写的函数的第一个版本是
use std::collections::HashSet;
pub fn sum_of_multiples(limit: u32, factors: &[u32]) -> u32 {
let set: HashSet<u32> = factors
.iter()
.map(|factor| {
let top: u32 = (limit - 1) / factor;
(1..=top).map(move |num| num * factor)
}).flatten()
.collect();
set.iter().fold(0, |acc, num| acc + num)
}
Run Code Online (Sandbox Code Playgroud)
(我知道合并HashSets这样可能不是最好的解决方案).这给出了预期的结果:
println!("{}", sum_of_multiples(100, &[3, 5])) // 2318
Run Code Online (Sandbox Code Playgroud)
当我把呼叫collect从中间取出并连接到最后一个时fold,我得到了一个不同的答案:
pub fn sum_of_multiples(limit: u32, factors: &[u32]) -> u32 {
let val: u32 = factors
.iter()
.map(|factor| {
let top: u32 = (limit - …Run Code Online (Sandbox Code Playgroud) 我有下一个哈希:
%hash = (
name => {
pos => 1
},
name_xxx => {
pos => 2
},
name_yyy => {
pos => 3
},
)
Run Code Online (Sandbox Code Playgroud)
并且想要构造下一个数组(键必须按顺序排列pos):
qw/ name name_xxx name_yyy /
Run Code Online (Sandbox Code Playgroud)
我想我应该做Schwartzian变换
以给定顺序提取哈希键的最短和/或最快方法是什么?
在Python3中,比较两个s 的最耗时的方法是什么(它们是否具有相同的元素)?Setelements
例如,我想要一个名为compareSets如下的函数.我应该如何编写代码以使其以最耗时的方式工作?
def compareSets(a, b):
# if (elements are identical)
# return True
# if (elements are not identical)
# return False
pass
Run Code Online (Sandbox Code Playgroud) 我在接受采访时被要求为以下问题创建一个有效的算法:
输入:
我们有一个RGB颜色列表.由3表示的每种颜色<x,y,z>在0到255之间坐标.此列表永远不会改变.
我们正在每当一些额外的颜色(不是一定从上面的列表),我们需要从列表返回最接近(距离任期)颜色的附加颜色.
笔记:
我们可以对颜色列表进行一些预处理,因为列表永远不会改变.
颜色之间的距离定义为:
d = ((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)^1/2
例:
让:列表:{<1,1,1>,<1,0,1>,<2,2,2>,<0,1,0>}
其他颜色:<0,0,0>
结果:最小距离<0,0,0>为<0,1,0>.
我试图解决这个问题:
显然,我们可以进行预处理并保留世界上的所有颜色对并保存距离,我们可以在O(1)运行时获得解决方案,但内存很大 (255^3*n)
最天真的解决方案是遍历列表并计算列表中每种颜色与附加颜色之间的距离,并返回最小距离的颜色.它的O(n)位置是n是颜色列表的长度.
我试图用x,y,z坐标对列表进行排序,并保持3个排序列表或按距离排序,<0,0,0>但我不知道如何继续使用它.
我也看到了这个,但问题不在于问题的算法方法.
Mark Weiss的数据结构书中Linked List的实现使我有些困惑。
List类在内部包含一个Node结构,如下所示。
...
class List {
private:
struct Node {
...
};
...
public:
...
private:
int theSize;
Node *head;
Node *tail;
};
Run Code Online (Sandbox Code Playgroud)
我的问题是,在List类内部确实有一个Node结构真的必要吗?我认为只要List类包含指向标头和尾节点的指针就足够了。将Node结构作为私有成员有什么好处?
谢谢!
我是一名Android开发人员,在Swift或中没有太多接触机会Kotlin。最近,我正在与一个Socket.Io客户端(镜像现有的iOS应用程序)合作,并找到了该代码段。
// swift code snippet
socket.emit("joinRoom", ["room": roomName])
Run Code Online (Sandbox Code Playgroud)
问题:如何在kotlin中尤其是第二个参数中表示上述代码行(["room": roomName])
更清楚地讲:可以使用哪种数据类型来表示第二个参数。
// Server Implementation
socket.on("joinRoom", function (data) {
console.log("Got 'joinRoom' from client , " + JSON.stringify(data));
sub.subscribe(data.room);
socket.join(data.room);
});
Run Code Online (Sandbox Code Playgroud) 我试图解决需要使用的“黑客等级”问题LinkedList,但发现了一些奇怪的问题。目的是打印LinkedList反面。
我已经尝试调试程序,但是找不到任何错误。
在下面的第一段代码中,我只能将的第一个和最后一个元素LinkedList放入ArrayList。
static void reversePrint(SinglyLinkedListNode head) {
List<Integer> tempList = null;
if (head == null)
return;
else {
tempList = new ArrayList<>();
tempList.add(head.data);
while(head.next != null)
head = head.next;
tempList.add(head.data);
}
System.out.println("Size of the List -"+tempList.size());
for(int i = tempList.size()-1; i >= 0; i--)
System.out.println("Index +"+i+" "+tempList.get(i));
}
Run Code Online (Sandbox Code Playgroud)
在下面的代码中,我将java.lang.OutOfMemoryError: Java heap space无法理解实际上是什么导致了此错误。
static void reversePrint(SinglyLinkedListNode head) {
List<Integer> tempList = null;
if (head == null)
return;
else {
tempList …Run Code Online (Sandbox Code Playgroud) 如果我们知道最后一个元素,则可以在O(1)时间内连接两个链表。那么,有没有办法C++使用内置数据结构来连接两个List,或者我必须自己实现链接列表然后使用它?
我以前编写过C代码,用于在二进制搜索树中插入和遍历整数值。我试图使它也适用于字符串。我进行了一些更改,例如将所有整数转换为字符串,还添加了诸如strcpy()和strcmp()之类的函数来处理字符串操作。但是代码似乎不起作用。有人可以向我解释哪里出了问题吗?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char *str;
struct node *left;
struct node *right;
};
struct node *root=NULL;
void preorder(struct node *temp)
{
if(temp==NULL)
return;
printf("%s ", temp->str);
preorder(temp->left);
preorder(temp->right);
}
void inorder(struct node *temp)
{
if(temp==NULL)
return;
inorder(temp->left);
printf("%s ", temp->str);
inorder(temp->right);
}
void postorder(struct node *temp)
{
if(temp==NULL)
return;
postorder(temp->left);
postorder(temp->right);
printf("%s ", temp->str);
}
struct node* create(char *str) // Function to create new node
{
struct node *new1;
new1=(struct node*)malloc(strlen(str)+10);
strcpy(new1->str,str);
new1->left=NULL;
new1->right=NULL;
return new1;
} …Run Code Online (Sandbox Code Playgroud) 尝试执行此代码时出现以下错误,但我不知道为什么。它似乎与使用StringBuffer并附加到它有关。
代码:
class Stock {
static String findStock(int[] arr) {
int flag = 0;
int begin = 0;
int start = 0;
int endIndex = 0;
boolean foundStart = false;
StringBuffer op = new StringBuffer();
while (flag != 1) {
//find start
for (int i = begin; i < arr.length - 1; i++) {
if (arr[i + 1] > arr[i]) {
start = arr[i];
endIndex = i;
foundStart = true;
break;
}
if (i == arr.length - 1) {
flag …Run Code Online (Sandbox Code Playgroud) data-structures ×10
c++ ×2
java ×2
linked-list ×2
algorithm ×1
android ×1
binary-tree ×1
c ×1
colors ×1
comparison ×1
geometry ×1
hashset ×1
iterator ×1
kotlin ×1
perl ×1
python ×1
python-3.x ×1
rust ×1
set ×1
sorting ×1
string ×1
swift ×1