运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,暗/亮
前往 Spaces
Python
C
Java
my_hash_set = [None,'Jones',None,'Lisa',None,'Bob',None,'Siri','Pete',None] def hash_function(value): sum_of_chars = 0 for char in value: sum_of_chars += ord(char) return sum_of_chars % 10 def contains(name): index = hash_function(name) return my_hash_set[index] == name print("'Pete' is in the Hash Set:",contains('Pete')) #Python
#include <stdio.h> #include <string.h> char* myHashSet[10] = {NULL, "Jones", NULL, "Lisa", NULL, "Bob", NULL, "Siri", "Pete", NULL}; int hashFunction(const char* value) { int sumOfChars = 0; while (*value) { sumOfChars += (int)*value; value++; } return sumOfChars % 10; } int contains(const char* name) { int index = hashFunction(name); if (myHashSet[index] != NULL && strcmp(myHashSet[index], name) == 0) { return 1; // True } return 0; // False } int main() { printf("'Pete' is in the Hash Set: %s\n", contains("Pete") ? "true" : "false"); return 0; } // C
public class Main { static final String[] myHashSet = {null, "Jones", null, "Lisa", null, "Bob", null, "Siri", "Pete", null}; public static void main(String[] args) { System.out.println("'Pete' is in the Hash Set: " + contains("Pete")); } public static int hashFunction(String value) { int sumOfChars = 0; for (char c : value.toCharArray()) { sumOfChars += c; } return sumOfChars % 10; } public static boolean contains(String name) { int index = hashFunction(name); return myHashSet[index] != null && myHashSet[index].equals(name); } } //Java
Python 结果
C 结果
Java 结果
'Pete' 在哈希集中:True
'Pete' 在哈希集中:true
'Pete' 在哈希集中:true