guokaide / algorithm Goto Github PK
View Code? Open in Web Editor NEW「面试算法练级攻略」 - 「LeetCode题解」 - 「剑指offer题解」
「面试算法练级攻略」 - 「LeetCode题解」 - 「剑指offer题解」
在对两个链表长度判断得到较长或者较短的地方,忽略了两个链表相等的问题,导致测试不通过:
ListNode longListHead = len1 > len2 ? pHead1 : pHead2;
ListNode shortListHead = len1 < len2 ? pHead1 : pHead2;
第二句修改为:
ListNode shortListHead = len1 <= len2 ? pHead1 : pHead2;
问题解决
`for (int i = 2; i < prices.length; i++) {
int curDiff = prices[i] - minPrice;
if (curDiff > maxDiff) {
maxDiff = curDiff;
}
if (prices[i] < minPrice) {
minPrice = prices[i];
}
}`
此处更新最低价格的时机不太对,如果在计算结束计算价格的话,会忽略 i = 1位置价格最低的情况。
样例:
{9,1,8,5,7,12,11,9}
输出是7
正确输出11
建议:
`public int calc(int[] array){
if (array == null || array.length < 2) {
return -1;
}
int min = array[0];
int profit = array[1] - array[0];
for(int i = 2; i < array.length; i++){
if(array[i-1]<min){
min = array[i-1];
}
if(array[i] - min > profit){
profit = array[i] - min;
}
}
return profit;
}`
对于前面的指针先走K步的做法欠妥,当k大于长度的时候,将会出现空指针异常。
for (int i = 0; i < k-1; i++) { before = before.next; } // 2. n < k: 第k个元素before已经到null,则k > n if (before == null) return null;
建议:
`int length = k;
//先让前面的指针走K步,然后在一起走,前面的指针走到最后,后面的指针指向倒数第K个节点
while(length > 1 && node2 != null)
{
node2 = node2.next;
length--;
}
if(length > 1 || node2 == null)
{
return null;
}`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.