same idea. But I use 2 helpers to find the left and right boundaries.
Vick.Chen
@Vick.Chen
Posts made by Vick.Chen

RE: Clean iterative solution with two binary searches (with explanation)

RE: My O(n) solution using a stack
Similar idea as mine, but better than mine :)
I use char Stack instead of int Stack.
It leads I have to use another temp Stack to store information for valid parentheses. 
RE: Share my O(n) time solution
@GoldenalCheese
Let's discuss the worst case:
#1 take "1" n to find the index for swapping
#2 take another "1" n to to find the next bigger number
#3 take "1" step to swipe
#4 take "1/2" n to reverse sort the tail array.
The total O is n+n+1+n/2 = 2.5n +1 ~= O(n)And here's my own code, same idea, but less lines
public void NextPermutation(int[] nums) { int swapi = 0; for(int i = nums.Length  2; i >= 0 && swapi == 0; i) { if(nums[i] < nums[i + 1]) { for(int k = nums.Length  1; k >= i  1 && swapi == 0; k) { if(nums[k] > nums[i]) { int temp = nums[k]; nums[k] = nums[i]; nums[i] = temp; swapi = i + 1; } } } } //I'm lazy to write the funtion for swipe tail, //so the default sort is n*lgn, but you can write swipe function to reduce it to O(n/2) Array.Sort(nums, swapi, nums.Length  swapi); }

RE: Java BFS Solution
same idea as me, I use Stack instead.
public IList<double> AverageOfLevels(TreeNode root) { IList<double> rs = new List<double>(); Stack<TreeNode> rsk = new Stack<TreeNode>(); rsk.Push(root); return helper(rs, rsk); } public IList<double> helper(IList<double> rs, Stack<TreeNode> rsk) { if(rsk.Count == 0)return rs; Stack<TreeNode> next = new Stack<TreeNode>(); double isum = 0; double icount = 0; while(rsk.Count > 0) { TreeNode cur = rsk.Pop(); isum += cur.val; icount++; if(cur.left != null)next.Push(cur.left); if(cur.right != null)next.Push(cur.right); } rs.Add(isum/icount); return helper(rs, next); }

RE: 7ms java code win over 100%
@rikimberley said in 7ms java code win over 100%:
threeSumForFourSum
nice details about throwing impossibles cases out!

RE: Simple and easyunderstanding O(n^2) JAVA solution
same solution as mine.
But I think the global variable "count" is not necessary. You can put it as local.
Correct me if I'm wrong. 
Very Straightforward Easy solution
public string Tree2str(TreeNode t) { if(t == null) return ""; if(t.left == null && t.right == null) return t.val.ToString(); string s = t.val.ToString() + "(" + Tree2str(t.left) + ")"; s = t.right == null ? s : s + "(" + Tree2str(t.right) + ")"; return s; }

5 lines Elegant Solution
public TreeNode MergeTrees(TreeNode t1, TreeNode t2) { if(t1 == null  t2 == null) return t1 == null ? t2:t1; t1.val += t2.val; t1.left = MergeTrees(t1.left, t2.left); t1.right = MergeTrees(t1.right, t2.right); return t1; }

I think this should be marked as Easy
It's very easy to figure it out as soon as you understand what's Roman Number.
And here's my common solutionpublic string IntToRoman(int num) { string rs=""; int[] vals = {1000, 500, 100, 50, 10, 5, 1}; char[] chars = {'M', 'D', 'C', 'L', 'X', 'V', 'I'}; for(int i = 0; i < vals.Length && num > 0 ; i+=2) { int cur = num / vals[i]; if(cur == 0) { num = num % vals[i]; continue; } if(cur < 4) { while(cur > 0) { rs += chars[i].ToString(); cur; } } else if(cur == 4) { rs += chars[i].ToString() + chars[i  1].ToString(); } else if(cur < 9) { rs += chars[i  1].ToString(); while(cur  5 > 0) { rs += chars[i].ToString(); cur; } } else if(cur == 9) { rs += chars[i].ToString() + chars[i  2].ToString(); } num = num % vals[i]; } return rs; }

RE: Simple and fast C++/C with explanation
Simple and easy to understand !!!