Thursday, August 30, 2018

Leetcode 448. Find All Numbers Disappeared in an Array - My struggle

August 30, 2018

Introduction


I have to figure out how to complete my broken solution afterwards. I found the optimal solution by reading one of discuss, I wrote the similar one. But I like to continue to write the one much complicated, and then I need to figure out how to make it work as well.

My idea is too complicated and I could not come out the solution. My design is like the following.

[3, 1, 2],
Line 1:  First, index = 0, value 3, so 3 should be put at index = 2,
         2: Next, index = 0, put -1,
         3: Next, index = 2, value 2 is save as a variable
         4: Next, index = 2, put 3,
         5: Next, work on value 2, put its position to index = 1 if need.
I introduce negative one value as the array's temporary value, the change brings into so many user cases. It affects line 5, line 1. The value has to be checked first, since now there is an empty element concept now.

The lesson learned is that I have to evaluate all options first, it is better to choose easy and short solution to write. Evaluate user cases before I write code for the solution.

My practice 


I found out that I could not make the code working, since there are so many cases to handle related to -1 value.

I chose to use extra space to save some elements in the array. And I wrote C# code to pass the test cases. Here is my C# practice.


No comments:

Post a Comment