Giter VIP home page Giter VIP logo

leetcodeincsharp's People

Contributors

bysz71 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leetcodeincsharp's Issues

Functional flavored solution to Problem 137

Same idea as that in this repo. Just more functional style. Perhaps it may be a start to get used to lambda and Linq.

    public int SingleNumber(int[] nums) 
    {
        var dic = new Dictionary<int, int>();
        new HashSet<int>(nums).ToList().ForEach(n => dic[n] = 0);
        nums.ToList().ForEach(n => ++dic[n]);
        return dic.Where(p => p.Value == 1).First().Key;
    }

Complexity Analysis of 13 Roman to Integer

Please note the fact that the size of the dictionary you used, which has been deleted in a8627ed, has nothing to do with argument size. Hence the solution using dictionaries takes only O(1) time and O(1) extra space. Actually, as used in the current implementation, multiple if else statements are considered to be slower than Hash Table and switch cases statement. Basically, if else takes O(n) time. A reference can be found here : If else vs switch on SO.

That said, the version deleted in a8627ed still can be improved, because dictionaries are good at lookup rather than iteration, wherras array (i.e. String s) good at iteration rather than lookup.

I implemented another version with same idea as that deleted, like following:

public class Solution 
{
    public int RomanToInt(string s) 
    {
        var twos = new Dictionary<string, int>()
        {
             { "CM", 900 }, { "CD", 400 }, { "XC", 90 }, { "XL", 40 }, { "IX", 9 }, { "IV", 4 }
        };
        var ones = new Dictionary<string, int>()
        {
            { "I", 1 }, { "V", 5 }, { "X", 10 }, { "L", 50 }, { "C", 100 }, { "D", 500 }, { "M", 1000 }
        };

        int sum = 0;
        for(int i = 0; i < s.Length;)
        {
            if(i + 1 == s.Length || !twos.ContainsKey(s.Substring(i, 2)))
            {
                sum += ones[s[i].ToString()];
                i += 1;
            }
            else
            {
                sum += twos[s.Substring(i, 2)];
                i += 2;
            }
        }
        return sum;
    }
}

AddRange in #144 Binary tree pre-order traversal

Please notice the semantics of method AddRange. Basically, it copies items from source collections. The source items will be reclaimed by CLR some time later. So the current implementation in this repo wastes a lot of spaces.

Like following, I wrote another implementation using Concat to reduce this overhead.

    public IList<int> PreorderTraversal(TreeNode root) 
    {
        if (root == null) 
            return new List<int>();
        else
            return new List<int>{ root.val }
                .Concat(PreorderTraversal(root.left))
                .Concat(PreorderTraversal(root.right))
                .ToList();
    }

Suggestion about removing unnecessary 'using'

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

Generated by VS, such statements can be found in almost every file in this repo. Just suggest to remove these using NeverUsedNameSpace. I think this should be a good practice.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.