public int numDecodings(String s) {
if(s.length() == 0) return 0;
int pre = 26, digit, answer = 0, first = 1, second = 1;
for(int i = s.length()  1; i >= 0; i){
digit = s.charAt(i)  '0';
answer = digit == 0 ? 0 : first + (digit * 10 + pre <= 26 ? second : 0);
second = first;
first = answer;
pre = digit;
}
return answer;
}
chern_yee
@chern_yee
Posts made by chern_yee

Beat 90% submission (Java)

RE: Share my short JAVA solution
Arrays.sort(strs)
is not necessary since the question didn't ask you to return list of String that is in lexicographical order. If you remove this line, your result will be 99% of submission :) 
Short and simple Java solution (15 lines)
public int maxEnvelopes(int[][] envelopes) { Arrays.sort(envelopes, (a, b) > a[0]  b[0]); int max = 0; int dp [] = new int [envelopes.length]; for(int i = 0; i < envelopes.length; i++){ dp[i] = 1; for(int j = 0; j < i; j++){ if(envelopes[j][0] < envelopes[i][0] && envelopes[j][1] < envelopes[i][1]) dp[i] = Math.max(dp[i], dp[j] + 1); } max = Math.max(dp[i], max); } return max; }

RE: Java code with 13 lines
That is why it is the longest common prefix among other elements.

RE: Simple JAVA solution
http://stackoverflow.com/questions/1532461/stringbuildervsstringconcatenationintostringinjava
In this case, it doesn't really matter as long as you aren't concatenate strings in a loop, otherwise that makes not much different in terms of performance, more like personal style wise.

RE: JAVAEasy Version To Understand!!!!!!!!!!!
You are such a strict person! Or ~result + 1...

RE: My short java solution without using string buffer and string builder
Why people are upvoting this? Using String just to avoid using StringBuffer like everybody else? Do you know why we use StringBuilder not String most of the time? Because every time you concatenate the string using "+", you are creating a String object in the String pool. Don't ever do that man...

RE: Very easy with Java(14 Line) beats 73.95% (13 ms)
You can set the second for(int i : nums) to for(int i : hash) to avoid duplicates.

RE: Java solution refer to Mirris traversal using constant place
brilliant and easy to understand solution!

Java AC Solution
Inspired by @TWiStErRob, basically the idea is find the index of last peak element, then find the element which is greater than the last peak element and then swap them. Once you swap those values, reverse the list after that element. See picture here for better understanding : https://www.nayuki.io/res/nextlexicographicalpermutationalgorithm/nextpermutationalgorithmthumb.png
public void nextPermutation(int[] nums) { int pivot = indexOfLastPeak(nums)  1; if(pivot != 1){ int pivotSuccessor = lastIndexOfGreater(nums, nums[pivot]); int temp = nums[pivot]; nums[pivot] = nums[pivotSuccessor]; nums[pivotSuccessor] = temp; } int start = pivot + 1, end = nums.length  1; while(start < end){ int temp = nums[start]; nums[start++] = nums[end]; nums[end] = temp; } } private int indexOfLastPeak(int [] nums){ for(int i = nums.length  1 ; i > 0; i){ if(nums[i] > nums[i  1]) return i; } return 0; /** first index */ } private int lastIndexOfGreater(int [] nums, int threshold){ for(int i = nums.length  1 ; i >= 0; i){ if(nums[i] > threshold) return i; } return 1; /** shouldn't be executed */ }