A flaw of java knowledge found by taking leetcode Contest

This morning , I took my first leetcode online.
However, that is far beyond what I expected . I didn’t make it to finish in 1 hour and 30 minutes. But via solving the 1st problem , which is marked easy, I realized that I’m not that perfectly familiar with java.java

The problem looks like follows:git

You're now a baseball game point recorder.>
Given a list of strings, each string can be one of the 4 following types:
Integer (one round's score): Directly represents the number of points you get in this round.>
「+」 (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.>
「D」 (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.>
「C」 (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.>
Each round's operation is permanent and could have an impact on the > round before and the round after.app

You need to return the sum of the points you could get in all the rounds.less

And this is my java code:ide

class Solution {
    public int calPoints(String[] ops) {
        int sum=0;
        int len=ops.length;
        boolean valid[] = new boolean[len];
        int point[] = new int[len];
        
        for (int i = 0; i < len; i++) {
            valid[i]=true;
            point[i]=0;
        }

        for (int i = 0; i < len; i++) {
            if (isdigit(ops[i])) { 
                point[i] = Integer.parseInt(ops[i]);
                sum += point[i];
            }
            else if (ops[i].equals("+")) {
                int j=i-1;
                while (j>=0) {
                    if (valid[j]) {
                        point[i] += point[j];
                        break;
                    }
                    j--;
                }
                j=j-1;
                while (j>=0) {
                    if (valid[j]) {
                        point[i] += point[j];
                        break;
                    }
                    j--;
                }
                sum+=point[i];
            }
            else if (ops[i].equals("D")) {
                int j=i-1;
                while (j>=0) {
                    if (valid[j]) {
                        point[i] += 2*point[j];
                        sum+=point[i];
                        break;
                    }
                    j--;
                }
            }
            else if (ops[i].equals("C")) {
                valid[i]=false;
                int j=i-1;
                while (j>=0) {
                    if (valid[j]) {
                        valid[j] = false;
                        sum-=point[j];
                        break;
                    }
                    j--;
                }
            }
        }
        return sum;
    }
    public boolean isdigit(String str) {
        int len = str.length();
        char [] a;
        a = str.toCharArray();
        
        for (int i = 0; i < a.length; i++) {
            if (!(a[i]>='0'&&a[i]<='9')) {
                return false;
            }
        }
        return true;
    }
}

I spent about 30 minutes to finish it but 2 hours to figure out what exactly is wrong with my code, cuz I ran it on my local IDE and it turned out to be right.this

However, when I paste the code on the online editor and ran it , it just could not get the right answer. Which make me very confused.
I did debug on the playground, and first, I found that the length of the array is not the correct one. Thus I begin to doubt if the array.length return the correct number. google

But it’s right, I searched online , it is the one.debug

Then I found that my code cannot enter the 「D」,「C」 and 「+」 section.
Why?code


Then the fatal one appears in my mindleetcode

~if the code does not enter that two section, so the condition must be wrong, thus I come to look at the 「=「, ~

And I googled it .
OMG!….

equals will only compare what it is written to compare, no more, no less. That being said, the equals() method compares the "value" inside String instances (on the heap)

the "==" operator compares the value of two object references to see whether they refer to the same String instance.

Just due to this tiny difference, it cost me more than 2 hours!A SOLID FOUNDATION OF LANGUAGE IS FAR MORE IMPORTANT THAN YOU EXPECTED!

相關文章
相關標籤/搜索