Algorithm is tool for exercising our thinking patterns, and we can strengthen the ability to convert mathematical models into code. Whether you are engaged in artificial intelligence, in-depth learning, or advanced software development, no matter what language you use, such as C#,C++,Java,python,etc., and applying the most appropriate algorithm is always the most important point when faced with a specific problem. Every problem in practice has its own particularity, which makes it not that easier to choose the most appropriate algorithm. How do we write the algorithm that most efficiently apply to a practical issue? Yes, LeetCode. You can write an algorithm until it accepted, and do not rush to do the next question, and learn the solution someone else has submitted, so you can solve the problem from the ability of solving the problem to that fast and efficient realm
.
I create this respository called leetcode-csharp because I apply C# language to solove LeetCode and every day
will update it and also publish it in CSDN blog
(http://blog.csdn.net/daigualu) my blog column(http://blog.csdn.net/column/details/14761.html) Also, I will write some famous algorithm ideas on My Wiki for this repository such asFlody tortoise and hare
and KMP
and so on.
Anyway, welcome to view, star and fork, then contribute.
- Fork it!
- Create your feature branch: git checkout -b my-leetcode-csharp
- Commit your changes: git commit -am 'Add some questions and better solutions'
- Push to the branch: git push origin my-leetcode-csharp
- Submit a pull request and enjoy! :D
- Github:#400 Nth Digit
- CSDN:#400 Nth Digit
- Tips:
- careful to prevent overflowing for bas*digits, so declaring bas is long.
//for this issue, there are two different ways to decribe a number //1 element. this is our common way //2 Nth digit. this is a new way public int FindNthDigit(int n) { long bas = 9; int digits = 1, i = 0; //first: getting n which digit is in while (n > bas * digits) // prevent overflowing. Since bas is long, so result of bas*digits is auto imporved as long { n -= (int)(bas * (digits++)); //nth i += (int)bas; //number of pasted elements bas *= 10; //1 digit->9; 2 digits->90; 3 digits->900, ... } //second: Nth digit ->element //in all numbers containing digits, pasted numbers int pasted = (int)((n - 1) / digits); int element = pasted + i + 1; //third: once getting the element Nth digits stands, //(n-1)%digits of element is solution int nth = (n - 1) % digits; return element.ToString()[nth] - '0'; }
- Tips:
- Github:#69 Sqrt(x)
- CSDN:#69 Sqrt(x)
- Tips:
- careful to prevent overflowing! Again careful to overflow!
public int MySqrt(int x) { int lo = 0, hi = x ; while (lo - hi < -1) { //get [lo,hi] middle point,then compare pow2 to x, // lo or hi is setted by mid //so accelarate the process long mid = lo + (hi - lo) / 2; //prevent overflowing long pow2 = mid * mid; //prevent overflowing if (pow2 < x) lo = (int)mid; else if (pow2 > x) hi = (int)mid; else return (int)mid; } return lo; }
- Tips:
solutions using C# for leetcode according to tags of questions Tags are following:
- Github:#448 Find All Numbers Disappeared in an Array
- CSDN:#448 Find All Numbers Disappeared in an Array
- Github:#448 Find All Numbers Disappeared in an Array
- CSDN: #448 Find All Numbers Disappeared in an Array
- Github:#451 Sort Characters By Frequency
- CSDN:#451 Sort Characters By Frequency
- this problem solving is not by sorting order by frequency of chars! please read it.
- Github:# 381 Insert Delete GetRandom O(1) - Duplicates allowed
- CSDN:#381 Insert Delete GetRandom O(1) - Duplicates allowed
- Github:#7 Reverse Integer
- CSDN:#7 Reverse Integer
- Tips:
- an interesting way to check if happens overflow.
- Github:#202 Happy Number
- CSDN:#202 Happy Number
- Floyd's Tortoise and Hare
- reference:https://en.wikipedia.org/wiki/Cycle_detection
- Github:#453 Minimum Moves to Equal Array Elements
- CSDN:#453 Minimum Moves to Equal Array Elements
- Tips:
- using Math equation to solve this issue!
- Github:#415 Add Strings
- CSDN:#415 Add Strings
- Tips:
- this is an interesting question!
- Github:#144 Binary Tree Preorder Traversal Stack version
- CSDN:#144 Binary Tree Preorder Traversal Stack version
- Github:#94 Binary Tree Inorder Traversal Stack version
- CSDN:#94 Binary Tree Inorder Traversal Stack version
- Github:#572 Subtree of Another Tree
- CSDN:#572 Subtree of Another Tree
- Subtree of Another Tree: it's extended from problem of "is same tree".
- Github:#103 Binary Tree Zigzag Level Order Traversal
- CSDN:#103 Binary Tree Zigzag Level Order Traversal
- Github:#95 Unique Binary Search Trees II
- CSDN:#95 Unique Binary Search Trees II
- this is the famous "Catalan number", please reference https://en.wikipedia.org/wiki/Catalan_number
- Github:#105 Construct Binary Tree from Preorder and Inorder Traversal
- CSDN:#105 Construct Binary Tree from Preorder and Inorder Traversal
- Tips:
- the most important function in solving this issue is
- private TreeNode bulidTree(int preStart, int inStart, int inEnd) ;
- Plus, preStart index in preorder is the root index, which is also the separate point in inorder and it’s left is left subtree and right is right subtree.
- the most important function in solving this issue is