classSolution{ public List<String> generateParenthesis(int n){ List<String> res = new ArrayList<>(); if(n == 0) { return res; } char[] array = newchar[2 * n]; helper(array, 0, n, n, res); return res; } privatevoidhelper(char[] array, int index, int left, int right, List<String> res){ if(index == array.length) { res.add(new String(array)); return; } if(left > 0) { array[index] = '('; helper(array, index + 1, left - 1, right, res); } if(right > left) { array[index] = ')'; helper(array, index + 1, left, right - 1, res); } } }
26. Remove Duplicates from Sorted Array
Description
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
Clarification
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); }
classSolution{ publicintstrStr(String haystack, String needle){ if (needle.isEmpty()) return0; if (!haystack.contains(needle)) return -1; for (int l = 0, r = needle.length()-1; l < haystack.length(); l++, r++){ if (haystack.substring(l, r+1).equals(needle)) return l; } return -1; } }
29. Divide Two Integers
Description
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero.
Example
Input: dividend = 10, divisor = 3 Output: 3
Input: dividend = 7, divisor = -3 Output: -2
Clarification
Both dividend and divisor will be 32-bit signed integers.
The divisor will never be 0.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,231−1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
classSolution{ publicintdivide(int dividend, int divisor){ if (divisor == 0) return Integer.MAX_VALUE; int symbol = 1; if ((dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)) symbol = -1;
long ldivident = Math.abs((long)dividend); long ldivisor = Math.abs((long)divisor);
if (ldivident == 0 || ldivident < ldivisor) return0; if (ldivisor == 1){ if (dividend == Integer.MIN_VALUE || dividend == Integer.MAX_VALUE){ if (symbol == -1) return Integer.MIN_VALUE; elsereturn Integer.MAX_VALUE; }else{ return (int)(symbol * ldivisor); } }
long lans = computeRes(ldivident, ldivisor); return (int)(lans * symbol); }
privatelongcomputeRes(long ldividend, long ldivisor){ if (ldividend - ldivisor < 0) return0; long sum = ldivisor; long temp = 1; while (sum + sum <= ldividend){ sum += sum; temp += temp; } return temp + computeRes(ldividend-sum, ldivisor); }