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!
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/70532.html
摘要:當(dāng)我們尋找到的第一個非空字符為正或者負號時,則將該符號與之后面盡可能多的連續(xù)數(shù)字組合起來,作為該整數(shù)的正負號假如第一個非空字符是數(shù)字,則直接將其與之后連續(xù)的數(shù)字字符組合起來,形成整數(shù)。數(shù)字前正負號要保留。 Time:2019/4/19Title: String To IntegerDifficulty: MediumAuthor: 小鹿 題目:String To Integer(字...
摘要:難度是標(biāo)準(zhǔn)庫中的一個函數(shù)可以將字符串表示的整數(shù)轉(zhuǎn)換為現(xiàn)在要求我們自己來實現(xiàn)它解題過程中主要有以下兩點需要注意字符串開頭可能出現(xiàn)或者需要處理使用來記錄中間結(jié)果防止溢出下面是的解法 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...
閱讀 925·2021-11-08 13:22
閱讀 2849·2021-09-29 09:45
閱讀 2827·2021-09-09 11:52
閱讀 2262·2019-08-30 13:20
閱讀 3747·2019-08-29 13:28
閱讀 1362·2019-08-29 12:32
閱讀 2726·2019-08-29 11:10
閱讀 1648·2019-08-26 13:34