leetcodeincsharp's People
Forkers
lipingwang2015 liusc1028 beginnerl lilei132 407insomnia tianxuemu bill666500 3zhangsen bwjh520 jimmywang1994 exploreying hsnowman congqi808 eastww m5jun yasminetym oneseral zjhzjhzjh1026 chickenlove keymea yanglr woying456 xiaomanzijia jaydengui suzeyinhappyboy danielbrezoi goodniuniu kimbin cvqw503 gongfupanada jack2949 selftool zhaoyan211314 jianminchen gaohongbin yu-qiao-hong whtsogreat layne22 panchengtao wdxgtsh fengjiang96 skywp leon-zhou terriermon jasonscor sprout2011 victorque hooping zzg6 unoiou zuxbabyworld smilemilk1992 forrest15 biganans yuixc ruim-mr windygu xiao------xiao xuhuawei131 lpflpy suzy323 founderisshadowwalker interestdictateshero nerissa0505 sandbox3aster pingpi357 shangshanruoshui77 largesweetpotato hhuang2013 anchorhanme jzhong4 laoji525 zhaoshiling1017 johnsonjohnsonyuan bomby33 qwe81301 wcc3358 pyj125 wangweiyu0819 lzroc tifosi-m lingboweibu1234 melonfish caojinqin happybearzzz williambryan777 buffbu ylgf eva-ya tabzhangjx yunkaiyueming v-chfeng zoeh37 neverwell guanxiaonan liuhouting guxiaodai cuiyijing wanglong williami-oneleetcodeincsharp'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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.