chapter_searching/replace_linear_by_hashing/ #33
Replies: 24 comments 40 replies
-
建议将“两数之和”的题目也放进来,免得跳转过去看分心 |
Beta Was this translation helpful? Give feedback.
-
dic.containsKey函数的最差时间复杂度不是O(1) |
Beta Was this translation helpful? Give feedback.
-
元旦过后,遇到最好的礼物。我真心觉得这么好的入门工具书,首先应该让人读的尽兴。而不是半遮半掩,我希望后面那些有规划没有写出来的内容抓紧和我们读者见面。否则很扫兴呀。 |
Beta Was this translation helpful? Give feedback.
-
为啥程序在C语言那一栏显示不了? |
Beta Was this translation helpful? Give feedback.
-
两数之和那道题,有点背单词 abandon 的感觉。 |
Beta Was this translation helpful? Give feedback.
-
没有C的代码吗? |
Beta Was this translation helpful? Give feedback.
-
作者你好,示例代码对于处理nums列表中存在多个满足两个数值之和为target的情况是不是解决不了呢?? |
Beta Was this translation helpful? Give feedback.
-
// C++ // Swift k神,两个方法结果不同,twoSumHashTable 这个取到的不是第1个下标。 |
Beta Was this translation helpful? Give feedback.
-
测试了一下,还是需要具体问题具体分析,如下情况线性查找就比哈希好。
输出结果: |
Beta Was this translation helpful? Give feedback.
-
如果能给出对应力扣上可以训练同类型的题目就好了 |
Beta Was this translation helpful? Give feedback.
-
@krahets佬,为什么C++哈希查找的算法时间复杂度是O(n)呀!在使用find()这个函数的时候不是需要遍历哈希表吗?时间复杂度是O(n),n次不就是O(n2)吗? |
Beta Was this translation helpful? Give feedback.
-
问一下,为什么要用诸如 |
Beta Was this translation helpful? Give feedback.
-
leetcode第一题哈哈哈哈大家的入门题 |
Beta Was this translation helpful? Give feedback.
-
为什么暴力时不先把target - nums[i]提出来? ns级优化 |
Beta Was this translation helpful? Give feedback.
-
请问,c语言暴力枚举中函数第四个参数 |
Beta Was this translation helpful? Give feedback.
-
c#中这段代码
改变一下顺序也是可以的,
目前想不懂源代码的写法是不是更有优势呢。 |
Beta Was this translation helpful? Give feedback.
-
/* 方法二:辅助哈希表 */
vector<int> twoSumHashTable(vector<int> &nums, int target) {
int size = nums.size();
// 辅助哈希表,空间复杂度为 O(n)
unordered_map<int, int> dic;
// 单层循环,时间复杂度为 O(n)
for (int i = 0; i < size; i++) {
if (dic.find(target - nums[i]) != dic.end()) {
return {dic[target - nums[i]], i};
}
dic.emplace(nums[i], i);
}
return {};
} 辅助哈希表中的查找和维护时间复杂度不是log(n)吗 |
Beta Was this translation helpful? Give feedback.
-
K大,举一反三,假定题中的两个元素可以重复,例如:
可以返回 [1, 1]作为结果,两种解法该如何更改呢? |
Beta Was this translation helpful? Give feedback.
-
补充一个排序+双指针策略,空间复杂度O(1) 时间复杂度O(nlogn) int[] findTwoSum(int[] nums, int target) {
Arrays.sort(nums); // 对数组排序
int left = 0;
int right = nums.length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
return new int[]{left, right};
} else if (sum < target) {
left++;
} else {
right--;
}
}
return null; |
Beta Was this translation helpful? Give feedback.
-
001 两数之和! |
Beta Was this translation helpful? Give feedback.
-
two_sum.c中针对uthash.h提供的函数的使用是否有错误?查阅文档,似乎HASH_ADD_INT()等接收的第一个参数均为指向HashTable指针的指针 |
Beta Was this translation helpful? Give feedback.
-
发现一个小bug,图10-9的“7”行加法算错了,“7+15”=22不是23😄 |
Beta Was this translation helpful? Give feedback.
-
chapter_searching/replace_linear_by_hashing/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/
Beta Was this translation helpful? Give feedback.
All reactions