canton7
✅ Need help implementing support for quoting with single quotes
(There are times when regexes are great, and there are times when breaking out a manual loop is an awful lot easier. This problem in particular is a lot easier with a manual loop)
15 replies
✅ Need help implementing support for quoting with single quotes
Inside the
else
in the loop you could add the chars to an array sure, but it would be easier to use a StringBuilder. (Or you could track the starting index of the current arg and then use Substring
, but that falls down when you come to escape quotes).
Inside the else if (ch == ' ' && !inQuotes)
, that's when you find a space that isn't in quotes, i.e. the end of one argument and the start of another. So that's where you turn the current in-progress argument into a string, probably add it to a list of string args, and clear the StringBuilder.
Outside of the loop you need to check whether you're in the middle of processing an arg, and finish it if so. The if (inQuotes)
means that you finished processing with mis-matched quotes, so maybe you need to raise an error15 replies
✅ Need help implementing support for quoting with single quotes
(That will really come in handy when you come to having to escape quotes, which is something that is really painful with regex, if you can make it work at all. Since quote escapes don't form a regular language IIRC)
15 replies
Prefix sum with Hashmaps
Actually "Approach #2" here explains it well: https://leetcode.com/problems/contiguous-array/editorial/
9 replies
Prefix sum with Hashmaps
So, image walking through the array, element by element. If we see a 0 we subtract 1 from
count
, and if we see a 1 we add 1 to count
. Let's take the Example 3 from the leetcode page (0,1,1,1,1,1,0,0,0), and we'll record count
against each element:
If we calculate a count
value which is equal to a count
value that we calculated for a previous index (for example, at index 6 we calculate a count
value of 3, which is the same as the count
for index 4), then we know there must be an equal number of 1's and 0's between those two indexes. Because we had to sum an equal number of 1's and 0's to get back to where we started.
So, we keep track of the first position at which we first found each count
. And when we calculate a count
which we've seen before, we look at the number of elements between that first position and the current position: that's the length of the sub-array between those two positions. Then we keep track of the max length of such sub-arrays, and that's our answer.9 replies
✅ What are some more ways I can asynchronously wait on callbacks
You linked https://stackoverflow.com/questions/18756354/wrapping-manualresetevent-as-awaitable-task, which uses it in most of the answers
12 replies