Giter VIP home page Giter VIP logo

algorithm's People

Contributors

guokaide avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

algorithm's Issues

剑指offer52FirstCommonNode

在对两个链表长度判断得到较长或者较短的地方,忽略了两个链表相等的问题,导致测试不通过:
ListNode longListHead = len1 > len2 ? pHead1 : pHead2;
ListNode shortListHead = len1 < len2 ? pHead1 : pHead2;

第二句修改为:
ListNode shortListHead = len1 <= len2 ? pHead1 : pHead2;

问题解决

剑指Offer_63_MaxProfit

`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;
}`

剑指offer22 FindKthNodeToTail.java

对于前面的指针先走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;
    }`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.