Skip to content

Latest commit

 

History

History
387 lines (332 loc) · 20.6 KB

READMEOld.md

File metadata and controls

387 lines (332 loc) · 20.6 KB

Today Update

(Notes: "♥" Welcome to visit or fork or star my LeetCode Manager @ https://github.com/jackzhenguo/LeetCodeManager

Sort

324 Wiggle Sort II

  • 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;
         }
     }

Bit Mainpulation

342 Power of Four


About it

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.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-leetcode-csharp
  3. Commit your changes: git commit -am 'Add some questions and better solutions'
  4. Push to the branch: git push origin my-leetcode-csharp
  5. Submit a pull request and enjoy! :D

Solution List

solutions using C# for leetcode according to tags of questions Tags are following:

Details

Array

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

532 K-diff Pairs in an Array

217 Contains Duplicate

448 Find All Numbers Disappeared in an Array

219 Contains Duplicate II

189. Rotate Array

448 Find All Numbers Disappeared in an Array

219 Contains Duplicate II

566 Reshape the Matrix

561 Array Partition I

Hash Table

242 Valid Anagram

561 Array Partition I

438 Find All Anagrams in a String

204 Count Primes

500 Keyboard Row

389 Find the Difference

380 Insert Delete GetRandom O(1)

451 Sort Characters By Frequency

575 Distribute Candies

381 Insert Delete GetRandom O(1) - Duplicates allowed

Linked List

Math

7 Reverse Integer

202 Happy Number

453 Minimum Moves to Equal Array Elements

415 Add Strings

400 Nth Digit

69 Sqrt(x)

9 Palindrome Number

171 Excel Sheet Column Number

258 Add Digits

263 Ugly Number

326 Power of Three

Two Pointers

#234Palindrome Linked List

String

Binary Search

Tree

144 Binary Tree Preorder Traversal Stack version

94 Binary Tree Inorder Traversal Stack version

572 Subtree of Another Tree

103 Binary Tree Zigzag Level Order Traversal

95 Unique Binary Search Trees II

105 Construct Binary Tree from Preorder and Inorder Traversal