资讯专栏INFORMATION COLUMN

A flaw of java knowledge found by taking leetcode

impig33 / 562人阅读

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.

The problem looks like follows:

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.

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

And this is my java code:

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.

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.

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

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

Then the fatal one appears in my mind

~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!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70532.html

相关文章

  • LeetCode 之 JavaScript 解答第8题 —— 字符串转换整数 (String to

    摘要:当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。数字前正负号要保留。 Time:2019/4/19Title: String To IntegerDifficulty: MediumAuthor: 小鹿 题目:String To Integer(字...

    zhisheng 评论0 收藏0
  • Leetcode 8 String to Integer (atoi)

    摘要:难度是标准库中的一个函数可以将字符串表示的整数转换为现在要求我们自己来实现它解题过程中主要有以下两点需要注意字符串开头可能出现或者需要处理使用来记录中间结果防止溢出下面是的解法 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If ...

    cod7ce 评论0 收藏0
  • [LeetCode] 210. Course Schedule II

    Problem There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as...

    zhkai 评论0 收藏0
  • [LeetCode] 568. Maximum Vacation Days

    Problem LeetCode wants to give one of its best employees the option to travel among N cities to collect algorithm problems. But all work and no play makes Jack a dull boy, you could take vacations in ...

    468122151 评论0 收藏0
  • [Leetcode] Course Schedule 课程计划

    Course Schedule I There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is e...

    Amio 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<