I have seen in many questions that the C# code times out, but the very same code in Java (and probably other languages) executes successfully.
Beside of that, I run a code in Java which runs in less than 5ms but the same code would take more than 100ms in C# (if doesn't time out!).
This issue makes me have to check my code over and over again and finally have to give up. Is there any way we can match the time out between C# and other languages, or increase C# time out?
The last time I have run into this issue is on Q298.
Java code copied from LeetCode Solution, executes in 3ms:
private int maxLength = 0;
public int longestConsecutive(TreeNode root)
{
dfs(root, null, 0);
return maxLength;
}
private void dfs(TreeNode p, TreeNode parent, int length)
{
if (p == null) return;
length = (parent != null && p.val == parent.val + 1) ? length + 1 : 1;
maxLength = Math.max(maxLength, length);
dfs(p.left, p, length);
dfs(p.right, p, length);
}Literally the same code in C# times out. Just had to change 'longestConsecutive' to 'LongestConsecutive' and 'Math.max' to 'Math.Max'.
private int maxLength = 0;
public int LongestConsecutive(TreeNode root)
{
dfs(root, null, 0);
return maxLength;
}
private void dfs(TreeNode p, TreeNode parent, int length)
{
if (p == null) return;
length = (parent != null && p.val == parent.val + 1) ? length + 1 : 1;
maxLength = Math.Max(maxLength, length);
dfs(p.left, p, length);
dfs(p.right, p, length);
}In addition, I would like to report another strange issue that I have encountered in one of my submissions.
This is regarding Q482.
The code fails on the last test case 35/35.
Input= "-", K=2
Even tough I had to hard code the condition to check "-" in the first line, it still throws Memory Limit Exceeded error.
public string LicenseKeyFormatting(string S, int K)
{
if (S == "-") return "";
string withOutDashes = RemoveDashes(S);
int d = withOutDashes.Length % K;
int counter = d;
StringBuilder res = new StringBuilder();
for (int i = 0; i < withOutDashes.Length; i++)
{
if (i == counter)
{
if (i != 0)
res.Append("-");
counter += K;
}
res.Append(withOutDashes[i]);
}
return res.ToString().ToUpper();
}
private string RemoveDashes(string s)
{
string result = "";
for (int i = 0; i < s.Length; i++)
{
if (s[i] != '-') result += s[i];
}
return result;
}