HT350IntersactionOfTwoArrays2
来源:互联网 发布:淘宝店铺图片授权 编辑:程序博客网 时间:2024/06/06 18:43
Clue
- 最大的提点是,一旦发现map里有,可以直接加入另一个result array而不需要在当前map上标记是否是intesaction。来源https://discuss.leetcode.com/topic/45920/ac-solution-using-java-hashmap *
Logic Flaw
- 之前确定了longArray和shortArray,感觉用shortArray做map会更快,但是却忘了另一个array决定着答案的数量
- 忘记了value并非自然数,可以为负数,如果加入result,需要确定value>0
- 在用arraylist复制到int[]的时候,注意如果用_arraylist.remove(i)会改变原结构, i会溢出.
BinarySearch的bugs
- 在用BinaryArray的时候, 找到相等的, 应该向前追溯到第一个,作为startIndex
- 但要注意,小心i == 1啊
private static int binarySearch (int[] num, int start, int end, int target) { while(start <= end) { int mid = start + (end - start)/2; if (num[mid] == target) { while (mid > 1 && mid > start && num[mid] == num[mid - 1]) --mid; return mid; } else if (num[mid] > target) { end = mid - 1; } else { start = mid + 1; } } return -1;}
- 看中间的while,其实没必要, 其实可以继续用binary算法,就当作num[mid] > target
- 并且除去上面的bug 也还有问题, mid == start 的时候就不返回了,这样不对.
Attention
- If compare two sorted arrays, we usually don’t use self-increased indexes, like for loop
- Don’t forget to increase both indexes if equals
0 0
- HT350IntersactionOfTwoArrays2
- queryTask,geometryService操作,通过画矩形获得外接图形
- 72. Edit Distance
- 谷歌Cartographer学习(1)-快速安装测试
- window上使用放大镜功能时显示错误问题
- Arduino使用PN532注意问题#NFC
- HT350IntersactionOfTwoArrays2
- 删除文件夹下所有的.git文件
- Round B APAC Test 2017 Problem D. Sherlock and Permutation Sorting
- JavaScript个人笔记:面向对象设计二(继承)
- nc批量检测代理服务器端口可用性
- [四]java作业
- CentOS6.5下为编译Caffe升级python2.6到2.7
- 100亿数据排序
- 计算广告的指标