id
int64 0
1.17k
| comefrom
stringlengths 21
84
| code
stringlengths 50
1.89k
| masked
stringlengths 63
2.47k
|
---|---|---|---|
100 | <qiyuangong>005_Longest_Palindromic_Substring_1.java | private int expandAroundCenter(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
return R - L - 1;
} | private int sample_funcname ( String var_1 , int var_2 , int var_3 ) {
int var_4 = var_2 , var_5 = var_3 ;
while ( var_4 >= 0 && var_5 < var_1 . length ( ) && var_1 . charAt ( var_4 ) == var_1 . charAt ( var_5 ) ) {
var_4 -- ;
var_5 ++ ;
}
return var_5 - var_4 - 1 ;
}
|
101 | <qiyuangong>347_Top_K_Frequent_Elements_1.java | public List<Integer> topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> count = new HashMap();
for (int n: nums) {
count.put(n, count.getOrDefault(n, 0) + 1);
}
PriorityQueue<Integer> heap =
new PriorityQueue<Integer>((n1, n2) -> count.get(n1) - count.get(n2));
for (int n: count.keySet()) {
heap.add(n);
if (heap.size() > k)
heap.poll();
}
List<Integer> top_k = new LinkedList();
while (!heap.isEmpty())
top_k.add(heap.poll());
Collections.reverse(top_k);
return top_k;
} | public List < Integer > var_1 ( int [ ] var_2 , int var_3 ) {
HashMap < Integer , Integer > var_5 = new HashMap ( ) ;
for ( int var_6 : var_2 ) {
var_5 . put ( var_6 , var_5 . getOrDefault ( var_6 , 0 ) + 1 ) ;
}
PriorityQueue < Integer > var_15 = new PriorityQueue < Integer > ( ( var_13 , var_14 ) -> var_5 . get ( var_13 ) - var_5 . get ( var_14 ) ) ;
for ( int var_6 : var_5 . keySet ( ) ) {
var_15 . add ( var_6 ) ;
if ( var_15 . size ( ) > var_3 ) var_15 . poll ( ) ;
}
List < Integer > var_22 = new LinkedList ( ) ;
while ( ! var_15 . isEmpty ( ) ) var_22 . add ( var_15 . poll ( ) ) ;
Collections . reverse ( var_22 ) ;
return var_22 ;
}
|
102 | <qiyuangong>1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number_1.java | public int[] smallerNumbersThanCurrent(int[] nums) {
int[] countList = new int[101];
int[] res = new int[nums.length];
for (int i = 0; i < nums.length; i++)
countList[nums[i]]++;
for (int i = 1; i < 101; i++)
countList[i] += countList[i-1];
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) res[i] = 0;
else res[i] = countList[nums[i]-1];
}
return res;
} | public int [ ] sample_funcname ( int [ ] var_0 ) {
int [ ] var_2 = new int [ 101 ] ;
int [ ] var_4 = new int [ var_0 . length ] ;
for ( int var_6 = 0 ;
var_6 < var_0 . length ;
var_6 ++ ) var_2 [ var_0 [ var_6 ] ] ++ ;
for ( int var_6 = 1 ;
var_6 < 101 ;
var_6 ++ ) var_2 [ var_6 ] += var_2 [ var_6 - 1 ] ;
for ( int var_6 = 0 ;
var_6 < var_0 . length ;
var_6 ++ ) {
if ( var_0 [ var_6 ] == 0 ) var_4 [ var_6 ] = 0 ;
else var_4 [ var_6 ] = var_2 [ var_0 [ var_6 ] - 1 ] ;
}
return var_4 ;
}
|
103 | <qiyuangong>438_Find_All_Anagrams_in_a_String_1.java | public List<Integer> findAnagrams(String s, String p) {
List<Integer> list = new ArrayList<>();
if (s == null || s.length() == 0 || p == null || p.length() == 0) return list;
int[] hash = new int[256]; //character hash
for (char c : p.toCharArray()) {
hash[c]++;
}
int left = 0, right = 0, count = p.length();
while (right < s.length()) {
if (hash[s.charAt(right++)]-- >= 1) count--;
if (count == 0) list.add(left);
if (right - left == p.length() && hash[s.charAt(left++)]++ >= 0) count++;
}
return list;
} | public List < Integer > var_1 ( String var_3 , String var_4 ) {
List < Integer > var_7 = new ArrayList < > ( ) ;
if ( var_3 == null || var_3 . length ( ) == 0 || var_4 == null || var_4 . length ( ) == 0 ) return var_7 ;
int [ ] var_12 = new int [ 256 ] ;
for ( char var_15 : var_4 . toCharArray ( ) ) {
var_12 [ var_15 ] ++ ;
}
int var_16 = 0 , var_17 = 0 , var_18 = var_4 . length ( ) ;
while ( var_17 < var_3 . length ( ) ) {
if ( var_12 [ var_3 . charAt ( var_17 ++ ) ] -- >= 1 ) var_18 -- ;
if ( var_18 == 0 ) var_7 . add ( var_16 ) ;
if ( var_17 - var_16 == var_4 . length ( ) && var_12 [ var_3 . charAt ( var_16 ++ ) ] ++ >= 0 ) var_18 ++ ;
}
return var_7 ;
}
|
104 | <qiyuangong>733_Flood_Fill_1.java | private void dfs(int[][] image, int r, int c, int color, int newColor) {
if (image[r][c] == color) {
image[r][c] = newColor;
if (r - 1 >= 0) dfs(image, r - 1, c, color, newColor);
if (r + 1 < image.length) dfs(image, r + 1, c, color, newColor);
if (c - 1 >= 0) dfs(image, r, c - 1, color, newColor);
if (c + 1 < image[0].length) dfs(image, r, c + 1, color, newColor);
}
} | private void sample_funcname ( int [ ] [ ] image , int r , int c , int color , int newColor ) {
if ( image [ r ] [ c ] == color ) {
image [ r ] [ c ] = newColor ;
if ( r - 1 >= 0 ) dfs ( image , r - 1 , c , color , newColor ) ;
if ( r + 1 < image . length ) dfs ( image , r + 1 , c , color , newColor ) ;
if ( c - 1 >= 0 ) dfs ( image , r , c - 1 , color , newColor ) ;
if ( c + 1 < image [ 0 ] . length ) dfs ( image , r , c + 1 , color , newColor ) ;
}
}
|
105 | <qiyuangong>1189_Maximum_Number_of_Balloons_1.java | public int maxNumberOfBalloons(String text) {
HashMap<Character, Integer> map = new HashMap<>();
for (char ch : text.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
int res = Integer.MAX_VALUE;
res = Math.min(res, map.getOrDefault('b', 0));
res = Math.min(res, map.getOrDefault('a', 0));
res = Math.min(res, map.getOrDefault('n', 0));
res = Math.min(res, map.getOrDefault('l', 0) / 2);
res = Math.min(res, map.getOrDefault('o', 0) / 2);
return res;
} | public int sample_funcname ( String var_1 ) {
HashMap < Character , Integer > var_5 = new HashMap < > ( ) ;
for ( char var_7 : var_1 . toCharArray ( ) ) {
var_5 . put ( var_7 , var_5 . getOrDefault ( var_7 , 0 ) + 1 ) ;
}
int var_13 = Integer . MAX_VALUE ;
var_13 = Math . min ( var_13 , var_5 . getOrDefault ( 'b' , 0 ) ) ;
var_13 = Math . min ( var_13 , var_5 . getOrDefault ( 'a' , 0 ) ) ;
var_13 = Math . min ( var_13 , var_5 . getOrDefault ( 'n' , 0 ) ) ;
var_13 = Math . min ( var_13 , var_5 . getOrDefault ( 'l' , 0 ) / 2 ) ;
var_13 = Math . min ( var_13 , var_5 . getOrDefault ( 'o' , 0 ) / 2 ) ;
return var_13 ;
}
|
106 | <qiyuangong>547_Friend_Circles_2.java | public int findCircleNum(int[][] M) {
int[] visited = new int[M.length];
int count = 0;
Queue < Integer > queue = new LinkedList < > ();
for (int i = 0; i < M.length; i++) {
if (visited[i] == 0) {
queue.add(i);
while (!queue.isEmpty()) {
int s = queue.remove();
visited[s] = 1;
for (int j = 0; j < M.length; j++) {
if (M[s][j] == 1 && visited[j] == 0)
queue.add(j);
}
}
count++;
}
}
return count;
} | public int sample_funcname ( int [ ] [ ] var_0 ) {
int [ ] var_2 = new int [ var_0 . length ] ;
int var_4 = 0 ;
Queue < Integer > var_8 = new LinkedList < > ( ) ;
for ( int var_9 = 0 ;
var_9 < var_0 . length ;
var_9 ++ ) {
if ( var_2 [ var_9 ] == 0 ) {
var_8 . add ( var_9 ) ;
while ( ! var_8 . isEmpty ( ) ) {
int var_13 = var_8 . remove ( ) ;
var_2 [ var_13 ] = 1 ;
for ( int var_15 = 0 ;
var_15 < var_0 . length ;
var_15 ++ ) {
if ( var_0 [ var_13 ] [ var_15 ] == 1 && var_2 [ var_15 ] == 0 ) var_8 . add ( var_15 ) ;
}
}
var_4 ++ ;
}
}
return var_4 ;
}
|
107 | <qiyuangong>012_Integer_to_Roman_1.java | public String intToRoman(int num) {
Map<Integer, String> map = new HashMap();
map.put(1, "I"); map.put(5, "V"); map.put(10, "X");
map.put(50, "L"); map.put(100, "C"); map.put(500, "D"); map.put(1000, "M");
map.put(4, "IV"); map.put(9, "IX"); map.put(40, "XL"); map.put(90, "XC");
map.put(400, "CD"); map.put(900, "CM");
int[] sequence = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
StringBuffer sb = new StringBuffer();
for (int i = 0; i<sequence.length; i++) {
int base = sequence[i];
while (num >= base) {
sb.append(map.get(base));
num -= base;
}
}
return sb.toString();
} | public String var_0 ( int var_1 ) {
Map < Integer , String > var_6 = new HashMap ( ) ;
var_6 . put ( 1 , "I" ) ;
var_6 . put ( 5 , "V" ) ;
var_6 . put ( 10 , "X" ) ;
var_6 . put ( 50 , "L" ) ;
var_6 . put ( 100 , "C" ) ;
var_6 . put ( 500 , "D" ) ;
var_6 . put ( 1000 , "M" ) ;
var_6 . put ( 4 , "IV" ) ;
var_6 . put ( 9 , "IX" ) ;
var_6 . put ( 40 , "XL" ) ;
var_6 . put ( 90 , "XC" ) ;
var_6 . put ( 400 , "CD" ) ;
var_6 . put ( 900 , "CM" ) ;
int [ ] var_34 = {
1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1 };
StringBuffer var_36 = new StringBuffer ( ) ;
for ( int var_38 = 0 ;
var_38 < var_34 . length ;
var_38 ++ ) {
int var_40 = var_34 [ var_38 ] ;
while ( var_1 >= var_40 ) {
var_36 . append ( var_6 . get ( var_40 ) ) ;
var_1 -= var_40 ;
}
}
return var_36 . toString ( ) ;
}
|
108 | <qiyuangong>766_Toeplitz_Matrix_1.java | public boolean isToeplitzMatrix(int[][] matrix) {
for (int r = 1; r < matrix.length; ++r)
for (int c = 1; c < matrix[0].length; ++c)
if (matrix[r-1][c-1] != matrix[r][c])
return false;
return true;
} | public boolean var_0 ( int [ ] [ ] var_1 ) {
for ( int var_3 = 1 ;
var_3 < var_1 . length ;
++ var_3 ) for ( int var_5 = 1 ;
var_5 < var_1 [ 0 ] . length ;
++ var_5 ) if ( var_1 [ var_3 - 1 ] [ var_5 - 1 ] != var_1 [ var_3 ] [ var_5 ] ) return false ;
return true ;
}
|
109 | <qiyuangong>003_Longest_Substring_Without_Repeating_Characters_1.java | public int lengthOfLongestSubstring(String s) {
boolean[] exist = new boolean[256];
int i = 0, maxLen = 0;
for (int j = 0; j < s.length(); j++) {
while (exist[s.charAt(j)]){
exist[s.charAt(i)] = false;
i++;
}
exist[s.charAt(j)] = true;
maxLen = Math.max(j - i + 1, maxLen);
}
return maxLen;
} | public int sample_funcname ( String var_1 ) {
boolean [ ] var_4 = new boolean [ 256 ] ;
int var_6 = 0 , var_7 = 0 ;
for ( int var_8 = 0 ;
var_8 < var_1 . length ( ) ;
var_8 ++ ) {
while ( var_4 [ var_1 . charAt ( var_8 ) ] ) {
var_4 [ var_1 . charAt ( var_6 ) ] = false ;
var_6 ++ ;
}
var_4 [ var_1 . charAt ( var_8 ) ] = true ;
var_7 = Math . max ( var_8 - var_6 + 1 , var_7 ) ;
}
return var_7 ;
}
|
110 | <qiyuangong>458_Poor_Pigs_1.java | public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
int n = minutesToTest / minutesToDie + 1;
int pigs = 0;
while (Math.pow(n, pigs) < buckets) pigs++;
return pigs;
} | public int sample_funcname ( int var_0 , int var_1 , int var_2 ) {
int var_4 = var_2 / var_1 + 1 ;
int var_6 = 0 ;
while ( Math . pow ( var_4 , var_6 ) < var_0 ) var_6 ++ ;
return var_6 ;
}
|
111 | <qiyuangong>001_Two_Sum_1.java | public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
if (map.containsKey(target - x)) {
return new int[]{map.get(target - x), i};
}
map.put(x, i);
}
throw new IllegalArgumentException("No two sum solution");
} | public int [ ] sample_funcname ( int [ ] var_0 , int var_1 ) {
Map < Integer , Integer > var_5 = new HashMap < > ( ) ;
for ( int var_7 = 0 ;
var_7 < var_0 . length ;
var_7 ++ ) {
int var_9 = var_0 [ var_7 ] ;
if ( var_5 . containsKey ( var_1 - var_9 ) ) {
return new int [ ] {
var_5 . get ( var_1 - var_9 ) , var_7 };
}
var_5 . put ( var_9 , var_7 ) ;
}
throw new IllegalArgumentException ( "No two sum solution" ) ;
}
|
112 | <qiyuangong>004_Median_of_Two_Sorted_Arrays_1.java | public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int p1 = 0, p2 = 0, pos = 0;
int ls1 = nums1.length, ls2 = nums2.length;
int[] all_nums = new int[ls1+ls2];
double median = 0.0;
while (p1 < ls1 && p2 < ls2){
if (nums1[p1] <= nums2[p2])
all_nums[pos++] = nums1[p1++];
else
all_nums[pos++] = nums2[p2++];
}
while (p1 < ls1)
all_nums[pos++] = nums1[p1++];
while (p2 < ls2)
all_nums[pos++] = nums2[p2++];
if ((ls1 + ls2) % 2 == 1)
median = all_nums[(ls1 + ls2) / 2];
else
median = (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2.0;
return median;
} | public double sample_funcname ( int [ ] var_0 , int [ ] var_1 ) {
int var_3 = 0 , var_4 = 0 , var_5 = 0 ;
int var_7 = var_0 . length , var_8 = var_1 . length ;
int [ ] var_9 = new int [ var_7 + var_8 ] ;
double var_11 = 0.0 ;
while ( var_3 < var_7 && var_4 < var_8 ) {
if ( var_0 [ var_3 ] <= var_1 [ var_4 ] ) var_9 [ var_5 ++ ] = var_0 [ var_3 ++ ] ;
else var_9 [ var_5 ++ ] = var_1 [ var_4 ++ ] ;
}
while ( var_3 < var_7 ) var_9 [ var_5 ++ ] = var_0 [ var_3 ++ ] ;
while ( var_4 < var_8 ) var_9 [ var_5 ++ ] = var_1 [ var_4 ++ ] ;
if ( ( var_7 + var_8 ) % 2 == 1 ) var_11 = var_9 [ ( var_7 + var_8 ) / 2 ] ;
else var_11 = ( var_9 [ ( var_7 + var_8 ) / 2 ] + var_9 [ ( var_7 + var_8 ) / 2 - 1 ] ) / 2.0 ;
return var_11 ;
}
|
113 | <qiyuangong>066_Plus_One_1.java | private int[] addToDigit(int[] digits, int index) {
if (index == -1) {
int[] newDigits = new int[digits.length + 1];
newDigits[0] = 1;
for (int i = 0; i < digits.length; i++) {
newDigits[i + 1] = digits[i];
}
return newDigits;
}
if (digits[index] == 9) {
digits[index] = 0;
return addToDigit(digits, index - 1);
} else {
digits[index]++;
return digits;
}
} | private int [ ] sample_funcname ( int [ ] digits , int index ) {
if ( index == - 1 ) {
int [ ] newDigits = new int [ digits . length + 1 ] ;
newDigits [ 0 ] = 1 ;
for ( int i = 0 ;
i < digits . length ;
i ++ ) {
newDigits [ i + 1 ] = digits [ i ] ;
}
return newDigits ;
}
if ( digits [ index ] == 9 ) {
digits [ index ] = 0 ;
return addToDigit ( digits , index - 1 ) ;
}
else {
digits [ index ] ++ ;
return digits ;
}
}
|
114 | <qiyuangong>434_Number_of_Segments_in_a_String_1.java | public int countSegments(String s) {
String trimmed = s.trim();
if (trimmed.equals("")) {
return 0;
}
return trimmed.split("\\s+").length;
} | public int sample_funcname ( String var_1 ) {
String var_3 = var_1 . trim ( ) ;
if ( var_3 . equals ( "" ) ) {
return 0 ;
}
return var_3 . split ( "\\s+" ) . length ;
}
|
115 | <qiyuangong>560_Subarray_Sum_Equals_K_1.java | public int subarraySum(int[] nums, int k) {
int count = 0;
for (int start = 0; start < nums.length; start++) {
int sum = 0;
for (int end = start; end < nums.length; end++) {
sum += nums[end];
if (sum == k)
count++;
}
}
return count;
} | public int sample_funcname ( int [ ] var_0 , int var_1 ) {
int var_3 = 0 ;
for ( int var_4 = 0 ;
var_4 < var_0 . length ;
var_4 ++ ) {
int var_6 = 0 ;
for ( int var_7 = var_4 ;
var_7 < var_0 . length ;
var_7 ++ ) {
var_6 += var_0 [ var_7 ] ;
if ( var_6 == var_1 ) var_3 ++ ;
}
}
return var_3 ;
}
|
116 | <qiyuangong>784_Letter_Case_Permutation_1.java | public List<String> letterCasePermutation(String S) {
List<StringBuilder> ans = new ArrayList();
ans.add(new StringBuilder());
for (char c: S.toCharArray()) {
int n = ans.size();
if (Character.isLetter(c)) {
for (int i = 0; i < n; ++i) {
ans.add(new StringBuilder(ans.get(i)));
ans.get(i).append(Character.toLowerCase(c));
ans.get(n + i).append(Character.toUpperCase(c));
}
} else {
for (int i = 0; i < n; ++i)
ans.get(i).append(c);
}
}
List<String> finalans = new ArrayList();
for (StringBuilder sb: ans)
finalans.add(sb.toString());
return finalans;
} | public List < String > var_1 ( String var_2 ) {
List < StringBuilder > var_6 = new ArrayList ( ) ;
var_6 . add ( new StringBuilder ( ) ) ;
for ( char var_9 : var_2 . toCharArray ( ) ) {
int var_11 = var_6 . size ( ) ;
if ( Character . isLetter ( var_9 ) ) {
for ( int var_15 = 0 ;
var_15 < var_11 ;
++ var_15 ) {
var_6 . add ( new StringBuilder ( var_6 . get ( var_15 ) ) ) ;
var_6 . get ( var_15 ) . append ( Character . toLowerCase ( var_9 ) ) ;
var_6 . get ( var_11 + var_15 ) . append ( Character . toUpperCase ( var_9 ) ) ;
}
}
else {
for ( int var_15 = 0 ;
var_15 < var_11 ;
++ var_15 ) var_6 . get ( var_15 ) . append ( var_9 ) ;
}
}
List < String > var_20 = new ArrayList ( ) ;
for ( StringBuilder var_21 : var_6 ) var_20 . add ( var_21 . toString ( ) ) ;
return var_20 ;
}
|
117 | <qiyuangong>414_Third_Maximum_Number_1.java | public int thirdMax(int[] nums) {
PriorityQueue<Integer> pq = new PriorityQueue<>(3);
Set<Integer> set = new HashSet<>();
for (int i : nums) {
if (set.contains(i)) continue;
pq.offer(i);
set.add(i);
if (pq.size() > 3) set.remove(pq.poll());
}
while (pq.size() < 3 && pq.size() > 1) {
pq.poll();
}
return pq.peek();
} | public int sample_funcname ( int [ ] var_0 ) {
PriorityQueue < Integer > var_4 = new PriorityQueue < > ( 3 ) ;
Set < Integer > var_7 = new HashSet < > ( ) ;
for ( int var_8 : var_0 ) {
if ( var_7 . contains ( var_8 ) ) continue ;
var_4 . offer ( var_8 ) ;
var_7 . add ( var_8 ) ;
if ( var_4 . size ( ) > 3 ) var_7 . remove ( var_4 . poll ( ) ) ;
}
while ( var_4 . size ( ) < 3 && var_4 . size ( ) > 1 ) {
var_4 . poll ( ) ;
}
return var_4 . peek ( ) ;
}
|
118 | <qiyuangong>448_Find_All_Numbers_Disappeared_in_an_Array_1.java | public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> ret = new ArrayList<Integer>();
for(int i = 0; i < nums.length; i++) {
int val = Math.abs(nums[i]) - 1;
if(nums[val] > 0) {
nums[val] = -nums[val];
}
}
for(int i = 0; i < nums.length; i++) {
if(nums[i] > 0) {
ret.add(i+1);
}
}
return ret;
} | public List < Integer > var_1 ( int [ ] var_2 ) {
List < Integer > var_5 = new ArrayList < Integer > ( ) ;
for ( int var_7 = 0 ;
var_7 < var_2 . length ;
var_7 ++ ) {
int var_12 = Math . abs ( var_2 [ var_7 ] ) - 1 ;
if ( var_2 [ var_12 ] > 0 ) {
var_2 [ var_12 ] = - var_2 [ var_12 ] ;
}
}
for ( int var_7 = 0 ;
var_7 < var_2 . length ;
var_7 ++ ) {
if ( var_2 [ var_7 ] > 0 ) {
var_5 . add ( var_7 + 1 ) ;
}
}
return var_5 ;
}
|
119 | <qiyuangong>461_Hamming_Distance_1.java | public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
} | public int sample_funcname ( int var_0 , int var_1 ) {
return Integer . bitCount ( var_0 ^ var_1 ) ;
}
|
120 | <qiyuangong>977_Squares_of_a_Sorted_Array_1.java | public int[] sortedSquares(int[] A) {
int pos = 0;
int[] res = new int[A.length];
int curr = 0;
while (pos < A.length && A[pos] < 0) pos++;
int npos = pos - 1;
while (pos < A.length && npos >= 0) {
if (A[pos] * A[pos] < A[npos] * A[npos]) {
res[curr++] = A[pos] * A[pos];
pos++;
} else {
res[curr++] = A[npos] * A[npos];
npos--;
}
}
while (npos >= 0) {
res[curr++] = A[npos] * A[npos];
npos--;
}
while (pos < A.length) {
res[curr++] = A[pos] * A[pos];
pos++;
}
return res;
} | public int [ ] sample_funcname ( int [ ] var_0 ) {
int var_2 = 0 ;
int [ ] var_4 = new int [ var_0 . length ] ;
int var_5 = 0 ;
while ( var_2 < var_0 . length && var_0 [ var_2 ] < 0 ) var_2 ++ ;
int var_7 = var_2 - 1 ;
while ( var_2 < var_0 . length && var_7 >= 0 ) {
if ( var_0 [ var_2 ] * var_0 [ var_2 ] < var_0 [ var_7 ] * var_0 [ var_7 ] ) {
var_4 [ var_5 ++ ] = var_0 [ var_2 ] * var_0 [ var_2 ] ;
var_2 ++ ;
}
else {
var_4 [ var_5 ++ ] = var_0 [ var_7 ] * var_0 [ var_7 ] ;
var_7 -- ;
}
}
while ( var_7 >= 0 ) {
var_4 [ var_5 ++ ] = var_0 [ var_7 ] * var_0 [ var_7 ] ;
var_7 -- ;
}
while ( var_2 < var_0 . length ) {
var_4 [ var_5 ++ ] = var_0 [ var_2 ] * var_0 [ var_2 ] ;
var_2 ++ ;
}
return var_4 ;
}
|
121 | <qiyuangong>692_Top_K_Frequent_Words_1.java | public List<String> topKFrequent(String[] words, int k) {
Map<String, Integer> count = new HashMap();
for (String word: words) {
count.put(word, count.getOrDefault(word, 0) + 1);
}
List<String> candidates = new ArrayList(count.keySet());
Collections.sort(candidates, (w1, w2) -> count.get(w1).equals(count.get(w2)) ?
w1.compareTo(w2) : count.get(w2) - count.get(w1));
return candidates.subList(0, k);
} | public List < String > var_1 ( String [ ] var_2 , int var_3 ) {
Map < String , Integer > var_7 = new HashMap ( ) ;
for ( String var_8 : var_2 ) {
var_7 . put ( var_8 , var_7 . getOrDefault ( var_8 , 0 ) + 1 ) ;
}
List < String > var_16 = new ArrayList ( var_7 . keySet ( ) ) ;
Collections . sort ( var_16 , ( var_19 , var_20 ) -> var_7 . get ( var_19 ) . equals ( var_7 . get ( var_20 ) ) ? var_19 . compareTo ( var_20 ) : var_7 . get ( var_20 ) - var_7 . get ( var_19 ) ) ;
return var_16 . subList ( 0 , var_3 ) ;
}
|
122 | <qiyuangong>009_Palindrome_Number_3.java | public boolean isPalindrome(int x) {
int r,s=0,number=x;
if(number<0){
return false;
}
while (number!=0){
r=number%10;
s= s*10 +r;
number/=10;
}
if (s==x){
return true;
}
else {
return false;
}
} | public boolean var_0 ( int var_1 ) {
int var_2 , var_4 = 0 , var_5 = var_1 ;
if ( var_5 < 0 ) {
return false ;
}
while ( var_5 != 0 ) {
var_2 = var_5 % 10 ;
var_4 = var_4 * 10 + var_2 ;
var_5 /= 10 ;
}
if ( var_4 == var_1 ) {
return true ;
}
else {
return false ;
}
}
|
123 | <awangdev>Total Hamming Distance_1.java | private void populateBinaryCount(int[] countZero, int[] countOne, int num) {
for (int i = 0; i < 32; i++){
if ((num & 1) == 1) countOne[i]++;
else countZero[i]++;
num = (num >> 1);
}
} | private void var_0 ( int [ ] var_1 , int [ ] var_2 , int var_3 ) {
for ( int var_5 = 0 ;
var_5 < 32 ;
var_5 ++ ) {
if ( ( var_3 & 1 ) == 1 ) var_2 [ var_5 ] ++ ;
else var_1 [ var_5 ] ++ ;
var_3 = ( var_3 >> 1 ) ;
}
}
|
124 | <awangdev>Wood Cut_1.java | private boolean validate(int size, int k, int[] L) {
int count = 0;
for (int length : L) {
count += length / size;
}
return count >= k;
} | private boolean var_0 ( int var_1 , int var_2 , int [ ] var_3 ) {
int var_5 = 0 ;
for ( int var_6 : var_3 ) {
var_5 += var_6 / var_1 ;
}
return var_5 >= var_2 ;
}
|
125 | <awangdev>Single Number III_1.java | public List<Integer> singleNumberIII(int[] A) {
if (A == null || A.length == 0) {
return null;
}
List<Integer> rst = new ArrayList<Integer>();
int xor = 0;
for (int i = 0; i < A.length; i++) {
xor ^= A[i];
}
int bitOnePos = 0;
for (int i = 0; i < 32; i++) {
if ((xor >> i & 1) == 1) {
bitOnePos = i;
}
}
int rstA = 0;
int rstB = 0;
for (int i = 0; i < A.length; i++) {
if ((A[i] >> bitOnePos & 1) == 1) {
rstA ^= A[i];
} else {
rstB ^= A[i];
}
}
rst.add(rstA);
rst.add(rstB);
return rst;
} | public List < Integer > var_1 ( int [ ] var_2 ) {
if ( var_2 == null || var_2 . length == 0 ) {
return null ;
}
List < Integer > var_8 = new ArrayList < Integer > ( ) ;
int var_9 = 0 ;
for ( int var_10 = 0 ;
var_10 < var_2 . length ;
var_10 ++ ) {
var_9 ^= var_2 [ var_10 ] ;
}
int var_11 = 0 ;
for ( int var_10 = 0 ;
var_10 < 32 ;
var_10 ++ ) {
if ( ( var_9 >> var_10 & 1 ) == 1 ) {
var_11 = var_10 ;
}
}
int var_14 = 0 ;
int var_15 = 0 ;
for ( int var_10 = 0 ;
var_10 < var_2 . length ;
var_10 ++ ) {
if ( ( var_2 [ var_10 ] >> var_11 & 1 ) == 1 ) {
var_14 ^= var_2 [ var_10 ] ;
}
else {
var_15 ^= var_2 [ var_10 ] ;
}
}
var_8 . add ( var_14 ) ;
var_8 . add ( var_15 ) ;
return var_8 ;
}
|
126 | <awangdev>62. Unique Path_1.java | public int uniquePaths(int m, int n) {
if (m == 0 || n == 0) return 0;
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int i = 0; i < n; i++) dp[0][i] = 1;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m-1][n-1];
} | public int sample_funcname ( int var_0 , int var_1 ) {
if ( var_0 == 0 || var_1 == 0 ) return 0 ;
int [ ] [ ] var_3 = new int [ var_0 ] [ var_1 ] ;
for ( int var_4 = 0 ;
var_4 < var_0 ;
var_4 ++ ) var_3 [ var_4 ] [ 0 ] = 1 ;
for ( int var_4 = 0 ;
var_4 < var_1 ;
var_4 ++ ) var_3 [ 0 ] [ var_4 ] = 1 ;
for ( int var_4 = 1 ;
var_4 < var_0 ;
var_4 ++ ) {
for ( int var_6 = 1 ;
var_6 < var_1 ;
var_6 ++ ) {
var_3 [ var_4 ] [ var_6 ] = var_3 [ var_4 - 1 ] [ var_6 ] + var_3 [ var_4 ] [ var_6 - 1 ] ;
}
}
return var_3 [ var_0 - 1 ] [ var_1 - 1 ] ;
}
|
127 | <awangdev>Max Sum of Rectangle No Larger Than K_1.java | public int maxSumSubmatrix(int[][] matrix, int k) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return -1;
}
int m = Math.min(matrix.length, matrix[0].length);
int n = Math.max(matrix.length, matrix[0].length);
boolean rowFirst = matrix.length < matrix[0].length;
int max = Integer.MIN_VALUE;
for (int row = 0; row < m; row++) {
int[] preSum = new int[n];
for (int i = row; i < m; i++) {
TreeSet<Integer> areaSet = new TreeSet<>();
int newSum = 0;
for (int j = 0; j < n; j++) {
preSum[j] += rowFirst? matrix[i][j] : matrix[j][i];
newSum += preSum[j];
if (newSum <= k) max = Math.max(max, newSum);
Integer extraArea = areaSet.ceiling(newSum - k);
if (extraArea != null) {
max = Math.max(max, newSum - extraArea);
}
areaSet.add(newSum);
}
}
}
return max == Integer.MIN_VALUE ? -1 : max;
} | public int sample_funcname ( int [ ] [ ] var_0 , int var_1 ) {
if ( var_0 == null || var_0 . length == 0 || var_0 [ 0 ] == null || var_0 [ 0 ] . length == 0 ) {
return - 1 ;
}
int var_8 = Math . min ( var_0 . length , var_0 [ 0 ] . length ) ;
int var_10 = Math . max ( var_0 . length , var_0 [ 0 ] . length ) ;
boolean var_12 = var_0 . length < var_0 [ 0 ] . length ;
int max = Integer . MIN_VALUE ;
for ( int var_15 = 0 ;
var_15 < var_8 ;
var_15 ++ ) {
int [ ] var_16 = new int [ var_10 ] ;
for ( int var_17 = var_15 ;
var_17 < var_8 ;
var_17 ++ ) {
TreeSet < Integer > var_19 = new TreeSet < > ( ) ;
int var_20 = 0 ;
for ( int var_21 = 0 ;
var_21 < var_10 ;
var_21 ++ ) {
var_16 [ var_21 ] += var_12 ? var_0 [ var_17 ] [ var_21 ] : var_0 [ var_21 ] [ var_17 ] ;
var_20 += var_16 [ var_21 ] ;
if ( var_20 <= var_1 ) max = Math . max ( max , var_20 ) ;
Integer var_23 = var_19 . ceiling ( var_20 - var_1 ) ;
if ( var_23 != null ) {
max = Math . max ( max , var_20 - var_23 ) ;
}
var_19 . add ( var_20 ) ;
}
}
}
return max == Integer . MIN_VALUE ? - 1 : max ;
}
|
128 | <awangdev>64. Minimum Path Sum_4.java | public int minPathSum(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0) return 0;
int m = grid.length, n = grid[0].length;
int[][] dp = new int[2][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
dp[i%2][j] = grid[i][j];
if (i == 0 && j == 0) continue;
if (i == 0 && j > 0) dp[i%2][j] += dp[i%2][j - 1];
else if (i > 0 && j == 0) dp[i%2][j] += dp[(i - 1)%2][j];
else dp[i%2][j] += Math.min(dp[(i - 1)%2][j], dp[i%2][j - 1]);
}
}
return dp[(m - 1)%2][n - 1];
} | public int sample_funcname ( int [ ] [ ] var_0 ) {
if ( var_0 == null || var_0 . length == 0 || var_0 [ 0 ] == null || var_0 [ 0 ] . length == 0 ) return 0 ;
int var_4 = var_0 . length , var_5 = var_0 [ 0 ] . length ;
int [ ] [ ] var_7 = new int [ 2 ] [ var_5 ] ;
for ( int var_8 = 0 ;
var_8 < var_4 ;
var_8 ++ ) {
for ( int var_9 = 0 ;
var_9 < var_5 ;
var_9 ++ ) {
var_7 [ var_8 % 2 ] [ var_9 ] = var_0 [ var_8 ] [ var_9 ] ;
if ( var_8 == 0 && var_9 == 0 ) continue ;
if ( var_8 == 0 && var_9 > 0 ) var_7 [ var_8 % 2 ] [ var_9 ] += var_7 [ var_8 % 2 ] [ var_9 - 1 ] ;
else if ( var_8 > 0 && var_9 == 0 ) var_7 [ var_8 % 2 ] [ var_9 ] += var_7 [ ( var_8 - 1 ) % 2 ] [ var_9 ] ;
else var_7 [ var_8 % 2 ] [ var_9 ] += Math . min ( var_7 [ ( var_8 - 1 ) % 2 ] [ var_9 ] , var_7 [ var_8 % 2 ] [ var_9 - 1 ] ) ;
}
}
return var_7 [ ( var_4 - 1 ) % 2 ] [ var_5 - 1 ] ;
}
|
129 | <awangdev>443. String Compression_1.java | private int assignChar(char[] chars, int index, int count) {
String s = String.valueOf(count);
for (char c : s.toCharArray()) {
chars[index++] = c;
}
return index;
} | private int sample_funcname ( char [ ] var_0 , int var_1 , int var_2 ) {
String var_5 = String . valueOf ( var_2 ) ;
for ( char var_7 : var_5 . toCharArray ( ) ) {
var_0 [ var_1 ++ ] = var_7 ;
}
return var_1 ;
}
|
130 | <awangdev>332. Reconstruct Itinerary_1.java | private Map<String, List<String>> buildMap(List<List<String>> tickets) {
Map<String, List<String>> map = new HashMap<>();
for (List<String> ticket : tickets) {
map.putIfAbsent(ticket.get(0), new LinkedList<>());
map.get(ticket.get(0)).add(ticket.get(1));
}
for (String key : map.keySet()) Collections.sort(map.get(key));
return map;
} | private Map < String , List < String >> var_2 ( List < List < String >> var_3 ) {
Map < String , List < String >> var_6 = new HashMap < > ( ) ;
for ( List < String > var_7 : var_3 ) {
var_6 . putIfAbsent ( var_7 . get ( 0 ) , new LinkedList < > ( ) ) ;
var_6 . get ( var_7 . get ( 0 ) ) . add ( var_7 . get ( 1 ) ) ;
}
for ( String var_15 : var_6 . keySet ( ) ) Collections . sort ( var_6 . get ( var_15 ) ) ;
return var_6 ;
}
|
131 | <awangdev>Pow(x, n)_1.java | public double helper(double x, int n) {
if (n == 0) {
return 1;
}
if (n == 1) {
return x;
}
double val = helper(x, n / 2);
if (n % 2 == 0) {
return val * val;
} else {
return val * val * x;
}
} | public double sample_funcname ( double x , int n ) {
if ( n == 0 ) {
return 1 ;
}
if ( n == 1 ) {
return x ;
}
double val = helper ( x , n / 2 ) ;
if ( n % 2 == 0 ) {
return val * val ;
}
else {
return val * val * x ;
}
}
|
132 | <awangdev>Backpack_4.java | public int backPack(int m, int[] A) {
if (A == null || A.length < 0) {
return 0;
}
int n = A.length;
boolean[][] dp = new boolean[2][m + 1];
int curr = 0;
int pre = 1;
dp[curr][0] = true;
for (int j = 1; j <= m; j++) {
dp[curr][j] = false;
}
for (int i = 1; i <= n; i++) {
curr = pre;
pre = 1 - curr;
for (int j = 0; j <= m; j++) {
dp[curr][j] = dp[pre][j];
if (j - A[i - 1] >= 0) {
dp[curr][j] |= dp[pre][j - A[i - 1]];
}
}
}
for (int j = m; j >= 0; j--) {
if (dp[curr][j]) {
return j;
}
}
return 0;
} | public int sample_funcname ( int var_0 , int [ ] var_1 ) {
if ( var_1 == null || var_1 . length < 0 ) {
return 0 ;
}
int var_5 = var_1 . length ;
boolean [ ] [ ] var_9 = new boolean [ 2 ] [ var_0 + 1 ] ;
int var_10 = 0 ;
int var_11 = 1 ;
var_9 [ var_10 ] [ 0 ] = true ;
for ( int var_12 = 1 ;
var_12 <= var_0 ;
var_12 ++ ) {
var_9 [ var_10 ] [ var_12 ] = false ;
}
for ( int var_13 = 1 ;
var_13 <= var_5 ;
var_13 ++ ) {
var_10 = var_11 ;
var_11 = 1 - var_10 ;
for ( int var_12 = 0 ;
var_12 <= var_0 ;
var_12 ++ ) {
var_9 [ var_10 ] [ var_12 ] = var_9 [ var_11 ] [ var_12 ] ;
if ( var_12 - var_1 [ var_13 - 1 ] >= 0 ) {
var_9 [ var_10 ] [ var_12 ] |= var_9 [ var_11 ] [ var_12 - var_1 [ var_13 - 1 ] ] ;
}
}
}
for ( int var_12 = var_0 ;
var_12 >= 0 ;
var_12 -- ) {
if ( var_9 [ var_10 ] [ var_12 ] ) {
return var_12 ;
}
}
return 0 ;
}
|
133 | <awangdev>Triangles_3.java | public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return 0;
}
int n = triangle.size();
int[] dp = new int[n];
for (int j = 0; j < n; j++) {
dp[j] = triangle.get(n - 1).get(j);
}
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j);
}
}
return dp[0];
} | public int sample_funcname ( List < List < Integer >> var_2 ) {
if ( var_2 == null || var_2 . size ( ) == 0 ) {
return 0 ;
}
int var_6 = var_2 . size ( ) ;
int [ ] var_7 = new int [ var_6 ] ;
for ( int var_8 = 0 ;
var_8 < var_6 ;
var_8 ++ ) {
var_7 [ var_8 ] = var_2 . get ( var_6 - 1 ) . get ( var_8 ) ;
}
for ( int var_12 = var_6 - 2 ;
var_12 >= 0 ;
var_12 -- ) {
for ( int var_8 = 0 ;
var_8 <= var_12 ;
var_8 ++ ) {
var_7 [ var_8 ] = Math . min ( var_7 [ var_8 ] , var_7 [ var_8 + 1 ] ) + var_2 . get ( var_12 ) . get ( var_8 ) ;
}
}
return var_7 [ 0 ] ;
}
|
134 | <awangdev>31. Next Permutation_1.java | private int findLow(int[] nums) {
int end = nums.length - 1;
while (end > 0) {
if (nums[end - 1] < nums[end]) return end - 1;
end--;
}
return -1;
} | private int sample_funcname ( int [ ] var_0 ) {
int var_3 = var_0 . length - 1 ;
while ( var_3 > 0 ) {
if ( var_0 [ var_3 - 1 ] < var_0 [ var_3 ] ) return var_3 - 1 ;
var_3 -- ;
}
return - 1 ;
}
|
135 | <awangdev>5. Longest Palindromic Substring_1.java | public String longestPalindrome(String s) {
if (s == null || s.length() <= 1) return s;
int n = s.length();
boolean dp[][] = new boolean[n][n];
String str = String.valueOf(s.charAt(n - 1));
for (int i = n - 1; i >= 0; i--) {
dp[i][i] = true;
for (int j = i + 1; j < n; j++) {
if (s.charAt(i) == s.charAt(j) && (i + 1 == j || dp[i + 1][j - 1])) {
dp[i][j] = true;
str = str.length() <= (j - i + 1) ? s.substring(i, j + 1) : str;
}
}
}
return str;
} | public String var_0 ( String var_2 ) {
if ( var_2 == null || var_2 . length ( ) <= 1 ) return var_2 ;
int var_6 = var_2 . length ( ) ;
boolean var_8 [ ] [ ] = new boolean [ var_6 ] [ var_6 ] ;
String var_11 = String . valueOf ( var_2 . charAt ( var_6 - 1 ) ) ;
for ( int var_12 = var_6 - 1 ;
var_12 >= 0 ;
var_12 -- ) {
var_8 [ var_12 ] [ var_12 ] = true ;
for ( int var_14 = var_12 + 1 ;
var_14 < var_6 ;
var_14 ++ ) {
if ( var_2 . charAt ( var_12 ) == var_2 . charAt ( var_14 ) && ( var_12 + 1 == var_14 || var_8 [ var_12 + 1 ] [ var_14 - 1 ] ) ) {
var_8 [ var_12 ] [ var_14 ] = true ;
var_11 = var_11 . length ( ) <= ( var_14 - var_12 + 1 ) ? var_2 . substring ( var_12 , var_14 + 1 ) : var_11 ;
}
}
}
return var_11 ;
}
|
136 | <awangdev>1048. Longest String Chain_2.java | private List[] buildBucket(String[] words) {
List[] bucket = new List[17];
for (String w : words) {
int len = w.length();
if (bucket[len] == null) bucket[len] = new ArrayList<>();
bucket[len].add(w);
}
return bucket;
} | private List [ ] var_0 ( String [ ] var_2 ) {
List [ ] var_5 = new List [ 17 ] ;
for ( String var_6 : var_2 ) {
int var_8 = var_6 . length ( ) ;
if ( var_5 [ var_8 ] == null ) var_5 [ var_8 ] = new ArrayList < > ( ) ;
var_5 [ var_8 ] . add ( var_6 ) ;
}
return var_5 ;
}
|
137 | <awangdev>1007. Minimum Domino Rotations For Equal Row_2.java | private int checkRotation(int[] A, int[] B, int candidate) {
int rotationA = 0, rotationB = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] != candidate && B[i] != candidate) return -1;
else if (A[i] != candidate) rotationA++;
else if (B[i] != candidate) rotationB++;
}
return Math.min(rotationA, rotationB);
} | private int sample_funcname ( int [ ] var_0 , int [ ] var_1 , int var_2 ) {
int var_4 = 0 , var_5 = 0 ;
for ( int var_6 = 0 ;
var_6 < var_0 . length ;
var_6 ++ ) {
if ( var_0 [ var_6 ] != var_2 && var_1 [ var_6 ] != var_2 ) return - 1 ;
else if ( var_0 [ var_6 ] != var_2 ) var_4 ++ ;
else if ( var_1 [ var_6 ] != var_2 ) var_5 ++ ;
}
return Math . min ( var_4 , var_5 ) ;
}
|
138 | <awangdev>Longest Increasing Continuous subsequence_1.java | public int longestIncreasingContinuousSubsequence(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
int leftRun = 1;
int rightRun = 1;
int ans = 1;
for (int i = 1; i < A.length; i++) {
if (A[i] > A[i - 1]) {
leftRun++;
} else {
leftRun = 1;
}
ans = Math.max(ans, leftRun);
}
for (int i = A.length - 2; i >= 0; i--) {
if (A[i] > A[i + 1]) {
rightRun++;
} else {
rightRun = 1;
}
ans = Math.max(ans, rightRun);
}
return ans;
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length == 0 ) {
return 0 ;
}
int var_5 = 1 ;
int var_6 = 1 ;
int var_7 = 1 ;
for ( int var_8 = 1 ;
var_8 < var_0 . length ;
var_8 ++ ) {
if ( var_0 [ var_8 ] > var_0 [ var_8 - 1 ] ) {
var_5 ++ ;
}
else {
var_5 = 1 ;
}
var_7 = Math . max ( var_7 , var_5 ) ;
}
for ( int var_8 = var_0 . length - 2 ;
var_8 >= 0 ;
var_8 -- ) {
if ( var_0 [ var_8 ] > var_0 [ var_8 + 1 ] ) {
var_6 ++ ;
}
else {
var_6 = 1 ;
}
var_7 = Math . max ( var_7 , var_6 ) ;
}
return var_7 ;
}
|
139 | <awangdev>Perfect Squares_3.java | public int numSquares(int n) {
if (n <= 0) {
return 0;
}
Queue<Integer> queue = new LinkedList<>();
Set<Integer> set = new HashSet<>();
queue.offer(n);
int level = 1;
while (!queue.isEmpty()) {
int size = queue.size();
for (int x = 0; x < size; x++) {
int num = queue.poll();
for (int i = 1; i <= num; i++) {
int remain = num - i * i;
if (remain == 0) {
return level;
} else if (remain > 0 && !set.contains(remain)) {
queue.offer(remain);
set.add(remain);
} else if (remain < 0) {
break;
}
}
}
level++;
}
return level;
} | public int sample_funcname ( int var_0 ) {
if ( var_0 <= 0 ) {
return 0 ;
}
Queue < Integer > var_5 = new LinkedList < > ( ) ;
Set < Integer > var_8 = new HashSet < > ( ) ;
var_5 . offer ( var_0 ) ;
int var_11 = 1 ;
while ( ! var_5 . isEmpty ( ) ) {
int size = var_5 . size ( ) ;
for ( int var_14 = 0 ;
var_14 < size ;
var_14 ++ ) {
int var_16 = var_5 . poll ( ) ;
for ( int var_17 = 1 ;
var_17 <= var_16 ;
var_17 ++ ) {
int var_18 = var_16 - var_17 * var_17 ;
if ( var_18 == 0 ) {
return var_11 ;
}
else if ( var_18 > 0 && ! var_8 . contains ( var_18 ) ) {
var_5 . offer ( var_18 ) ;
var_8 . add ( var_18 ) ;
}
else if ( var_18 < 0 ) {
break ;
}
}
}
var_11 ++ ;
}
return var_11 ;
}
|
140 | <awangdev>Best Time to Buy and Sell Stock with Cooldown_1.java | public int maxProfit(int[] prices) {
if (prices == null || prices.length <= 1) {
return 0;
}
int n = prices.length;
int[][] dp = new int[n][2];
dp[0][0] = - prices[0];
dp[0][1] = 0;
for (int i = 1; i < n; i++) {
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
dp[i][0] = dp[i - 1][0];
if (i >= 2) {
dp[i][0] = Math.max(dp[i][0], dp[i - 2][1] - prices[i]);
} else {
dp[i][0] = Math.max(dp[i][0], - prices[i]);
}
}
return dp[n - 1][1];
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length <= 1 ) {
return 0 ;
}
int var_5 = var_0 . length ;
int [ ] [ ] var_7 = new int [ var_5 ] [ 2 ] ;
var_7 [ 0 ] [ 0 ] = - var_0 [ 0 ] ;
var_7 [ 0 ] [ 1 ] = 0 ;
for ( int var_8 = 1 ;
var_8 < var_5 ;
var_8 ++ ) {
var_7 [ var_8 ] [ 1 ] = Math . max ( var_7 [ var_8 - 1 ] [ 1 ] , var_7 [ var_8 - 1 ] [ 0 ] + var_0 [ var_8 ] ) ;
var_7 [ var_8 ] [ 0 ] = var_7 [ var_8 - 1 ] [ 0 ] ;
if ( var_8 >= 2 ) {
var_7 [ var_8 ] [ 0 ] = Math . max ( var_7 [ var_8 ] [ 0 ] , var_7 [ var_8 - 2 ] [ 1 ] - var_0 [ var_8 ] ) ;
}
else {
var_7 [ var_8 ] [ 0 ] = Math . max ( var_7 [ var_8 ] [ 0 ] , - var_0 [ var_8 ] ) ;
}
}
return var_7 [ var_5 - 1 ] [ 1 ] ;
}
|
141 | <awangdev>Decode String_2.java | private String popStack(Stack<Object> stack) {
StringBuffer sb = new StringBuffer();
Stack<String> buffer = new Stack<>();
while (!stack.isEmpty() && (stack.peek() instanceof String)) {
buffer.push((String) stack.pop());
}
while (!buffer.isEmpty()) {
sb.append(buffer.pop());
}
return sb.toString();
} | private String var_0 ( Stack < Object > var_3 ) {
StringBuffer var_5 = new StringBuffer ( ) ;
Stack < String > var_7 = new Stack < > ( ) ;
while ( ! var_3 . isEmpty ( ) && ( var_3 . peek ( ) instanceof String ) ) {
var_7 . push ( ( String ) var_3 . pop ( ) ) ;
}
while ( ! var_7 . isEmpty ( ) ) {
var_5 . append ( var_7 . pop ( ) ) ;
}
return var_5 . toString ( ) ;
}
|
142 | <awangdev>Interleaving String_2.java | public boolean isInterleave(String s1, String s2, String s3) {
if (s1 == null || s2 == null || s1.length() + s2.length() != s3.length()) {
return false;
}
int m = s1.length();
int n = s2.length();
boolean[][] dp = new boolean[2][n + 1];
int curr = 0;
int prev = 0;
for (int i = 0; i <= m; i++) {
prev = curr;
curr = 1 - prev;
for (int j = 0; j <= n; j++) {
if (i == 0 && j == 0) {
dp[curr][j] = true;
continue;
}
dp[curr][j] = false;
if (i > 0 && s1.charAt(i - 1) == s3.charAt(i + j - 1)) {
dp[curr][j] |= dp[prev][j];
}
if (j > 0 && s2.charAt(j - 1) == s3.charAt(i + j - 1)) {
dp[curr][j] |= dp[curr][j - 1];
}
}
}
return dp[curr][n];
} | public boolean var_0 ( String var_2 , String var_3 , String var_4 ) {
if ( var_2 == null || var_3 == null || var_2 . length ( ) + var_3 . length ( ) != var_4 . length ( ) ) {
return false ;
}
int var_7 = var_2 . length ( ) ;
int var_8 = var_3 . length ( ) ;
boolean [ ] [ ] var_12 = new boolean [ 2 ] [ var_8 + 1 ] ;
int var_14 = 0 ;
int var_15 = 0 ;
for ( int var_16 = 0 ;
var_16 <= var_7 ;
var_16 ++ ) {
var_15 = var_14 ;
var_14 = 1 - var_15 ;
for ( int var_17 = 0 ;
var_17 <= var_8 ;
var_17 ++ ) {
if ( var_16 == 0 && var_17 == 0 ) {
var_12 [ var_14 ] [ var_17 ] = true ;
continue ;
}
var_12 [ var_14 ] [ var_17 ] = false ;
if ( var_16 > 0 && var_2 . charAt ( var_16 - 1 ) == var_4 . charAt ( var_16 + var_17 - 1 ) ) {
var_12 [ var_14 ] [ var_17 ] |= var_12 [ var_15 ] [ var_17 ] ;
}
if ( var_17 > 0 && var_3 . charAt ( var_17 - 1 ) == var_4 . charAt ( var_16 + var_17 - 1 ) ) {
var_12 [ var_14 ] [ var_17 ] |= var_12 [ var_14 ] [ var_17 - 1 ] ;
}
}
}
return var_12 [ var_14 ] [ var_8 ] ;
}
|
143 | <awangdev>Find Peak Element II_1.java | private List<Integer> find(int x1, int x2, int y1, int y2, int[][] A) {
int midX = x1 + ((x2 - x1) >> 1);
int midY = y1 + ((y2 - y1) >> 1);
int x = midX, y = midY;
int max = A[x][y];
for (int j = y1; j <= y2; j++) {
if (A[midX][j] > max) {
max = A[midX][j];
x = midX;
y = j;
}
}
for (int i = x1; i <= x2; i++) {
if (A[i][midY] > max) {
max = A[i][midY];
y = midY;
x = i;
}
}
if (A[x - 1][y] > max) {
return find(x1, midX - 1, y1, y2, A);
} else if (A[x + 1][y] > max) {
return find(x1 + 1, midX, y1, y2, A);
} else if (A[x][y - 1] > max) {
return find(x1, x2, y1, midY - 1, A);
} else if (A[x][y + 1] > max) {
return find(x1, x2, midY + 1, y2, A);
}
return new ArrayList<>(Arrays.asList(x, y));
} | private List < Integer > sample_funcname ( int x1 , int x2 , int y1 , int y2 , int [ ] [ ] A ) {
int midX = x1 + ( ( x2 - x1 ) >> 1 ) ;
int midY = y1 + ( ( y2 - y1 ) >> 1 ) ;
int x = midX , y = midY ;
int max = A [ x ] [ y ] ;
for ( int j = y1 ;
j <= y2 ;
j ++ ) {
if ( A [ midX ] [ j ] > max ) {
max = A [ midX ] [ j ] ;
x = midX ;
y = j ;
}
}
for ( int i = x1 ;
i <= x2 ;
i ++ ) {
if ( A [ i ] [ midY ] > max ) {
max = A [ i ] [ midY ] ;
y = midY ;
x = i ;
}
}
if ( A [ x - 1 ] [ y ] > max ) {
return find ( x1 , midX - 1 , y1 , y2 , A ) ;
}
else if ( A [ x + 1 ] [ y ] > max ) {
return find ( x1 + 1 , midX , y1 , y2 , A ) ;
}
else if ( A [ x ] [ y - 1 ] > max ) {
return find ( x1 , x2 , y1 , midY - 1 , A ) ;
}
else if ( A [ x ] [ y + 1 ] > max ) {
return find ( x1 , x2 , midY + 1 , y2 , A ) ;
}
return new ArrayList < > ( Arrays . asList ( x , y ) ) ;
}
|
144 | <awangdev>Ugly Number_1.java | public boolean isUgly(int num) {
if (num == 0) {
return false;
}
while (num % 2 == 0) num /= 2;
while (num % 3 == 0) num /= 3;
while (num % 5 == 0) num /= 5;
return num == 1;
} | public boolean var_0 ( int var_1 ) {
if ( var_1 == 0 ) {
return false ;
}
while ( var_1 % 2 == 0 ) var_1 /= 2 ;
while ( var_1 % 3 == 0 ) var_1 /= 3 ;
while ( var_1 % 5 == 0 ) var_1 /= 5 ;
return var_1 == 1 ;
}
|
145 | <awangdev>125. Valid Palindrome_1.java | private boolean isValid(char c) {
if (c >= '0' && c <= '9') return true;
if (c >= 'a' && c <= 'z') return true;
if (c >= 'A' && c <= 'Z') return true;
return false;
} | private boolean var_0 ( char var_1 ) {
if ( var_1 >= '0' && var_1 <= '9' ) return true ;
if ( var_1 >= 'a' && var_1 <= 'z' ) return true ;
if ( var_1 >= 'A' && var_1 <= 'Z' ) return true ;
return false ;
}
|
146 | <awangdev>Summary Ranges_1.java | public List<String> summaryRanges(int[] nums) {
List<String> rst = new ArrayList<>();
if (nums == null || nums.length == 0) {
return rst;
}
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
list.add(nums[i]);
if (i + 1 == nums.length || nums[i] + 1 != nums[i + 1]) {
if (list.size() == 1) {
rst.add(list.get(0) + "");
} else {
rst.add(list.get(0) + "->" + list.get(list.size() - 1));
}
list = new ArrayList<Integer>();
}
}
return rst;
} | public List < String > var_1 ( int [ ] var_2 ) {
List < String > var_5 = new ArrayList < > ( ) ;
if ( var_2 == null || var_2 . length == 0 ) {
return var_5 ;
}
List < Integer > var_10 = new ArrayList < > ( ) ;
for ( int var_11 = 0 ;
var_11 < var_2 . length ;
var_11 ++ ) {
var_10 . add ( var_2 [ var_11 ] ) ;
if ( var_11 + 1 == var_2 . length || var_2 [ var_11 ] + 1 != var_2 [ var_11 + 1 ] ) {
if ( var_10 . size ( ) == 1 ) {
var_5 . add ( var_10 . get ( 0 ) + "" ) ;
}
else {
var_5 . add ( var_10 . get ( 0 ) + "->" + var_10 . get ( var_10 . size ( ) - 1 ) ) ;
}
var_10 = new ArrayList < Integer > ( ) ;
}
}
return var_5 ;
}
|
147 | <awangdev>Backpack III_2.java | public int backPackIII(int[] A, int[] V, int m) {
if (A == null || A.length == 0 || V == null || V.length == 0 || m <= 0) {
return 0;
}
int n = A.length;
int[][] dp = new int[n + 1][m + 1];
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++) {
dp[i][j] = dp[i - 1][j];
if (j >= A[i - 1]) {
dp[i][j] = Math.max(dp[i][j], dp[i][j - A[i - 1]] + V[i - 1]);
}
}
}
return dp[n][m];
} | public int sample_funcname ( int [ ] var_0 , int [ ] var_1 , int var_2 ) {
if ( var_0 == null || var_0 . length == 0 || var_1 == null || var_1 . length == 0 || var_2 <= 0 ) {
return 0 ;
}
int var_6 = var_0 . length ;
int [ ] [ ] var_8 = new int [ var_6 + 1 ] [ var_2 + 1 ] ;
var_8 [ 0 ] [ 0 ] = 0 ;
for ( int var_9 = 1 ;
var_9 <= var_6 ;
var_9 ++ ) {
for ( int var_10 = 0 ;
var_10 <= var_2 ;
var_10 ++ ) {
var_8 [ var_9 ] [ var_10 ] = var_8 [ var_9 - 1 ] [ var_10 ] ;
if ( var_10 >= var_0 [ var_9 - 1 ] ) {
var_8 [ var_9 ] [ var_10 ] = Math . max ( var_8 [ var_9 ] [ var_10 ] , var_8 [ var_9 ] [ var_10 - var_0 [ var_9 - 1 ] ] + var_1 [ var_9 - 1 ] ) ;
}
}
}
return var_8 [ var_6 ] [ var_2 ] ;
}
|
148 | <awangdev>Word Ladder_2.java | public int ladderLength(String start, String end, Set<String> dict) {
if (start == null || end == null || dict == null || start.length() != end.length()) {
return 0;
}
Queue<String> q = new LinkedList<String>();
HashSet<String> set = new HashSet<String>();
int level = 1;
int len = Integer.MAX_VALUE;
q.offer(start);
set.add(start);
while(!q.isEmpty()) {
int size = q.size();
level++;
for (int k = 0; k < size; k++) {
String str = q.poll();
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j < 26; j++) {
String newStr;
if (i == 0 && str.length() == 1) {
newStr = "" + (char)('a' + j);
}
else if (i == str.length() - 1) {
newStr = str.substring(0, i) + (char)('a' + j);
} else {
newStr = str.substring(0, i) + (char)('a' + j) + str.substring(i + 1);
}
if (!set.contains(newStr) && dict.contains(newStr)) {
if (newStr.equals(end)) {
len = Math.min(len, level);
} else {
set.add(newStr);
q.offer(newStr);
}
}
}
}
}
}
return len;
} | public int sample_funcname ( String var_1 , String var_2 , Set < String > var_4 ) {
if ( var_1 == null || var_2 == null || var_4 == null || var_1 . length ( ) != var_2 . length ( ) ) {
return 0 ;
}
Queue < String > var_10 = new LinkedList < String > ( ) ;
HashSet < String > var_12 = new HashSet < String > ( ) ;
int var_14 = 1 ;
int var_17 = Integer . MAX_VALUE ;
var_10 . offer ( var_1 ) ;
var_12 . add ( var_1 ) ;
while ( ! var_10 . isEmpty ( ) ) {
int size = var_10 . size ( ) ;
var_14 ++ ;
for ( int var_22 = 0 ;
var_22 < size ;
var_22 ++ ) {
String var_24 = var_10 . poll ( ) ;
for ( int var_25 = 0 ;
var_25 < var_24 . length ( ) ;
var_25 ++ ) {
for ( int var_26 = 0 ;
var_26 < 26 ;
var_26 ++ ) {
String var_28 ;
if ( var_25 == 0 && var_24 . length ( ) == 1 ) {
var_28 = "" + ( char ) ( 'a' + var_26 ) ;
}
else if ( var_25 == var_24 . length ( ) - 1 ) {
var_28 = var_24 . substring ( 0 , var_25 ) + ( char ) ( 'a' + var_26 ) ;
}
else {
var_28 = var_24 . substring ( 0 , var_25 ) + ( char ) ( 'a' + var_26 ) + var_24 . substring ( var_25 + 1 ) ;
}
if ( ! var_12 . contains ( var_28 ) && var_4 . contains ( var_28 ) ) {
if ( var_28 . equals ( var_2 ) ) {
var_17 = Math . min ( var_17 , var_14 ) ;
}
else {
var_12 . add ( var_28 ) ;
var_10 . offer ( var_28 ) ;
}
}
}
}
}
}
return var_17 ;
}
|
149 | <awangdev>Wildcard Matching_2.java | public boolean isMatch(String s, String p) {
if (s == null || p == null) {
return false;
}
int m = s.length();
int n = p.length();
boolean[][] dp = new boolean[2][n + 1];
char[] ss = s.toCharArray();
char[] pp = p.toCharArray();
int curr = 0;
int prev = 1;
for (int i = 0; i <= m; i++) {
prev = curr;
curr = 1 - prev;
for (int j = 0; j <= n; j++) {
if (i == 0 && j == 0) {
dp[curr][j] = true;
continue;
}
if (j == 0) {
dp[curr][j] = false;
continue;
}
dp[curr][j] = false;
if (pp[j - 1] != '*') {
if (i >= 1 && (ss[i - 1] == pp[j - 1] || pp[j - 1] == '?')) {
dp[curr][j] = dp[prev][j - 1];
}
} else {
dp[curr][j] |= dp[curr][j - 1];
if (i >= 1) {
dp[curr][j] |= dp[prev][j];
}
}
}
}
return dp[curr][n];
} | public boolean var_0 ( String var_2 , String var_3 ) {
if ( var_2 == null || var_3 == null ) {
return false ;
}
int var_6 = var_2 . length ( ) ;
int var_7 = var_3 . length ( ) ;
boolean [ ] [ ] var_11 = new boolean [ 2 ] [ var_7 + 1 ] ;
char [ ] var_13 = var_2 . toCharArray ( ) ;
char [ ] var_14 = var_3 . toCharArray ( ) ;
int var_16 = 0 ;
int var_17 = 1 ;
for ( int var_18 = 0 ;
var_18 <= var_6 ;
var_18 ++ ) {
var_17 = var_16 ;
var_16 = 1 - var_17 ;
for ( int var_19 = 0 ;
var_19 <= var_7 ;
var_19 ++ ) {
if ( var_18 == 0 && var_19 == 0 ) {
var_11 [ var_16 ] [ var_19 ] = true ;
continue ;
}
if ( var_19 == 0 ) {
var_11 [ var_16 ] [ var_19 ] = false ;
continue ;
}
var_11 [ var_16 ] [ var_19 ] = false ;
if ( var_14 [ var_19 - 1 ] != '*' ) {
if ( var_18 >= 1 && ( var_13 [ var_18 - 1 ] == var_14 [ var_19 - 1 ] || var_14 [ var_19 - 1 ] == '?' ) ) {
var_11 [ var_16 ] [ var_19 ] = var_11 [ var_17 ] [ var_19 - 1 ] ;
}
}
else {
var_11 [ var_16 ] [ var_19 ] |= var_11 [ var_16 ] [ var_19 - 1 ] ;
if ( var_18 >= 1 ) {
var_11 [ var_16 ] [ var_19 ] |= var_11 [ var_17 ] [ var_19 ] ;
}
}
}
}
return var_11 [ var_16 ] [ var_7 ] ;
}
|
150 | <awangdev>Walls and Gates_3.java | private boolean validateCoordinate(int[][] rooms, int x, int y) {
return x < 0 || x >= rooms.length || y < 0 || y >= rooms[0].length || rooms[x][y] == -1 || rooms[x][y] == 0;
} | private boolean var_0 ( int [ ] [ ] var_1 , int var_2 , int var_3 ) {
return var_2 < 0 || var_2 >= var_1 . length || var_3 < 0 || var_3 >= var_1 [ 0 ] . length || var_1 [ var_2 ] [ var_3 ] == - 1 || var_1 [ var_2 ] [ var_3 ] == 0 ;
}
|
151 | <awangdev>Maximum Vacation Days_1.java | public int maxVacationDays(int[][] flights, int[][] days) {
int m = days[0].length;
int n = days.length;
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0) {
dp[i][j] = j == 0 || flights[0][j] != 0 ? days[j][i]: Integer.MIN_VALUE;
continue;
}
dp[i][j] = Integer.MIN_VALUE;
for (int k = 0; k < m; k++) {
if ((k == j || flights[k][j] == 1) && dp[i - 1][k] != Integer.MIN_VALUE) {
dp[i][j] = Math.max(dp[i - 1][k] + days[j][i], dp[i][j]);
}
}
}
}
int max = 0;
for (int i = 0; i < n; i++) {
max = Math.max(max, dp[m - 1][i]);
}
return max;
} | public int sample_funcname ( int [ ] [ ] var_0 , int [ ] [ ] var_1 ) {
int var_4 = var_1 [ 0 ] . length ;
int var_5 = var_1 . length ;
int [ ] [ ] var_6 = new int [ var_4 ] [ var_5 ] ;
for ( int var_7 = 0 ;
var_7 < var_4 ;
var_7 ++ ) {
for ( int var_8 = 0 ;
var_8 < var_5 ;
var_8 ++ ) {
if ( var_7 == 0 ) {
var_6 [ var_7 ] [ var_8 ] = var_8 == 0 || var_0 [ 0 ] [ var_8 ] != 0 ? var_1 [ var_8 ] [ var_7 ] : Integer . MIN_VALUE ;
continue ;
}
var_6 [ var_7 ] [ var_8 ] = Integer . MIN_VALUE ;
for ( int var_11 = 0 ;
var_11 < var_4 ;
var_11 ++ ) {
if ( ( var_11 == var_8 || var_0 [ var_11 ] [ var_8 ] == 1 ) && var_6 [ var_7 - 1 ] [ var_11 ] != Integer . MIN_VALUE ) {
var_6 [ var_7 ] [ var_8 ] = Math . max ( var_6 [ var_7 - 1 ] [ var_11 ] + var_1 [ var_8 ] [ var_7 ] , var_6 [ var_7 ] [ var_8 ] ) ;
}
}
}
}
int max = 0 ;
for ( int var_7 = 0 ;
var_7 < var_5 ;
var_7 ++ ) {
max = Math . max ( max , var_6 [ var_4 - 1 ] [ var_7 ] ) ;
}
return max ;
}
|
152 | <awangdev>Palindrome Partitioning II_1.java | private boolean[][] calcPalindrome(char[] s) {
int n = s.length;
boolean[][] palindrome = new boolean[n][n];
for (int mid = 0; mid < n; mid++) {
int i = mid, j = mid;
while (i >= 0 && j < n && s[i] == s[j]) {
palindrome[i][j] = true;
i--;
j++;
}
i = mid;
j = mid + 1;
while (i >= 0 && j < n && s[i] == s[j]) {
palindrome[i][j] = true;
i--;
j++;
}
}
return palindrome;
} | private boolean [ ] [ ] var_0 ( char [ ] var_1 ) {
int var_3 = var_1 . length ;
boolean [ ] [ ] var_5 = new boolean [ var_3 ] [ var_3 ] ;
for ( int var_7 = 0 ;
var_7 < var_3 ;
var_7 ++ ) {
int var_8 = var_7 , var_9 = var_7 ;
while ( var_8 >= 0 && var_9 < var_3 && var_1 [ var_8 ] == var_1 [ var_9 ] ) {
var_5 [ var_8 ] [ var_9 ] = true ;
var_8 -- ;
var_9 ++ ;
}
var_8 = var_7 ;
var_9 = var_7 + 1 ;
while ( var_8 >= 0 && var_9 < var_3 && var_1 [ var_8 ] == var_1 [ var_9 ] ) {
var_5 [ var_8 ] [ var_9 ] = true ;
var_8 -- ;
var_9 ++ ;
}
}
return var_5 ;
}
|
153 | <awangdev>Friends Of Appropriate Ages_1.java | public int numFriendRequests(int[] ages) {
int[] count = new int[121];
for (int age: ages) {
count[age]++;
}
int rst = 0;
for (int i = 15; i < 121; i++) {
for (int j = (int)(i * 0.5 + 8); j <= i; j++) {
rst += i == j ? count[j] * (count[i] - 1) : count[j] * count[i];
}
}
return rst;
} | public int sample_funcname ( int [ ] var_0 ) {
int [ ] var_2 = new int [ 121 ] ;
for ( int var_3 : var_0 ) {
var_2 [ var_3 ] ++ ;
}
int var_5 = 0 ;
for ( int var_7 = 15 ;
var_7 < 121 ;
var_7 ++ ) {
for ( int var_10 = ( int ) ( var_7 * 0.5 + 8 ) ;
var_10 <= var_7 ;
var_10 ++ ) {
var_5 += var_7 == var_10 ? var_2 [ var_10 ] * ( var_2 [ var_7 ] - 1 ) : var_2 [ var_10 ] * var_2 [ var_7 ] ;
}
}
return var_5 ;
}
|
154 | <awangdev>229. Majority Element II_1.java | public List<Integer> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<>();
if (nums == null || nums.length == 0) {
return result;
}
int valA = 0, valB = 0, countA = 0, countB = 0;
for (int num : nums) {
if (num == valA) {
countA++;
} else if (num == valB) {
countB++;
} else if (countA == 0) {
valA = num;
countA++;
} else if (countB == 0){
valB = num;
countB++;
} else {
countA--;
countB--;
}
}
countA = 0;
countB = 0;
for (int num : nums) {
if (num == valA) {
countA++;
} else if (num == valB) {
countB++;
}
}
if (countA > nums.length / 3) result.add(valA);
if (countB > nums.length / 3) result.add(valB);
return result;
} | public List < Integer > var_1 ( int [ ] var_2 ) {
List < Integer > var_5 = new ArrayList < > ( ) ;
if ( var_2 == null || var_2 . length == 0 ) {
return var_5 ;
}
int var_9 = 0 , var_10 = 0 , var_11 = 0 , var_12 = 0 ;
for ( int var_13 : var_2 ) {
if ( var_13 == var_9 ) {
var_11 ++ ;
}
else if ( var_13 == var_10 ) {
var_12 ++ ;
}
else if ( var_11 == 0 ) {
var_9 = var_13 ;
var_11 ++ ;
}
else if ( var_12 == 0 ) {
var_10 = var_13 ;
var_12 ++ ;
}
else {
var_11 -- ;
var_12 -- ;
}
}
var_11 = 0 ;
var_12 = 0 ;
for ( int var_13 : var_2 ) {
if ( var_13 == var_9 ) {
var_11 ++ ;
}
else if ( var_13 == var_10 ) {
var_12 ++ ;
}
}
if ( var_11 > var_2 . length / 3 ) var_5 . add ( var_9 ) ;
if ( var_12 > var_2 . length / 3 ) var_5 . add ( var_10 ) ;
return var_5 ;
}
|
155 | <awangdev>274.H-Index_3.java | public int hIndex(int[] citations) {
if (citations == null || citations.length == 0) {
return 0;
}
int n = citations.length;
Arrays.sort(citations);
int index = n;
for (int i = n - 1; i >= 0; i--) {
if (citations[i] >= n - i) {
index = i;
continue;
}
break;
}
return n - index;
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length == 0 ) {
return 0 ;
}
int var_4 = var_0 . length ;
Arrays . sort ( var_0 ) ;
int var_7 = var_4 ;
for ( int var_9 = var_4 - 1 ;
var_9 >= 0 ;
var_9 -- ) {
if ( var_0 [ var_9 ] >= var_4 - var_9 ) {
var_7 = var_9 ;
continue ;
}
break ;
}
return var_4 - var_7 ;
}
|
156 | <awangdev>561. Array Partition I_1.java | public int arrayPairSum(int[] nums) {
if (nums == null || nums.length <= 1) {
return 0;
}
Arrays.sort(nums);
long result = 0;
for (int i = 0; i < nums.length; i++) {
result += i % 2 == 0 ? nums[i] : 0;
}
return (int)result;
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length <= 1 ) {
return 0 ;
}
Arrays . sort ( var_0 ) ;
long var_7 = 0 ;
for ( int var_8 = 0 ;
var_8 < var_0 . length ;
var_8 ++ ) {
var_7 += var_8 % 2 == 0 ? var_0 [ var_8 ] : 0 ;
}
return ( int ) var_7 ;
}
|
157 | <awangdev>Group Shifted Strings_1.java | private String convert(String s) {
StringBuffer sb = new StringBuffer();
int offset = s.charAt(0) - 'a';
for (char c : s.toCharArray()) {
char newChar = (char)(c - offset);
if (newChar < 'a') newChar += 26;
sb.append(newChar);
}
return sb.toString();
} | private String var_0 ( String var_2 ) {
StringBuffer var_4 = new StringBuffer ( ) ;
int var_8 = var_2 . charAt ( 0 ) - 'a' ;
for ( char var_10 : var_2 . toCharArray ( ) ) {
char var_11 = ( char ) ( var_10 - var_8 ) ;
if ( var_11 < 'a' ) var_11 += 26 ;
var_4 . append ( var_11 ) ;
}
return var_4 . toString ( ) ;
}
|
158 | <awangdev>Power of Three_1.java | public boolean isPowerOfThree(int n) {
if (n <= 0) {
return false;
}
int start = 0;
int end = n;
while (start + 1 < end) {
int mid = start + (end - start)/2;
long powerOfThree = (long) Math.pow(3, mid);
if (powerOfThree == n) {
return true;
}
if (powerOfThree < n) {
start = mid;
} else {
end = mid;
}
}
return Math.pow(3, start) == n;
} | public boolean var_0 ( int var_1 ) {
if ( var_1 <= 0 ) {
return false ;
}
int var_3 = 0 ;
int var_4 = var_1 ;
while ( var_3 + 1 < var_4 ) {
int var_7 = var_3 + ( var_4 - var_3 ) / 2 ;
long var_11 = ( long ) Math . pow ( 3 , var_7 ) ;
if ( var_11 == var_1 ) {
return true ;
}
if ( var_11 < var_1 ) {
var_3 = var_7 ;
}
else {
var_4 = var_7 ;
}
}
return Math . pow ( 3 , var_3 ) == var_1 ;
}
|
159 | <awangdev>119. Pascal's Triangle II_1.java | public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<>();
if (rowIndex == 0) result.add(1);
if (rowIndex >= 1) result.add(1);
for (int row = 1; row <= rowIndex; row++) {
List<Integer> list = new ArrayList<>(Arrays.asList(1, 1));
List<Integer> lastRow = result;
int end = row - 1;
for (int i = 1; i <= end; i++) {
list.add(i, lastRow.get(i) + lastRow.get(i - 1));
}
result = list;
}
return result;
} | public List < Integer > var_1 ( int var_2 ) {
List < Integer > var_5 = new ArrayList < > ( ) ;
if ( var_2 == 0 ) var_5 . add ( 1 ) ;
if ( var_2 >= 1 ) var_5 . add ( 1 ) ;
for ( int var_9 = 1 ;
var_9 <= var_2 ;
var_9 ++ ) {
List < Integer > var_12 = new ArrayList < > ( Arrays . asList ( 1 , 1 ) ) ;
List < Integer > var_13 = var_5 ;
int var_14 = var_9 - 1 ;
for ( int var_15 = 1 ;
var_15 <= var_14 ;
var_15 ++ ) {
var_12 . add ( var_15 , var_13 . get ( var_15 ) + var_13 . get ( var_15 - 1 ) ) ;
}
var_5 = var_12 ;
}
return var_5 ;
}
|
160 | <awangdev>169. Majority Element_1.java | public int majorityElement(int[] nums) {
if (nums == null || nums.length == 0) {
return -1;
}
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
if (!map.containsKey(num)) {
map.put(num, 0);
}
map.put(num, map.get(num) + 1);
}
int halfLen = nums.length / 2;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > halfLen) {
return entry.getKey();
}
}
return -1;
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length == 0 ) {
return - 1 ;
}
Map < Integer , Integer > var_8 = new HashMap < > ( ) ;
for ( int var_9 : var_0 ) {
if ( ! var_8 . containsKey ( var_9 ) ) {
var_8 . put ( var_9 , 0 ) ;
}
var_8 . put ( var_9 , var_8 . get ( var_9 ) + 1 ) ;
}
int var_14 = var_0 . length / 2 ;
for ( Map . Entry < Integer , Integer > var_16 : var_8 . entrySet ( ) ) {
if ( var_16 . getValue ( ) > var_14 ) {
return var_16 . getKey ( ) ;
}
}
return - 1 ;
}
|
161 | <awangdev>Rotate Image_1.java | public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int width = matrix.length;
for (int i = 0; i < width/2; i++) {
for (int j = 0; j < Math.ceil(width/2.0); j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[width - 1 - j][i];
matrix[width - 1 - j][i] = matrix[width - 1 - i][width - 1 - j];
matrix[width - 1 - i][width - 1 - j] = matrix[j][width - 1 - i];
matrix[j][width - 1 - i] = temp;
}
}
} | public void var_0 ( int [ ] [ ] var_1 ) {
if ( var_1 == null || var_1 . length == 0 || var_1 [ 0 ] . length == 0 ) {
return ;
}
int var_5 = var_1 . length ;
for ( int var_6 = 0 ;
var_6 < var_5 / 2 ;
var_6 ++ ) {
for ( int var_8 = 0 ;
var_8 < Math . ceil ( var_5 / 2.0 ) ;
var_8 ++ ) {
int var_12 = var_1 [ var_6 ] [ var_8 ] ;
var_1 [ var_6 ] [ var_8 ] = var_1 [ var_5 - 1 - var_8 ] [ var_6 ] ;
var_1 [ var_5 - 1 - var_8 ] [ var_6 ] = var_1 [ var_5 - 1 - var_6 ] [ var_5 - 1 - var_8 ] ;
var_1 [ var_5 - 1 - var_6 ] [ var_5 - 1 - var_8 ] = var_1 [ var_8 ] [ var_5 - 1 - var_6 ] ;
var_1 [ var_8 ] [ var_5 - 1 - var_6 ] = var_12 ;
}
}
}
|
162 | <awangdev>305. Number of Islands II_1.java | private boolean validateBorder(int[][] grid, int x, int y, int m, int n) {
return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1;
} | private boolean var_0 ( int [ ] [ ] var_1 , int var_2 , int var_3 , int var_4 , int var_5 ) {
return var_2 >= 0 && var_2 < var_4 && var_3 >= 0 && var_3 < var_5 && var_1 [ var_2 ] [ var_3 ] == 1 ;
}
|
163 | <awangdev>Backpack VI_1.java | public int backPackVI(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return 0;
}
int n = nums.length;
int[] dp = new int[target + 1];
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int j = 0; j < n; j++) {
if (i - nums[j] >= 0) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
} | public int sample_funcname ( int [ ] var_0 , int var_1 ) {
if ( var_0 == null || var_0 . length == 0 ) {
return 0 ;
}
int var_5 = var_0 . length ;
int [ ] var_7 = new int [ var_1 + 1 ] ;
var_7 [ 0 ] = 1 ;
for ( int var_8 = 1 ;
var_8 <= var_1 ;
var_8 ++ ) {
for ( int var_9 = 0 ;
var_9 < var_5 ;
var_9 ++ ) {
if ( var_8 - var_0 [ var_9 ] >= 0 ) {
var_7 [ var_8 ] += var_7 [ var_8 - var_0 [ var_9 ] ] ;
}
}
}
return var_7 [ var_1 ] ;
}
|
164 | <awangdev>Letter Combinations of a Phone Number_3.java | public List<String> letterCombinations(String digits) {
List<String> rst = new ArrayList<String>();
if (digits == null || digits.length() == 0) {
return rst;
}
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(2, "abc");map.put(3, "def");
map.put(4, "ghi");map.put(5, "jkl");map.put(6, "mno");
map.put(7, "pqrs");map.put(8,"tuv");map.put(9,"wxyz");
Queue<String> queue = new LinkedList<String>();
int index = 0;
int digit = Integer.parseInt(digits.substring(index, index + 1));
String keys = map.get(digit);
index++;
for (int i = 0; i < keys.length(); i++) {
queue.offer(keys.substring(i,i+1));
}
int size = queue.size();
while (index < digits.length() && !queue.isEmpty()) {
String str = queue.poll();
digit = Integer.parseInt(digits.substring(index, index + 1));
keys = map.get(digit);
for (int i = 0; i < keys.length(); i++) {
queue.offer(str + keys.substring(i,i+1));
}
size--;
if (size == 0 && index < digits.length() - 1) {
index++;
size = queue.size();
}
}
while (!queue.isEmpty()) {
rst.add(queue.poll());
}
return rst;
} | public List < String > var_1 ( String var_2 ) {
List < String > var_5 = new ArrayList < String > ( ) ;
if ( var_2 == null || var_2 . length ( ) == 0 ) {
return var_5 ;
}
HashMap < Integer , String > var_11 = new HashMap < Integer , String > ( ) ;
var_11 . put ( 2 , "abc" ) ;
var_11 . put ( 3 , "def" ) ;
var_11 . put ( 4 , "ghi" ) ;
var_11 . put ( 5 , "jkl" ) ;
var_11 . put ( 6 , "mno" ) ;
var_11 . put ( 7 , "pqrs" ) ;
var_11 . put ( 8 , "tuv" ) ;
var_11 . put ( 9 , "wxyz" ) ;
Queue < String > var_31 = new LinkedList < String > ( ) ;
int var_32 = 0 ;
int var_36 = Integer . parseInt ( var_2 . substring ( var_32 , var_32 + 1 ) ) ;
String var_38 = var_11 . get ( var_36 ) ;
var_32 ++ ;
for ( int var_39 = 0 ;
var_39 < var_38 . length ( ) ;
var_39 ++ ) {
var_31 . offer ( var_38 . substring ( var_39 , var_39 + 1 ) ) ;
}
int size = var_31 . size ( ) ;
while ( var_32 < var_2 . length ( ) && ! var_31 . isEmpty ( ) ) {
String var_44 = var_31 . poll ( ) ;
var_36 = Integer . parseInt ( var_2 . substring ( var_32 , var_32 + 1 ) ) ;
var_38 = var_11 . get ( var_36 ) ;
for ( int var_39 = 0 ;
var_39 < var_38 . length ( ) ;
var_39 ++ ) {
var_31 . offer ( var_44 + var_38 . substring ( var_39 , var_39 + 1 ) ) ;
}
size -- ;
if ( size == 0 && var_32 < var_2 . length ( ) - 1 ) {
var_32 ++ ;
size = var_31 . size ( ) ;
}
}
while ( ! var_31 . isEmpty ( ) ) {
var_5 . add ( var_31 . poll ( ) ) ;
}
return var_5 ;
}
|
165 | <awangdev>Shuffle an Array_2.java | private void swap(int[] nums, int x, int y) {
int temp = nums[y];
nums[y] = nums[x];
nums[x] = temp;
} | private void var_0 ( int [ ] var_1 , int var_2 , int var_3 ) {
int var_4 = var_1 [ var_3 ] ;
var_1 [ var_3 ] = var_1 [ var_2 ] ;
var_1 [ var_2 ] = var_4 ;
}
|
166 | <awangdev>243. Shortest Word Distance_1.java | public int shortestDistance(String[] words, String word1, String word2) {
Integer p1 = null, p2 = null;
int min = Integer.MAX_VALUE;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) p1 = i;
if (words[i].equals(word2)) p2 = i;
if (p1 != null && p2 != null) min = Math.min(min, Math.abs(p1 - p2));
}
return min;
} | public int sample_funcname ( String [ ] var_1 , String var_2 , String var_3 ) {
Integer var_6 = null , var_7 = null ;
int min = Integer . MAX_VALUE ;
for ( int var_11 = 0 ;
var_11 < var_1 . length ;
var_11 ++ ) {
if ( var_1 [ var_11 ] . equals ( var_2 ) ) var_6 = var_11 ;
if ( var_1 [ var_11 ] . equals ( var_3 ) ) var_7 = var_11 ;
if ( var_6 != null && var_7 != null ) min = Math . min ( min , Math . abs ( var_6 - var_7 ) ) ;
}
return min ;
}
|
167 | <awangdev>15. 3Sum_1.java | public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
int n = nums.length;
for (int i = 0; i < n - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
int lo = i + 1, hi = n - 1;
while (lo < hi) {
int sum = nums[i] + nums[lo] + nums[hi];
if (sum == 0) {
result.add(Arrays.asList(nums[i], nums[lo], nums[hi]));
while (lo < hi && nums[lo] == nums[lo + 1]) lo++;
while (lo < hi && nums[hi] == nums[hi - 1]) hi--;
lo++;
hi--;
} else if (sum < 0) {
lo++;
} else {
hi--;
}
}
}
return result;
} | public List < List < Integer >> var_2 ( int [ ] var_3 ) {
List < List < Integer >> var_5 = new ArrayList < > ( ) ;
Arrays . sort ( var_3 ) ;
int var_9 = var_3 . length ;
for ( int var_11 = 0 ;
var_11 < var_9 - 2 ;
var_11 ++ ) {
if ( var_11 > 0 && var_3 [ var_11 ] == var_3 [ var_11 - 1 ] ) continue ;
int var_14 = var_11 + 1 , var_15 = var_9 - 1 ;
while ( var_14 < var_15 ) {
int var_16 = var_3 [ var_11 ] + var_3 [ var_14 ] + var_3 [ var_15 ] ;
if ( var_16 == 0 ) {
var_5 . add ( Arrays . asList ( var_3 [ var_11 ] , var_3 [ var_14 ] , var_3 [ var_15 ] ) ) ;
while ( var_14 < var_15 && var_3 [ var_14 ] == var_3 [ var_14 + 1 ] ) var_14 ++ ;
while ( var_14 < var_15 && var_3 [ var_15 ] == var_3 [ var_15 - 1 ] ) var_15 -- ;
var_14 ++ ;
var_15 -- ;
}
else if ( var_16 < 0 ) {
var_14 ++ ;
}
else {
var_15 -- ;
}
}
}
return var_5 ;
}
|
168 | <awangdev>43. Multiply Strings_2.java | private int toInt(String s, int x) {
return s.charAt(x) - '0';
} | private int sample_funcname ( String var_1 , int var_2 ) {
return var_1 . charAt ( var_2 ) - '0' ;
}
|
169 | <awangdev>496. Next Greater Element I_2.java | private int check(int[] nums, int index, int val) {
for (int i = index; i < nums.length; i++) {
if (nums[i] > val) return nums[i];
}
return -1;
} | private int sample_funcname ( int [ ] var_0 , int var_1 , int var_2 ) {
for ( int var_3 = var_1 ;
var_3 < var_0 . length ;
var_3 ++ ) {
if ( var_0 [ var_3 ] > var_2 ) return var_0 [ var_3 ] ;
}
return - 1 ;
}
|
170 | <awangdev>Reverse String_1.java | public void reverseString(char[] s) {
if (s == null || s.length <= 1) {
return;
}
int n = s.length;
for (int i = 0; i < n / 2; i++) {
char temp = s[i];
s[i] = s[n - i - 1];
s[n - i - 1] = temp;
}
} | public void var_0 ( char [ ] var_1 ) {
if ( var_1 == null || var_1 . length <= 1 ) {
return ;
}
int var_5 = var_1 . length ;
for ( int var_7 = 0 ;
var_7 < var_5 / 2 ;
var_7 ++ ) {
char var_9 = var_1 [ var_7 ] ;
var_1 [ var_7 ] = var_1 [ var_5 - var_7 - 1 ] ;
var_1 [ var_5 - var_7 - 1 ] = var_9 ;
}
}
|
171 | <awangdev>Matrix Zigzag Traversal_1.java | public int[] printZMatrix(int[][] matrix) {
int[] rst = null;
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return rst;
}
int n = matrix.length;
int m = matrix[0].length;
rst = new int[n * m];
if (matrix.length == 1) {
return matrix[0];
}
int i = 0, j = 0;
int ind = 0;
rst[ind] = matrix[i][j];
ind++;
while (ind < rst.length) {
if (j + 1 < m || i + 1 < n) {
if (j + 1 < m) j++;
else if (i + 1 < n) i++;
rst[ind++] = matrix[i][j];
}
while (j - 1 >= 0 && i + 1 < n) {
rst[ind++] = matrix[++i][--j];
}
if (j + 1 < m || i + 1 < n) {
if (i + 1 < n) i++;
else if (j + 1 < m) j++;
rst[ind++] = matrix[i][j];
}
while (j + 1 < m && i - 1 >= 0) {
rst[ind++] = matrix[--i][++j];
}
}
return rst;
} | public int [ ] sample_funcname ( int [ ] [ ] var_0 ) {
int [ ] var_2 = null ;
if ( var_0 == null || var_0 . length == 0 || var_0 [ 0 ] == null || var_0 [ 0 ] . length == 0 ) {
return var_2 ;
}
int var_5 = var_0 . length ;
int var_6 = var_0 [ 0 ] . length ;
var_2 = new int [ var_5 * var_6 ] ;
if ( var_0 . length == 1 ) {
return var_0 [ 0 ] ;
}
int var_8 = 0 , var_9 = 0 ;
int var_10 = 0 ;
var_2 [ var_10 ] = var_0 [ var_8 ] [ var_9 ] ;
var_10 ++ ;
while ( var_10 < var_2 . length ) {
if ( var_9 + 1 < var_6 || var_8 + 1 < var_5 ) {
if ( var_9 + 1 < var_6 ) var_9 ++ ;
else if ( var_8 + 1 < var_5 ) var_8 ++ ;
var_2 [ var_10 ++ ] = var_0 [ var_8 ] [ var_9 ] ;
}
while ( var_9 - 1 >= 0 && var_8 + 1 < var_5 ) {
var_2 [ var_10 ++ ] = var_0 [ ++ var_8 ] [ -- var_9 ] ;
}
if ( var_9 + 1 < var_6 || var_8 + 1 < var_5 ) {
if ( var_8 + 1 < var_5 ) var_8 ++ ;
else if ( var_9 + 1 < var_6 ) var_9 ++ ;
var_2 [ var_10 ++ ] = var_0 [ var_8 ] [ var_9 ] ;
}
while ( var_9 + 1 < var_6 && var_8 - 1 >= 0 ) {
var_2 [ var_10 ++ ] = var_0 [ -- var_8 ] [ ++ var_9 ] ;
}
}
return var_2 ;
}
|
172 | <awangdev>1213. Intersection of Three Sorted Arrays_3.java | public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
List<Integer> rst = new LinkedList<>();
int i = 0, j = 0, k = 0;
while (i < arr1.length && j < arr2.length && k < arr3.length) {
if (arr1[i] == arr2[j] && arr2[j] == arr3[k]) {
if (rst.isEmpty() || arr1[i] != rst.get(rst.size() - 1)) rst.add(arr1[i]);
i++;
j++;
k++;
} else if (arr1[i] < arr2[j]) i++;
else if (arr2[j] < arr3[k]) j++;
else k++;
}
return rst;
} | public List < Integer > var_1 ( int [ ] var_2 , int [ ] var_3 , int [ ] var_4 ) {
List < Integer > var_7 = new LinkedList < > ( ) ;
int var_9 = 0 , var_10 = 0 , var_11 = 0 ;
while ( var_9 < var_2 . length && var_10 < var_3 . length && var_11 < var_4 . length ) {
if ( var_2 [ var_9 ] == var_3 [ var_10 ] && var_3 [ var_10 ] == var_4 [ var_11 ] ) {
if ( var_7 . isEmpty ( ) || var_2 [ var_9 ] != var_7 . get ( var_7 . size ( ) - 1 ) ) var_7 . add ( var_2 [ var_9 ] ) ;
var_9 ++ ;
var_10 ++ ;
var_11 ++ ;
}
else if ( var_2 [ var_9 ] < var_3 [ var_10 ] ) var_9 ++ ;
else if ( var_3 [ var_10 ] < var_4 [ var_11 ] ) var_10 ++ ;
else var_11 ++ ;
}
return var_7 ;
}
|
173 | <awangdev>219. Contains Duplicate II_1.java | public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null || nums.length == 0 || k <= 0) {
return false;
}
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (i > k) {
set.remove(nums[i - k - 1]);
}
if (!set.add(nums[i])) {
return true;
}
}
return false;
} | public boolean var_0 ( int [ ] var_1 , int var_2 ) {
if ( var_1 == null || var_1 . length == 0 || var_2 <= 0 ) {
return false ;
}
Set < Integer > var_9 = new HashSet < > ( ) ;
for ( int var_10 = 0 ;
var_10 < var_1 . length ;
var_10 ++ ) {
if ( var_10 > var_2 ) {
var_9 . remove ( var_1 [ var_10 - var_2 - 1 ] ) ;
}
if ( ! var_9 . add ( var_1 [ var_10 ] ) ) {
return true ;
}
}
return false ;
}
|
174 | <awangdev>67. Add Binary_1.java | public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int i = a.length() - 1, j = b.length() -1, carry = 0;
while (i >= 0 || j >= 0) {
int sum = carry;
if (i >= 0) sum += a.charAt(i--) - '0';
if (j >= 0) sum += b.charAt(j--) - '0';
sb.insert(0, sum % 2);
carry = sum / 2;
}
if (carry != 0) sb.insert(0, carry);
return sb.toString();
} | public String var_0 ( String var_2 , String var_3 ) {
StringBuilder var_5 = new StringBuilder ( ) ;
int var_8 = var_2 . length ( ) - 1 , var_9 = var_3 . length ( ) - 1 , var_11 = 0 ;
while ( var_8 >= 0 || var_9 >= 0 ) {
int var_12 = var_11 ;
if ( var_8 >= 0 ) var_12 += var_2 . charAt ( var_8 -- ) - '0' ;
if ( var_9 >= 0 ) var_12 += var_3 . charAt ( var_9 -- ) - '0' ;
var_5 . insert ( 0 , var_12 % 2 ) ;
var_11 = var_12 / 2 ;
}
if ( var_11 != 0 ) var_5 . insert ( 0 , var_11 ) ;
return var_5 . toString ( ) ;
}
|
175 | <awangdev>70. Climbing Stairs_1.java | public int climbStairs(int n) {
if (n <= 1) {
return 1;
}
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
} | public int sample_funcname ( int var_0 ) {
if ( var_0 <= 1 ) {
return 1 ;
}
int [ ] var_2 = new int [ var_0 + 1 ] ;
var_2 [ 0 ] = 1 ;
var_2 [ 1 ] = 1 ;
for ( int var_5 = 2 ;
var_5 <= var_0 ;
var_5 ++ ) {
var_2 [ var_5 ] = var_2 [ var_5 - 1 ] + var_2 [ var_5 - 2 ] ;
}
return var_2 [ var_0 ] ;
}
|
176 | <awangdev>Construct Binary Tree from Inorder and Postorder Traversal_1.java | public int findMid(int[] arr, int start, int end, int key) {
for (int i = start; i <= end; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
} | public int sample_funcname ( int [ ] var_0 , int var_1 , int var_2 , int var_3 ) {
for ( int var_4 = var_1 ;
var_4 <= var_2 ;
var_4 ++ ) {
if ( var_0 [ var_4 ] == var_3 ) {
return var_4 ;
}
}
return - 1 ;
}
|
177 | <awangdev>[lint]. Compare Strings_1.java | public boolean compareStrings(String A, String B) {
if (A == null || B == null || A.length() < B.length()) {
return false;
}
int[] count = new int[26];
for (char c : A.toCharArray()) {
count[c - 'A']++;
}
for (char c : B.toCharArray()) {
count[c - 'A']--;
if (count[c - 'A'] < 0) {
return false;
}
}
return true;
} | public boolean var_0 ( String var_2 , String var_3 ) {
if ( var_2 == null || var_3 == null || var_2 . length ( ) < var_3 . length ( ) ) {
return false ;
}
int [ ] var_7 = new int [ 26 ] ;
for ( char var_9 : var_2 . toCharArray ( ) ) {
var_7 [ var_9 - 'A' ] ++ ;
}
for ( char var_9 : var_3 . toCharArray ( ) ) {
var_7 [ var_9 - 'A' ] -- ;
if ( var_7 [ var_9 - 'A' ] < 0 ) {
return false ;
}
}
return true ;
}
|
178 | <awangdev>42. Trapping Rain Water_3.java | public int trap(int[] heights) {
if (heights == null || heights.length == 0) return 0;
int maxIndex = 0;
for (int i = 0; i < heights.length; i++) {
if (heights[i] > heights[maxIndex]) maxIndex = i;
}
int sum = 0, prev = 0;
for (int i = 0; i < maxIndex; i++) {
if (heights[i] > prev) {
sum += (maxIndex - i) * (heights[i] - prev);
prev = heights[i];
}
sum -= heights[i];
}
prev = 0;
for (int i = heights.length - 1; i > maxIndex; i--) {
if (heights[i] > prev) {
sum += (i - maxIndex) * (heights[i] - prev);
prev = heights[i];
}
sum -= heights[i];
}
return sum;
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length == 0 ) return 0 ;
int var_4 = 0 ;
for ( int var_5 = 0 ;
var_5 < var_0 . length ;
var_5 ++ ) {
if ( var_0 [ var_5 ] > var_0 [ var_4 ] ) var_4 = var_5 ;
}
int var_6 = 0 , var_7 = 0 ;
for ( int var_5 = 0 ;
var_5 < var_4 ;
var_5 ++ ) {
if ( var_0 [ var_5 ] > var_7 ) {
var_6 += ( var_4 - var_5 ) * ( var_0 [ var_5 ] - var_7 ) ;
var_7 = var_0 [ var_5 ] ;
}
var_6 -= var_0 [ var_5 ] ;
}
var_7 = 0 ;
for ( int var_5 = var_0 . length - 1 ;
var_5 > var_4 ;
var_5 -- ) {
if ( var_0 [ var_5 ] > var_7 ) {
var_6 += ( var_5 - var_4 ) * ( var_0 [ var_5 ] - var_7 ) ;
var_7 = var_0 [ var_5 ] ;
}
var_6 -= var_0 [ var_5 ] ;
}
return var_6 ;
}
|
179 | <awangdev>202. Happy Number_1.java | private long process(long n) {
long sum = 0;
while (n != 0) {
long mod = n % 10;
sum += mod * mod;
n /= 10;
}
return sum;
} | private long sample_funcname ( long var_0 ) {
long var_2 = 0 ;
while ( var_0 != 0 ) {
long var_4 = var_0 % 10 ;
var_2 += var_4 * var_4 ;
var_0 /= 10 ;
}
return var_2 ;
}
|
180 | <awangdev>Subsets II_1.java | private void dfs(List<List<Integer>> result, List<Integer> list, int[] nums, int depth) {
for (int i = depth; i < nums.length; i++) {
if (i > depth && nums[i] == nums[i - 1]) continue;
list.add(nums[i]);
result.add(new ArrayList<>(list));
dfs(result, list, nums, i + 1);
list.remove(list.size() - 1);
}
} | private void sample_funcname ( List < List < Integer >> result , List < Integer > list , int [ ] nums , int depth ) {
for ( int i = depth ;
i < nums . length ;
i ++ ) {
if ( i > depth && nums [ i ] == nums [ i - 1 ] ) continue ;
list . add ( nums [ i ] ) ;
result . add ( new ArrayList < > ( list ) ) ;
dfs ( result , list , nums , i + 1 ) ;
list . remove ( list . size ( ) - 1 ) ;
}
}
|
181 | <awangdev>String Permutation_1.java | public boolean Permutation(String A, String B) {
if (A == null || B == null || A.length() != B.length()) {
return false;
}
final Map<Character, Integer> strMap = new HashMap<>();
for (int i = 0; i < A.length(); i++) {
final char charA = A.charAt(i);
final char charB = B.charAt(i);
if (!strMap.containsKey(charA)) {
strMap.put(charA, 0);
}
strMap.put(charA, strMap.get(charA) + 1);
if (!strMap.containsKey(charB)) {
strMap.put(charB, 0);
}
strMap.put(charB, strMap.get(charB) - 1);
}
for (Map.Entry<Character, Integer> entry : strMap.entrySet()) {
if (entry.getValue() != 0) {
return false;
}
}
return true;
} | public boolean var_0 ( String var_2 , String var_3 ) {
if ( var_2 == null || var_3 == null || var_2 . length ( ) != var_3 . length ( ) ) {
return false ;
}
final Map < Character , Integer > var_10 = new HashMap < > ( ) ;
for ( int var_12 = 0 ;
var_12 < var_2 . length ( ) ;
var_12 ++ ) {
final char var_14 = var_2 . charAt ( var_12 ) ;
final char var_15 = var_3 . charAt ( var_12 ) ;
if ( ! var_10 . containsKey ( var_14 ) ) {
var_10 . put ( var_14 , 0 ) ;
}
var_10 . put ( var_14 , var_10 . get ( var_14 ) + 1 ) ;
if ( ! var_10 . containsKey ( var_15 ) ) {
var_10 . put ( var_15 , 0 ) ;
}
var_10 . put ( var_15 , var_10 . get ( var_15 ) - 1 ) ;
}
for ( Map . Entry < Character , Integer > var_21 : var_10 . entrySet ( ) ) {
if ( var_21 . getValue ( ) != 0 ) {
return false ;
}
}
return true ;
}
|
182 | <awangdev>416. Partition Equal Subset Sum_1.java | public boolean canPartition(int[] nums) {
if (nums == null || nums.length < 2) return false;
int sum = 0;
for (int num : nums) sum += num;
if (sum % 2 != 0) return false;
sum /= 2;
boolean[] dp = new boolean[sum + 1];
dp[0] = true;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
for (int j = sum; j >= num; j--) {
dp[j] = dp[j] || dp[j - num];
}
}
return dp[sum];
} | public boolean var_0 ( int [ ] var_1 ) {
if ( var_1 == null || var_1 . length < 2 ) return false ;
int var_6 = 0 ;
for ( int var_7 : var_1 ) var_6 += var_7 ;
if ( var_6 % 2 != 0 ) return false ;
var_6 /= 2 ;
boolean [ ] var_10 = new boolean [ var_6 + 1 ] ;
var_10 [ 0 ] = true ;
for ( int var_11 = 0 ;
var_11 < var_1 . length ;
var_11 ++ ) {
int var_7 = var_1 [ var_11 ] ;
for ( int var_12 = var_6 ;
var_12 >= var_7 ;
var_12 -- ) {
var_10 [ var_12 ] = var_10 [ var_12 ] || var_10 [ var_12 - var_7 ] ;
}
}
return var_10 [ var_6 ] ;
}
|
183 | <awangdev>26.Remove Duplicates from Sorted Array_2.java | public int removeDuplicates(int[] nums) {
if (nums == null) {
return 0;
}
if (nums.length <= 1) {
return nums.length;
}
int currPos = 0;
int movingPos = 1;
while (movingPos < nums.length) {
while(movingPos < nums.length && nums[currPos] == nums[movingPos]) {
movingPos++;
}
if (movingPos < nums.length && nums[currPos] != nums[movingPos]) {
nums[currPos + 1] = nums[movingPos];
currPos++;
movingPos++;
}
}
return currPos + 1;
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null ) {
return 0 ;
}
if ( var_0 . length <= 1 ) {
return var_0 . length ;
}
int var_5 = 0 ;
int var_6 = 1 ;
while ( var_6 < var_0 . length ) {
while ( var_6 < var_0 . length && var_0 [ var_5 ] == var_0 [ var_6 ] ) {
var_6 ++ ;
}
if ( var_6 < var_0 . length && var_0 [ var_5 ] != var_0 [ var_6 ] ) {
var_0 [ var_5 + 1 ] = var_0 [ var_6 ] ;
var_5 ++ ;
var_6 ++ ;
}
}
return var_5 + 1 ;
}
|
184 | <awangdev>340. Longest Substring with At Most K Distinct Characters_2.java | public int lengthOfLongestSubstringKDistinct(String s, int k) {
if (s == null || s.length() == 0) return 0;
int n = s.length();
Map<Character, Integer> lastOccurMap = new HashMap<>();
int left = 0, right = 0, max = 0;
while (right < n) {
if (lastOccurMap.size() <= k) {
lastOccurMap.put(s.charAt(right), right++);
}
if (lastOccurMap.size() > k) {
int leftMost = right;
for (int index : lastOccurMap.values()) {
leftMost = Math.min(leftMost, index);
}
lastOccurMap.remove(s.charAt(leftMost));
left = leftMost + 1;
}
max = Math.max(max, right - left);
}
return max;
} | public int sample_funcname ( String var_1 , int var_2 ) {
if ( var_1 == null || var_1 . length ( ) == 0 ) return 0 ;
int var_6 = var_1 . length ( ) ;
Map < Character , Integer > var_11 = new HashMap < > ( ) ;
int var_12 = 0 , var_13 = 0 , max = 0 ;
while ( var_13 < var_6 ) {
if ( var_11 . size ( ) <= var_2 ) {
var_11 . put ( var_1 . charAt ( var_13 ) , var_13 ++ ) ;
}
if ( var_11 . size ( ) > var_2 ) {
int var_18 = var_13 ;
for ( int var_20 : var_11 . values ( ) ) {
var_18 = Math . min ( var_18 , var_20 ) ;
}
var_11 . remove ( var_1 . charAt ( var_18 ) ) ;
var_12 = var_18 + 1 ;
}
max = Math . max ( max , var_13 - var_12 ) ;
}
return max ;
}
|
185 | <awangdev>Longest Substring Without Repeating Characters_4.java | public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int count = 0;
int max = 0;
for (int i = 0; i < s.length(); i++) {
if (!map.containsKey(s.charAt(i))) {
map.put(s.charAt(i), i);
count++;
} else {
i = map.get(s.charAt(i));
map = new HashMap<Character, Integer>();
count = 0;
}
max = Math.max(max, count);
}
return max;
} | public int sample_funcname ( String var_1 ) {
if ( var_1 == null || var_1 . length ( ) == 0 ) {
return 0 ;
}
HashMap < Character , Integer > var_8 = new HashMap < Character , Integer > ( ) ;
int var_9 = 0 ;
int max = 0 ;
for ( int var_11 = 0 ;
var_11 < var_1 . length ( ) ;
var_11 ++ ) {
if ( ! var_8 . containsKey ( var_1 . charAt ( var_11 ) ) ) {
var_8 . put ( var_1 . charAt ( var_11 ) , var_11 ) ;
var_9 ++ ;
}
else {
var_11 = var_8 . get ( var_1 . charAt ( var_11 ) ) ;
var_8 = new HashMap < Character , Integer > ( ) ;
var_9 = 0 ;
}
max = Math . max ( max , var_9 ) ;
}
return max ;
}
|
186 | <awangdev>Missing Ranges_1.java | public String printRange(int from, int to) {
return (from == to) ? String.valueOf(from) : from + "->" + to;
} | public String var_0 ( int var_1 , int var_2 ) {
return ( var_1 == var_2 ) ? String . valueOf ( var_1 ) : var_1 + "->" + var_2 ;
}
|
187 | <awangdev>741. Cherry Pickup_1.java | private String getKey(int i, int j) {
return i + "@" + j;
} | private String var_0 ( int var_1 , int var_2 ) {
return var_1 + "@" + var_2 ;
}
|
188 | <awangdev>Closest Number in Sorted Array_1.java | public int closestNumber(int[] A, int target) {
if (A == null || A.length == 0) {
return -1;
}
int start = 0;
int end = A.length - 1;
int mid;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (A[mid] == target) {
return mid;
} else if (mid - 1 >= 0 && A[mid - 1] <= target && target < A[mid]) {
return (target - A[mid - 1]) < (A[mid] - target) ? (mid - 1) : mid;
} else if (mid + 1 < A.length && A[mid] < target && target <= A[mid + 1]) {
return (target - A[mid]) < (A[mid + 1] - target) ? mid : mid + 1;
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
return (target - A[start]) < (A[end] - target) ? start : end;
} | public int sample_funcname ( int [ ] var_0 , int var_1 ) {
if ( var_0 == null || var_0 . length == 0 ) {
return - 1 ;
}
int var_6 = 0 ;
int var_7 = var_0 . length - 1 ;
int var_8 ;
while ( var_6 + 1 < var_7 ) {
var_8 = var_6 + ( var_7 - var_6 ) / 2 ;
if ( var_0 [ var_8 ] == var_1 ) {
return var_8 ;
}
else if ( var_8 - 1 >= 0 && var_0 [ var_8 - 1 ] <= var_1 && var_1 < var_0 [ var_8 ] ) {
return ( var_1 - var_0 [ var_8 - 1 ] ) < ( var_0 [ var_8 ] - var_1 ) ? ( var_8 - 1 ) : var_8 ;
}
else if ( var_8 + 1 < var_0 . length && var_0 [ var_8 ] < var_1 && var_1 <= var_0 [ var_8 + 1 ] ) {
return ( var_1 - var_0 [ var_8 ] ) < ( var_0 [ var_8 + 1 ] - var_1 ) ? var_8 : var_8 + 1 ;
}
else if ( var_0 [ var_8 ] < var_1 ) {
var_6 = var_8 ;
}
else {
var_7 = var_8 ;
}
}
return ( var_1 - var_0 [ var_6 ] ) < ( var_0 [ var_7 ] - var_1 ) ? var_6 : var_7 ;
}
|
189 | <awangdev>218. The Skyline Problem_1.java | private boolean isInvalid(int[][] buildings) {
return buildings == null || buildings.length == 0 || buildings[0] == null || buildings[0].length == 0;
} | private boolean var_0 ( int [ ] [ ] var_1 ) {
return var_1 == null || var_1 . length == 0 || var_1 [ 0 ] == null || var_1 [ 0 ] . length == 0 ;
}
|
190 | <awangdev>Sort Letters by Case_1.java | private void swap(char[] chars, int x, int y) {
char temp = chars[x];
chars[x] = chars[y];
chars[y] = temp;
} | private void var_0 ( char [ ] var_1 , int var_2 , int var_3 ) {
char var_4 = var_1 [ var_2 ] ;
var_1 [ var_2 ] = var_1 [ var_3 ] ;
var_1 [ var_3 ] = var_4 ;
}
|
191 | <awangdev>69. Sqrt(x)_1.java | public int mySqrt(int x) {
long start = 0;
long end = x;
while (start + 1 < end) {
long mid = (start + end) >> 1;
if (mid * mid < x) {
start = mid;
} else if (mid * mid > x) {
end = mid;
} else {
return (int)mid;
}
}
if (end * end <= x) {
return (int)end;
} else {
return (int)start;
}
} | public int sample_funcname ( int var_0 ) {
long var_2 = 0 ;
long var_3 = var_0 ;
while ( var_2 + 1 < var_3 ) {
long var_5 = ( var_2 + var_3 ) >> 1 ;
if ( var_5 * var_5 < var_0 ) {
var_2 = var_5 ;
}
else if ( var_5 * var_5 > var_0 ) {
var_3 = var_5 ;
}
else {
return ( int ) var_5 ;
}
}
if ( var_3 * var_3 <= var_0 ) {
return ( int ) var_3 ;
}
else {
return ( int ) var_2 ;
}
}
|
192 | <awangdev>Partition Array_1.java | public void swap(int[] nums, int x, int y){
int temp = nums[x];
nums[x] = nums[y];
nums[y] = temp;
} | public void var_0 ( int [ ] var_1 , int var_2 , int var_3 ) {
int var_4 = var_1 [ var_2 ] ;
var_1 [ var_2 ] = var_1 [ var_3 ] ;
var_1 [ var_3 ] = var_4 ;
}
|
193 | <awangdev>72. Edit Distance_6.java | public int minDistance(String word1, String word2) {
if (word1 == null && word2 != null) {
return word2.length();
} else if (word1 != null && word2 == null) {
return word1.length();
} else if (word1 == null && word2 == null) {
return 0;
}
int[][] DP = new int[word1.length() + 1][word2.length() + 1];
for (int i = 1; i <= word1.length(); i++) {
DP[i][0] = i;
}
for (int j = 1; j <= word2.length(); j++) {
DP[0][j] = j;
}
for (int i = 1; i <= word1.length(); i++) {
for (int j = 1; j <= word2.length(); j++) {
DP[i][j] = Math.min(Math.min(DP[i - 1][j] + 1, DP[i][j - 1] + 1), word1.charAt(i - 1) == word2.charAt(j - 1) ? DP[i - 1][j - 1] : DP[i - 1][j - 1] + 1);
}
}
return DP[word1.length()][word2.length()];
} | public int sample_funcname ( String var_1 , String var_2 ) {
if ( var_1 == null && var_2 != null ) {
return var_2 . length ( ) ;
}
else if ( var_1 != null && var_2 == null ) {
return var_1 . length ( ) ;
}
else if ( var_1 == null && var_2 == null ) {
return 0 ;
}
int [ ] [ ] var_7 = new int [ var_1 . length ( ) + 1 ] [ var_2 . length ( ) + 1 ] ;
for ( int var_8 = 1 ;
var_8 <= var_1 . length ( ) ;
var_8 ++ ) {
var_7 [ var_8 ] [ 0 ] = var_8 ;
}
for ( int var_9 = 1 ;
var_9 <= var_2 . length ( ) ;
var_9 ++ ) {
var_7 [ 0 ] [ var_9 ] = var_9 ;
}
for ( int var_8 = 1 ;
var_8 <= var_1 . length ( ) ;
var_8 ++ ) {
for ( int var_9 = 1 ;
var_9 <= var_2 . length ( ) ;
var_9 ++ ) {
var_7 [ var_8 ] [ var_9 ] = Math . min ( Math . min ( var_7 [ var_8 - 1 ] [ var_9 ] + 1 , var_7 [ var_8 ] [ var_9 - 1 ] + 1 ) , var_1 . charAt ( var_8 - 1 ) == var_2 . charAt ( var_9 - 1 ) ? var_7 [ var_8 - 1 ] [ var_9 - 1 ] : var_7 [ var_8 - 1 ] [ var_9 - 1 ] + 1 ) ;
}
}
return var_7 [ var_1 . length ( ) ] [ var_2 . length ( ) ] ;
}
|
194 | <awangdev>78. Subsets_2.java | private void dfs(List<List<Integer>> result, List<Integer> list, int[] nums, int depth) {
for (int i = depth; i < nums.length; i++) {
list.add(nums[i]);
result.add(new ArrayList<>(list));
dfs(result, list, nums, i + 1);
list.remove(list.size() - 1);
}
} | private void sample_funcname ( List < List < Integer >> result , List < Integer > list , int [ ] nums , int depth ) {
for ( int i = depth ;
i < nums . length ;
i ++ ) {
list . add ( nums [ i ] ) ;
result . add ( new ArrayList < > ( list ) ) ;
dfs ( result , list , nums , i + 1 ) ;
list . remove ( list . size ( ) - 1 ) ;
}
}
|
195 | <awangdev>House Robber II_2.java | public int rob(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
} else if (nums.length == 1) {
return nums[0];
}
int n = nums.length;
long[][] dp = new long[2][2];
dp[0][0] = 0;
dp[1][0] = 0;
dp[1][1] = nums[0];
for (int i = 2; i < n; i++) {
dp[i % 2][0] = Math.max(dp[(i - 1) % 2][0], dp[(i - 2) % 2][0] + nums[i - 1]);
dp[i % 2][1] = Math.max(dp[(i - 1) % 2][1], dp[(i - 2) % 2][1] + nums[i - 1]);
}
dp[n % 2][0] = Math.max(dp[(n - 1) % 2][0], dp[(n - 2) % 2][0] + nums[n - 1]);
dp[n % 2][1] = dp[(n - 1) % 2][1];
return (int) Math.max(dp[n % 2][0], dp[n % 2][1]);
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length == 0 ) {
return 0 ;
}
else if ( var_0 . length == 1 ) {
return var_0 [ 0 ] ;
}
int var_5 = var_0 . length ;
long [ ] [ ] var_7 = new long [ 2 ] [ 2 ] ;
var_7 [ 0 ] [ 0 ] = 0 ;
var_7 [ 1 ] [ 0 ] = 0 ;
var_7 [ 1 ] [ 1 ] = var_0 [ 0 ] ;
for ( int var_8 = 2 ;
var_8 < var_5 ;
var_8 ++ ) {
var_7 [ var_8 % 2 ] [ 0 ] = Math . max ( var_7 [ ( var_8 - 1 ) % 2 ] [ 0 ] , var_7 [ ( var_8 - 2 ) % 2 ] [ 0 ] + var_0 [ var_8 - 1 ] ) ;
var_7 [ var_8 % 2 ] [ 1 ] = Math . max ( var_7 [ ( var_8 - 1 ) % 2 ] [ 1 ] , var_7 [ ( var_8 - 2 ) % 2 ] [ 1 ] + var_0 [ var_8 - 1 ] ) ;
}
var_7 [ var_5 % 2 ] [ 0 ] = Math . max ( var_7 [ ( var_5 - 1 ) % 2 ] [ 0 ] , var_7 [ ( var_5 - 2 ) % 2 ] [ 0 ] + var_0 [ var_5 - 1 ] ) ;
var_7 [ var_5 % 2 ] [ 1 ] = var_7 [ ( var_5 - 1 ) % 2 ] [ 1 ] ;
return ( int ) Math . max ( var_7 [ var_5 % 2 ] [ 0 ] , var_7 [ var_5 % 2 ] [ 1 ] ) ;
}
|
196 | <awangdev>Ones and Zeroes_3.java | public int findMaxForm(String[] strs, int m, int n) {
if (strs == null || strs.length == 0 || (m == 0 && n == 0)) {
return 0;
}
int len = strs.length;
int[][][] dp = new int[m + 1][n + 1][len + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= len; k++) {
if (k == 0 || (i == 0 && j == 0)) {
dp[i][j][k] = 0;
continue;
}
char[] ss = strs[k - 1].toCharArray();
int countOne = 0;
int countZero = 0;
for (char c : ss) {
countOne += c == '1' ? 1 : 0;
countZero += c == '0' ? 1 : 0;
}
dp[i][j][k] = dp[i][j][k - 1];
if (i >= countZero && j >= countOne) {
dp[i][j][k] = Math.max(dp[i][j][k], dp[i - countZero][j - countOne][k - 1] + 1);
}
}
}
}
return dp[m][n][len];
} | public int sample_funcname ( String [ ] var_1 , int var_2 , int var_3 ) {
if ( var_1 == null || var_1 . length == 0 || ( var_2 == 0 && var_3 == 0 ) ) {
return 0 ;
}
int var_7 = var_1 . length ;
int [ ] [ ] [ ] var_9 = new int [ var_2 + 1 ] [ var_3 + 1 ] [ var_7 + 1 ] ;
for ( int var_10 = 0 ;
var_10 <= var_2 ;
var_10 ++ ) {
for ( int var_11 = 0 ;
var_11 <= var_3 ;
var_11 ++ ) {
for ( int var_12 = 0 ;
var_12 <= var_7 ;
var_12 ++ ) {
if ( var_12 == 0 || ( var_10 == 0 && var_11 == 0 ) ) {
var_9 [ var_10 ] [ var_11 ] [ var_12 ] = 0 ;
continue ;
}
char [ ] var_14 = var_1 [ var_12 - 1 ] . toCharArray ( ) ;
int var_15 = 0 ;
int var_16 = 0 ;
for ( char var_17 : var_14 ) {
var_15 += var_17 == '1' ? 1 : 0 ;
var_16 += var_17 == '0' ? 1 : 0 ;
}
var_9 [ var_10 ] [ var_11 ] [ var_12 ] = var_9 [ var_10 ] [ var_11 ] [ var_12 - 1 ] ;
if ( var_10 >= var_16 && var_11 >= var_15 ) {
var_9 [ var_10 ] [ var_11 ] [ var_12 ] = Math . max ( var_9 [ var_10 ] [ var_11 ] [ var_12 ] , var_9 [ var_10 - var_16 ] [ var_11 - var_15 ] [ var_12 - 1 ] + 1 ) ;
}
}
}
}
return var_9 [ var_2 ] [ var_3 ] [ var_7 ] ;
}
|
197 | <awangdev>747. Largest Number At Least Twice of Others_1.java | public int dominantIndex(int[] nums) {
if (nums == null || nums.length <= 1) {
return 0;
}
int max = -1;
int leastMax = -1;
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] >= max) {
leastMax = max;
max = nums[i];
index = i;
} else {
leastMax = Math.max(leastMax, nums[i]);
}
}
return max >= leastMax * 2 ? index : -1;
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length <= 1 ) {
return 0 ;
}
int max = - 1 ;
int var_6 = - 1 ;
int var_7 = 0 ;
for ( int var_8 = 0 ;
var_8 < var_0 . length ;
var_8 ++ ) {
if ( var_0 [ var_8 ] >= max ) {
var_6 = max ;
max = var_0 [ var_8 ] ;
var_7 = var_8 ;
}
else {
var_6 = Math . max ( var_6 , var_0 [ var_8 ] ) ;
}
}
return max >= var_6 * 2 ? var_7 : - 1 ;
}
|
198 | <awangdev>849. Maximize Distance to Closest Person_1.java | public int maxDistToClosest(int[] seats) {
if (seats == null || seats.length == 0) return -1;
int n = seats.length, dist = 0;
int start = seats[0] == 1 ? 0 : -1;
for (int end = 1; end < n; end++) {
if (seats[end] == 1) {
if (start == -1) dist = end;
else dist = Math.max(dist, (end - start) / 2);
start = end;
}
}
if (seats[n - 1] == 0) dist = Math.max(dist, n - 1 - start);
return dist;
} | public int sample_funcname ( int [ ] var_0 ) {
if ( var_0 == null || var_0 . length == 0 ) return - 1 ;
int var_5 = var_0 . length , var_6 = 0 ;
int var_7 = var_0 [ 0 ] == 1 ? 0 : - 1 ;
for ( int var_8 = 1 ;
var_8 < var_5 ;
var_8 ++ ) {
if ( var_0 [ var_8 ] == 1 ) {
if ( var_7 == - 1 ) var_6 = var_8 ;
else var_6 = Math . max ( var_6 , ( var_8 - var_7 ) / 2 ) ;
var_7 = var_8 ;
}
}
if ( var_0 [ var_5 - 1 ] == 0 ) var_6 = Math . max ( var_6 , var_5 - 1 - var_7 ) ;
return var_6 ;
}
|
199 | <awangdev>7. Reverse Integer_2.java | public int reverse(int x) {
long rst = 0;
while (x != 0) {
rst = rst * 10 + x % 10;
x /= 10;
if (rst > Integer.MAX_VALUE || rst < Integer.MIN_VALUE) return 0;
}
return (int) rst;
} | public int sample_funcname ( int var_0 ) {
long var_2 = 0 ;
while ( var_0 != 0 ) {
var_2 = var_2 * 10 + var_0 % 10 ;
var_0 /= 10 ;
if ( var_2 > Integer . MAX_VALUE || var_2 < Integer . MIN_VALUE ) return 0 ;
}
return ( int ) var_2 ;
}
|