(Notes: "♥" Welcome to visit or fork or star my LeetCode Manager @ https://github.com/jackzhenguo/LeetCodeManager
- Github:#324 Wiggle Sort II
- CSDN:#324 Wiggle Sort II
- This is a great question for it involves a great idea: virtual index
/// <summary> /// WiggleSort /// </summary> public class WiggleSortSln { private static int[] _array; public int[] WiggleSort(int[] array) { _array = array; int median = findKThLargest(_array.Length / 2); int left = 0, i = 0, right = _array.Length - 1; while (i <= right) { if (_array[newIndex(i)] > median) { //put newIndex(i) at odd index(from 1, 3, to 5, ...) swap(newIndex(left++), newIndex(i)); i++; } else if (_array[newIndex(i)] < median) { //put newIndex(i) at even index(max even index to little .... ) swap(newIndex(right--), newIndex(i)); //right--, so i relatively toward right 1 step } else { i++; } } return _array; } private int newIndex(int index) { return (1 + 2 * index) % (_array.Length | 1); } private void swap(int i, int j) { int tmp = _array[i]; _array[i] = _array[j]; _array[j] = tmp; } //based on quick sort to find the Kth largest in _array private int findKThLargest(int k) { int left = 0; int right = _array.Length - 1; while (true) { int pivotIndex = quickSort(left, right); if (k == pivotIndex) return _array[pivotIndex]; else if (k < pivotIndex) right = pivotIndex - 1; else left = pivotIndex + 1; } } private int quickSort(int lo, int hi) { int key = _array[lo]; while (lo < hi) { while (lo < hi && _array[hi] >= key) hi--; //hi is less than key, hi element moves to lo index _array[lo] = _array[hi]; while (lo < hi && _array[lo] < key) lo++; //lo is bigger than key, lo element moves to hi index _array[hi] = _array[lo]; } _array[lo] = key; return lo; } }
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 solve 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 put some algorithm ideas that famous scientists have created on My Wiki for this repository such as Flody 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
solutions using C# for leetcode according to tags of questions Tags are following:
Number | Title(Blog URL) |
---|---|
35 | Search Insert Position |
118 | Pascal's Triangle |
119 | Pascal's Triangle II |
414 | Third Maximum Number |
121 | Best Time to Buy and Sell Stock |
66 | Plus One |
26 | Remove Duplicates from Sorted Array |
27 | Remove Element |
122 | Best Time to Buy and Sell Stock II |
268 | Missing Number |
217 | Contains Duplicate |
532 | K-diff Pairs in an Array |
189 | Rotate Array |
169 | Majority Element |
167 | Two Sum II - Input array is sorted |
88 | Merge Sorted Array |
53 | Maximum Subarray |
485 | Max Consecutive Ones |
283 | Move Zeroes |
448 | Find All Numbers Disappeared in an Array |
1 | Two Sum |
219 | Contains Duplicate II |
566 | Reshape the Matrix |
561 | Array Partition I |
- 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:#400 Nth Digit
- CSDN:#400 Nth Digit
- Tips:
- careful to prevent overflowing for bas*digits, so declaring bas is long.
- Tips:
- Github:#69 Sqrt(x)
- CSDN:#69 Sqrt(x)
- Tips:
- careful to prevent overflowing! Again careful to overflow!
- Tips:
- Github:#258 Add Digits
- CSDN:#258 Add Digits
- Tips:
- In Indian mathematics, a Vedic square is a variation on a typical 9 × 9 multiplication table where the entry in each cell is the digital root of the product of the column and row headings i.e. the remainder when the product of the row and column headings is divided by 9 (with remainder 0 represented by 9). Numerous geometric patterns and symmetries can be observed in a Vedic square some of which can be found in traditional Islamic art.
- 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