1
0

771.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "math.h"
  2. #include "stdio.h"
  3. // 未验证,leecode使用了 uthash.h 库进行处理
  4. typedef struct linked {
  5. char val;
  6. struct linked *next;
  7. } *HashTable;
  8. HashTable *createTable(int hashCode, const char *jewels) {
  9. HashTable table[hashCode];
  10. for (int i = 0; i < sizeof(jewels) / sizeof(jewels[0]); ++i) {
  11. int temp = jewels[i] % hashCode;
  12. struct linked link = {jewels[i], NULL};
  13. if (table[temp] == NULL) {
  14. table[temp] = &link;
  15. } else {
  16. HashTable temps = table[temp];
  17. if (temps->val == jewels[i]) {
  18. continue;
  19. }
  20. while (temps->next != NULL) {
  21. temps = temps->next;
  22. if (temps->val == jewels[i]) {
  23. continue;
  24. }
  25. }
  26. temps->next = &link;
  27. }
  28. }
  29. return table;
  30. }
  31. int contained(char x, HashTable *table, int hashCode) {
  32. int temp = x % hashCode;
  33. HashTable temps = table[temp];
  34. while (temps){
  35. if (temps->val == x){
  36. return 1;
  37. }
  38. }
  39. return 0;
  40. }
  41. int numJewelsInStones(char *jewels, char *stones) {
  42. int count = 0;
  43. int hashCode = (int) sqrt(sizeof(jewels) / sizeof(jewels[0])) + 1;
  44. HashTable *table = createTable(hashCode, jewels);
  45. for (int i = 0; i < sizeof(stones) / sizeof(stones[0]); ++i) {
  46. if (contained(stones[i], table, hashCode)) {
  47. count++;
  48. }
  49. }
  50. return count;
  51. }
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。