简体   繁体   中英

How do I set up an environment to solve LeetCode problems in Visual Studio? C#

When making the file, I am thinking of selecting a console application. But which target framework do I choose? Is this incorrect? Also, I am having trouble figuring out how to make a method in the class Program that is able to be called in the Main method. Can someone give me some advice?

This will get you started with Visual Studio:

  1. Create a new console project - use the latest version of C#, which is probably what VS will "suggest" to you. Currently that's .NET 6 or .NET 7

  2. A modern (net 6 or later) console app lets you start writing code immediately. You could create a method and then call the method right in this little Program.cs file that you start out with. However, I would probably do the following instead:

a) Create a new class for your "problem"

b) In that class create a method that solves the problem.

c) In your Program.cs add a using statement to use the namespace that your new class uses

d) In your program.cs instantiate that class and call its method/test its method Here is an example:

Program.cs

using LeetCodeProject;

var solver = new Problem001_CalculateSquareRoot();
var solution = solver.calculate_square_root(8);
Console.WriteLine(solution);

Console.WriteLine("Press any key...");
Console.ReadKey();

Problem001_CalculateSquareRoot.cs (solves one leetcode problem)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCodeProject
{
    public class Problem001_CalculateSquareRoot
    {

        public double calculate_square_root(int number)
        {
            double root = 1;
            int i = 0;
            while (true)
            {
                i = i + 1;
                root = (number / root + root) / 2;
                if (i == number + 1)
                { 
                    break; 
                }
            }
            return root;
        }
    }
}

Now you can just add new classes for each problem, and as you work on them just edit Program.cs to create the class you are currently working with and calls its solution methods.

I can (and would - and actually have, in similar cases) implement an interface for this, but the goal here is not to get into OO design principles, but just to get you started so you can get to work on the leetcode problems...once you have a few done you can start thinking about better organization of the code.

one thing you can do is using interface to keep your code clean; for example :

you create an interface like this:

public interface IQuestionSolving
{
    public void Solution();
}

you create some question class :

public class Question1 : IQuestionSolving
{
    public void Solution()
    {

    }
}

and you use it like this :

static void Main(string[] args)
    {
        IQuestionSolving solve = new Question1();
        solve.Solution();
        Console.ReadKey();
    }

now each time you solve a question you need to change

IQuestionSolving solve = new Question1();

to

IQuestionSolving solve = new Question2(); // 2 3 4 .. etc

you can extract your project as template so you dont have to do this each time . or you can just use one solution and many classes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM