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!….
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
摘要:当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。数字前正负号要保留。 Time:2019/4/19Title: String To IntegerDifficulty: MediumAuthor: 小鹿 题目:String To Integer(字...
摘要:难度是标准库中的一个函数可以将字符串表示的整数转换为现在要求我们自己来实现它解题过程中主要有以下两点需要注意字符串开头可能出现或者需要处理使用来记录中间结果防止溢出下面是的解法 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If ...
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...
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 ...
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...
阅读 915·2021-11-08 13:22
阅读 2840·2021-09-29 09:45
阅读 2823·2021-09-09 11:52
阅读 2256·2019-08-30 13:20
阅读 3738·2019-08-29 13:28
阅读 1354·2019-08-29 12:32
阅读 2719·2019-08-29 11:10
阅读 1642·2019-08-26 13:34