DesignPatterns.Factory

MyCodeExample

Posted in Без рубрики | Comments Off on DesignPatterns.Factory

Leetcode.Solved.ContainsDublicate

// https://leetcode.com/problems/contains-duplicate
public class Solution {
    public bool ContainsDuplicate(int[] nums) {        
        Array.Sort(nums);

        for (int i = 0; i < nums.Length - 1; i++) {
            if (nums[i] == nums[i+1])
              return true;
        }  
             
        return false;
    }
}
// https://leetcode.com/problems/contains-duplicate
public class Solution {
    public bool ContainsDuplicate(int[] nums) {
        
        HashSet<int> set = new HashSet<int>();

        for (int i = 0; i < nums.Length; i++) {
            if (set.Contains(nums[i]))
              return true;
              
            set.Add(nums[i]);
        }  
             
        return false;
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.ContainsDublicate

Leetcode.Solved.Majority-element

// https://leetcode.com/problems/majority-element/
// Solved with Boyer–Moore majority vote algorithm
// https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
public class Solution {
    public int MajorityElement(int[] nums) {                
        int majority = nums[0];
        int count = 0;

        for (int i = 0; i < nums.Length; i++) {
            if (count == 0)
               majority = nums[i];                     
            
            if (majority == nums[i])
              count++;
            else count--; 
        }

        return majority;        
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.Majority-element

Leetcode.Solved.MergeSortedArray

// https://leetcode.com/problems/merge-sorted-array
// O(n) = nlog(n)
public class Solution {
    public void Merge(int[] nums1, int m, int[] nums2, int n) {
      
      int j = 0;
      int i = m;
      while (j < n) {       
       nums1[i] = nums2[j];
       i++;
       j++;
      }   

      Array.Sort(nums1);        
    }
}
// https://leetcode.com/problems/merge-sorted-array
// O(n) = n
public class Solution {
    public void Merge(int[] nums1, int m, int[] nums2, int n) {
      // make a copy of nums1
      int[] nums1copy = new int[m];
      for (int i1 = 0; i1 < m; i1++) {
        nums1copy[i1] = nums1[i1]; 
      }

      int i = 0;
      int j = 0;
      int k = 0;

      while (i < m || j < n) {        
        // intersection
        if (i < m && j < n) {
          if (nums1copy[i] < nums2[j]) {
            nums1[k++] = nums1copy[i++];
          }
          else if (nums1copy[i] > nums2[j]) {
            nums1[k++] = nums2[j++];
          }
          else {
            nums1[k++] = nums1copy[i++];
            nums1[k++] = nums2[j++];          
          }
        } 
        // rest nums1copy
        else if (i < m)        
         nums1[k++] = nums1copy[i++];
        // rest nums2
        else if (j < n)                 
         nums1[k++] = nums2[j++];
                        
 
       //if (i >= m && j >= n)
       //   break; 
      };                      
    }
   
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.MergeSortedArray

Leetcode.Solved.PlusOne

// https://leetcode.com/problems/plus-one/submissions/
public class Solution {
  public int[] PlusOne(int[] digits)
        {
            int[] toadd = new int[digits.Length];
            toadd[toadd.Length - 1] = 1;
            var numbers = new Stack<int>();            
            
            int inMemory = 0;
            for (int i = digits.Length - 1; i >=0; i--)
            {                
                int number = digits[i] + toadd[i] + inMemory;
                if (number > 9)
                {
                    string numberStr = number.ToString();                    
                    inMemory = numberStr[0] - '0';
                    numbers.Push(numberStr[1] - '0');
                }
                else
                {
                    inMemory = 0;
                    numbers.Push(number);                    
                }
            }

            if (inMemory > 0)
              numbers.Push(inMemory);

            int[] res = new int[numbers.Count];
            int j = 0;

            while (numbers.Count > 0)
            {

                var number = numbers.Pop();
                res[j] = number;
                j++;                
            }


            return res;
        }
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.PlusOne

Leetcode.Solved.SingleNumber

/*
Approach
Using Bit Manipulation -

As we know XOR operation with 0 gives the same number
i.e, a XOR 0 = a
eg, for decimal no. 2=> 2 XOR 0 = 2
in binary, 010 XOR 000 = 010

Also we know that , XOR operation with same number gives 0
i.e, a XOR a = 0
eg, 2 XOR 2 = 0
in binary, 010 XOR 010 = 000

XOR is associative (like sum)
i.e, (2 XOR 3) XOR 4 = 2 XOR (3 XOR 4), So the order doesn't matter in performing XOR operation.
eg, 2^3^4^6 = 3^2^6^4 = 4^2^6^3 ......

So, using these three properties of XOR , we will solve the question. we will take ans variable with 0 as initial value. And then for each element i in array, we will perform the XOR operation of the element with 0, ans will become 0 if the same number is found (as a XOR a = 0) and so after the completion of the loop, only element with no duplicate number will remain and will be returned as ans.
*/

// https://leetcode.com/problems/single-number/description/
public class Solution {
    public int SingleNumber(int[] nums) {
        int ans=0; //since XOR with 0 returns same number 
        for(int i=0; i < nums.Length; i++){
            ans ^= nums[i];  // ans = (ans) XOR (array element at i) 
        }
        return ans;
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.SingleNumber

Leetcode.Solved.Number-of-good-pairs

//https://leetcode.com/problems/number-of-good-pairs/
public class Solution {
    public int NumIdenticalPairs(int[] nums) {
        int count = 0;
        for (int i = 0; i < nums.Length; i++) {
            for (int j = 0; j < nums.Length; j++) {
              if (i < j && nums[i] == nums[j]) {
                 count++; 
              }
            }
        }

        return count;
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.Number-of-good-pairs

Leetcode.Solved.Two-sum

//https://leetcode.com/problems/two-sum/
public class Solution {
    public class TwoSum
    {
        public int[] TwoSum(int[] nums, int target) {        
            var dict = new Dictionary<int, int>();
            for (int i = 0; i < nums.Length; i++)
            {
                if (dict.ContainsKey(nums[i]))
                    return new int[] { i, dict[nums[i]] };

                int diff = target - nums[i];

                dict.Add(diff, i);
            }

            return null;
        }
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.Two-sum

Leetcode.Solved.Repeated-substring-pattern

//https://leetcode.com/problems/repeated-substring-pattern/
public class Solution {
    public bool RepeatedSubstringPattern(string s) {

            int minSubstrLength = 1;
            int maxSubstrLength = s.Length / 2;
            StringBuilder sb = new StringBuilder();

            for (int i = minSubstrLength; i <= maxSubstrLength; i++)
            {                
                int substrLength = i;
                string substr = s.Substring(0, substrLength);

                int nTimesRepeat = s.Length / substrLength;             

                sb.Clear();
                for (int j = 0; j < nTimesRepeat; j++)                
                  sb.Append(substr);
                                
                if (s.Equals(sb.ToString()))
                    return true;
            }

            return false;
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.Repeated-substring-pattern

Leetcode.Solved.Concatenation-of-array

// https://leetcode.com/problems/concatenation-of-array/
public class Solution {
    public int[] GetConcatenation(int[] nums) {
        int[] res = new int[2 * nums.Length];
        for (int i = 0 ; i < nums.Length; i++) {
          res[i] = nums[i];
          res[i + nums.Length] = nums[i];
        }        

        return res;
    }
}
Posted in Без рубрики | Comments Off on Leetcode.Solved.Concatenation-of-array