Wednesday, August 24, 2016

Leetcode 26. Remove Duplicates from Sorted Array

 public class Solution {  
   public int RemoveDuplicates(int[] nums) {  
     if (nums == null || nums.Length == 0)  
     {  
       return 0;  
     }  
     int fast = 1;  
     int slow = 0;  
     while (fast < nums.Length)  
     {  
       if (nums[fast] == nums[slow])  
       {  
         fast++;  
         continue;  
       }  
       slow++;  
       nums[slow] = nums[fast];  
       fast++;  
     }  
     return slow+1;  
   }  
 }  

Tuesday, August 23, 2016

Leetcode 22. Generate Parentheses

 public class Solution {  
   public IList<string> GenerateParenthesis(int n) {  
     List<string> ret = new List<string>();  
     string current = "";  
     helper(n, 0, 0, current, ret);  
     return ret;  
   }  
   void helper(int n, int leftcount, int rightcount, string current, List<string> ret)  
   {  
     if (leftcount + rightcount == n * 2)  
     {  
       ret.Add(current);  
       return;  
     }  
     if (leftcount < n)  
     {  
       helper(n, leftcount+1, rightcount, current + "(", ret);  
     }  
     if (rightcount < leftcount)
     {  
       helper(n, leftcount, rightcount + 1, current + ")", ret);  
     }  
   }  
 }  

Leetcode 15. 3Sum

 public class Solution {  
   public IList<IList<int>> ThreeSum(int[] nums) {  
     List<IList<int>> ret = new List<IList<int>>();  
     if (nums == null || nums.Length < 3)  
     {  
       return ret;  
     }  
     Array.Sort(nums);  
     int a = 0;  
     HashSet<string> hash = new HashSet<string>();  
     while (a <= nums.Length - 3)  
     {  
       if (a > 0 && nums[a] == nums[a-1])  
       {  
         a++;  
         continue;  
       }  
       int b = a + 1;  
       int c = nums.Length - 1;  
       while (b < c)  
       {  
         int sum = nums[a] + nums[b] + nums[c];  
         string array = String.Join(",", new int[3]{nums[a], nums[b], nums[c]});  
         if (sum == 0 && !hash.Contains(array))  
         {  
           List<int> res = new List<int>();  
           res.Add(nums[a]);  
           res.Add(nums[b]);  
           res.Add(nums[c]);  
           hash.Add(array);  
           ret.Add(res);  
           b++;  
           c--;  
         }  
         else if (sum < 0)  
         {  
           b++;  
         }  
         else  
         {  
           c--;  
         }  
       }  
       a++;  
     }  
     return ret;  
   }  
 }  

Leetcode 11. Container With Most Water

 public class Solution {  
   public int MaxArea(int[] height) {  
     if (height == null)  
     {  
       return 0;  
     }  
     int start = 0;  
     int end = height.Length - 1;  
     int max = 0;  
     while (start < end)  
     {  
       int current = (end - start) * Math.Min(height[start], height[end]);  
       max = Math.Max(max, current);  
       if (height[start] < height[end] && end > start + 1)  
       {  
         start++;  
         continue;  
       }  
       if (height[start] > height[end] && end - 1 > start)  
       {  
         end--;  
         continue;  
       }  
       start++;  
       end--;  
     }  
     return max;  
   }  
 }  

Leetcode 199. Binary Tree Right Side View

 /**  
  * Definition for a binary tree node.  
  * public class TreeNode {  
  *   public int val;  
  *   public TreeNode left;  
  *   public TreeNode right;  
  *   public TreeNode(int x) { val = x; }  
  * }  
  */  
 public class Solution {  
   public IList<int> RightSideView(TreeNode root) {  
     List<int> ret = new List<int>();  
     if (root == null)  
     {  
       return ret;  
     }  
     List<TreeNode> list = new List<TreeNode>();  
     list.Add(root);  
     while (list.Count > 0)  
     {  
       // Get children of all items from the list, and keep record of the last one  
       List<TreeNode> newlist = new List<TreeNode>();  
       foreach(TreeNode item in list)  
       {  
         if (item.left != null)  
         {  
           newlist.Add(item.left);  
         }  
         if (item.right != null)  
         {  
           newlist.Add(item.right);  
         }  
       }  
       TreeNode last = list[list.Count - 1];  
       ret.Add(last.val);  
       list = newlist;  
     }  
     return ret;  
   }  
 }  

Monday, August 22, 2016

Leetcode 62. Unique Paths

 public class Solution {  
   public int UniquePaths(int m, int n) {  
     if (m < 1 || n < 1)  
     {  
       return 0;  
     }  
     int[] dp = new int[n];  
     for (int i = 0; i < n; i++)  
     {  
       dp[i] = 1;  
     }  
     for (int i = 1; i < m; i++)  
     {  
       for (int j = 1; j < n; j++)  
       {  
         dp[j] += dp[j-1];  
       }  
     }  
     return dp[n-1];  
   }  
 }  

Leetcode 20. Valid Parentheses

 public class Solution {  
   public bool IsValid(string s) {  
     if (s == null)  
     {  
       return false;  
     }  
     char[] a = s.ToCharArray();  
     List<char> stack = new List<char>();  
     foreach(char c in a)  
     {  
       if (c =='(' || c=='[' || c=='{')  
       {  
         stack.Add(c);  
       }  
       else  
       {  
         if (c==')')  
         {  
           if (stack.Count ==0 || stack[stack.Count - 1] != '(')  
           {  
             return false;  
           }  
           stack.RemoveAt(stack.Count - 1);  
         }  
         if (c==']')  
         {  
           if (stack.Count ==0 || stack[stack.Count - 1] != '[')  
           {  
             return false;  
           }  
           stack.RemoveAt(stack.Count - 1);  
         }  
         if (c=='}')  
         {  
           if (stack.Count ==0 || stack[stack.Count - 1] != '{')  
           {  
             return false;  
           }  
           stack.RemoveAt(stack.Count - 1);  
         }  
       }  
     }  
     if (stack.Count != 0)  
     {  
       return false;  
     }  
     return true;  
   }  
 }