morry329#
IndexOutOfBoundsException triggered | LeetCode Where will the ball fall
Link to the puzzle description https://leetcode.com/problems/where-will-the-ball-fall/description/
I have been fighting with this puzzle for more than a month. Still my code cannot output correctly.
I tried this testcase
Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
Output: [1,-1,-1,-1,-1]
And my WIP code (it triggers IndexOutOfBoundsException at line int currRow = 0
. Could anyone kindly point me in the right direction?:
7 replies
A small hiccup for the LeetCode puzzle Where the ball will fall
Link to the original puzzle https://leetcode.com/problems/where-will-the-ball-fall/
My code does not print out the right output just yet
The output is
-1, -1, -1, -1 whilst it is supposed to be
1, -1, -1, -1, -1. The input here is
grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]`
Could anyone kindly point me in the right direction?1 replies
Not able to pass the test case correctly
The link to the original task https://leetcode.com/problems/where-will-the-ball-fall/
This is my WIP code
The output for this code is
==> -1 ==> -1 ==> -1 ==> -1 ==> -1
But it was supposed to be
==> 1 ==> -1 ==> -1 ==> -1 ==> -1` instead
I don't understand why my code stores -1 instead of 1 at the first index on my array. Could anyone kindly point me in the right direction?3 replies
Create Migration always fail
So I wanted to create a SQL Server database table based on your C# model. I have typed down
dotnet ef migrations add InitialCreate
but it never gets to migrate. The dotnet build
shows no noticeable errors like this
`
The link to all other database-related code https://pastebin.com/qFyNtM04
Could anyone point me in the right direction?4 replies
✅ MvC | IAction edit is not able to edit data
So I am following an YT tutorial for creating a CRUD operation with ASP.NET.
My tutorial code can create and read data but not update ( or edit). Whenever I tried to edit it pops the custom error message `
It was supposed to be the success message ``
Here's my code, could anyone point me in the right direction?
Controller
https://pastebin.com/fHLMY3jr
Edit.cshtml
https://pastebin.com/EmNXeEZ0
43 replies
IndexOutOfRangeException? Why??
I am analysing a solution for Spiral Matrix puzzle on LeetCode: https://leetcode.com/problems/spiral-matrix/
But I keep getting an IndexOutOfRange exception at this line
if (x >= r && !bol[y, x])
Could anyone kindly tell me why? Here is the whole solution code
`6 replies
RuntimeBinderException: '' does not contain 'Any()'
Would like another set of eyes on this exception.
I am following a YT tutorial to create a CRUD application via ASP.NET. Once I ran the code, this error popped out.
https://pastebin.com/7LqfcshB
Seems to me like this exception points out at those two lines in my cshtml file
@if(Model != null && Model.Any()){
(line 26)
and <h3>@ViewData["Title"]</h3>
(line 8).
I have added (at)using System.Linq
in cshtml file and all over classes with the name of Employee, but the exception persists.
Does anyone have an idea where it went wrong and triggered the exception?
EmployerController.cs
https://pastebin.com/VndL9jjG
cshtml file
https://pastebin.com/9FLYLwZc
EmployerViewModel.cs
https://pastebin.com/cVcw9vyQ
Employees.cs
https://pastebin.com/PWmaiNTC1 replies
The helper method and the char count do not work as I wanted
I wanted to solve this leetcode puzzle with recursion https://leetcode.com/problems/decode-string/
This is my WIP, the count variable (to count chars in the bracket) and the recursion line do not seem to work. For example, this code prints out
aabc
while it was supposed to be print out like aaabcbc
(for the case 3[a]2[bc]). Could anyone kindly point me in the right direction?
``4 replies
❔ Why is Pop() necessary here??
So I found a sample solution for the LeetCode decode string puzzle https://leetcode.com/problems/decode-string/
`
And I am confused with this line `
Why is it strings.Pop() needed here?? ChatGPT says it's not needed and it's redundant, I do not think that is the correct answer. Does the Pop remove the bracket [, so the code goes on to check part of the string??
5 replies
✅ why are they considered int instead of string?
So I was working on this puzzle https://leetcode.com/problems/backspace-string-compare/submissions/ I would like some advice on the compiler error/tipo (not the semantic/logical error to resolve this puzzle). My code is as follows, I am getting error
CS1503: Argument 1: cannot convert from 'string' to 'int' (in Solution.cs)
for two string initialisations inside the foreach-loop at the bottom. I had tried ToString()/Convert.ToString(), but to no avail. Could anyone kindly point me in the right direction?
public class Solution {
public bool BackspaceCompare(string s, string t) {
var dic1 = new Dictionary<string, string>(){
{" ", "#"},
{" ", "##"},
{" ", "###"}
};
var dic2 = new Dictionary<string, string>(){
{" ", "#"},
{" ", "##"},
{" ", "###"}
};
for(int i = 0; i < s.Length; i++){
string sStr = s[i].ToString();
string tStr = t[i].ToString();
if(!dic1.ContainsKey(sStr)){
dic1.Add(sStr, " ");
}
if(!dic2.ContainsKey(tStr)){
dic2.Add(tStr, " ");
}
}
foreach(var k in dic1.Keys){
string sForEach = s[k].Convert.ToString(); //ERROR HERE
string tForEach = t[k].Convert.ToString(); //ALSO HERE
if(dic2.ContainsKey(k) && sForEach == tForEach){
return true;
}
}
return false;
}
}
15 replies
❔ Getting a wrong result on LeetCode although the IDE shows the correct output
So I believe I still make some mistake in setting cows' Count for this puzzle https://leetcode.com/problems/bulls-and-cows/submissions/ For the test case string secret = "1807", guess = "7810";I get the wrong output 1A0B on the website while getting the correct one (1A3B) on Rider (IDE). Can anyone please point me in the right direction or is LC malfunctioning?
public class Solution {
public string GetHint(string secret, string guess) {
var dic = new Dictionary<char, int>();
int bulls = 0, cows = 0;
for (int i = 0; i < secret.Length; i++)
{
if (!dic.ContainsKey(secret[i]))
{
dic.TryAdd(secret[i], 0);
}
}
for (int i = 0; i < secret.Length; i++)
{
if (secret[i] == guess[i])
{
bulls++;
dic[secret[i]]++;
}
}
foreach (var k in dic)
{
/*if (!dic.TryGetValue(k.Key, out int val))
{
continue;
}*/
cows += Math.Min(k.Value, dic.GetValueOrDefault(guess[k.Key],0));
}
Console.WriteLine(bulls + "A" + cows + "B");
return $"{bulls}A{cows}B";
}
}
16 replies
✅ The code prints out no list elements
So I have been experimenting with this sample solution for this puzzle https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/?currentPage=1&orderBy=hot&query=&tag=c-2
public class Solution2
{
public List<int> FindAnagrams(string s, string p)
{
if (p.Length > s.Length)
{
return new List<int>();
}
List<int> result = new List<int>();
int[] mapP = new int[26];
foreach (var symbol in p)
{
mapP[symbol - 'a']++;
}
for (int i = 0; i < p.Length; i++)
{
if (IsAnagram(mapP))
{
result.Add(i - p.Length);
}
}
for (int i = p.Length; i < s.Length; i++)
{
mapP[s[i-p.Length] - 'a']++;
mapP[s[i] - 'a']--;
if (IsAnagram(mapP))
{
result.Add(i-p.Length+1);
}
}
Console.WriteLine($"return the list 2 ");
result.ForEach(Console.WriteLine);
return result;
}
bool IsAnagram(int[] map)
{
foreach (var count in map)
{
if (count != 0)
{
return false;
}
}
return true;
}
}
This code returns no list elements whilst it returns no exception/errors. Maybe can anyone explain to me what went wrong with it?7 replies
✅ What is the function of the Dictionary here?
public class Solution {
Dictionary<int,int>cached=new Dictionary<int,int>();
public int ClimbStairs(int n) {
if(cached.ContainsKey(n)) {
return cached[n];
}
int output = 1;
if(n<=3){
output = n;
} else {
output = ClimbStairs(n-1) + ClimbStairs(n-2);
} cached[n]=output; return output; } } So I wanted to understand the above solution for this LeetCode puzzle https://leetcode.com/problems/climbing-stairs/description/ I am still wondering what the Dictionary is doing here. My guess is that it prevents the code from exceeding the time limit (as my code went to exceed the limit without the Dictionary). But I appreciate any other plausible explanation for the Dictionary's presence here.
} cached[n]=output; return output; } } So I wanted to understand the above solution for this LeetCode puzzle https://leetcode.com/problems/climbing-stairs/description/ I am still wondering what the Dictionary is doing here. My guess is that it prevents the code from exceeding the time limit (as my code went to exceed the limit without the Dictionary). But I appreciate any other plausible explanation for the Dictionary's presence here.
7 replies