167. Two Sum II - Input array is sorted
class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
int i=0,j=numbers.length-1;
while(i<j){
if(numbers[i]+numbers[j]==target){
res[0]=i+1;
res[1]=j+1;
return res;
}else if(numbers[i]+numbers[j]<target){
i++;
}else{
j--;
}
}
return res;
}
}
88. Merge Sorted Array
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i=m-1,j=n-1;
int k=m+n-1;
while(i>=0 && j>=0){
if(nums1[i]>nums2[j]) nums1[k--]=nums1[i--];
else nums1[k--]=nums2[j--];
}
while(j>=0){
nums1[k--]=nums2[j--];
}
}
}
26. Remove Duplicates from Sorted Array
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length==0) return nums.length;
int n = nums.length;
int j=1;
for(int i=1;i<n;i++){
if(nums[i]!=nums[i-1]) nums[j++]=nums[i];
}
return j;
}
}
20. Valid Parentheses
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
char[] c = s.toCharArray();
for(int i=0;i<c.length;i++){
if(c[i]=='('){
stack.add(')');
}else if(c[i]=='['){
stack.add(']');
}else if(c[i]=='{'){
stack.add('}');
}else if(stack.isEmpty() || c[i]!=stack.pop()){
return false;
}
}
return stack.isEmpty();
}
}
32. Longest Valid Parentheses
class Solution {
public int longestValidParentheses(String s) {
char[] c = s.toCharArray();
int max= cal(c);
for(int i=0,j=c.length-1;i<=j;i++,j--){
char temp = c[i];
c[i]=c[j];
c[j]=temp;
}
for(int i=0;i<c.length;i++){
c[i]=(char)(c[i]^1);
}
return Math.max(max,cal(c));
}
public int cal(char[] c){
int res = 0;
for(int i=0,start=0,cnt=0;i<c.length;i++){
if(c[i]=='('){
cnt++;
}else {
cnt--;
if (cnt < 0) {
start = i + 1;
cnt = 0;
} else if (cnt == 0) {
res = Math.max(res, i - start + 1);
}
}
}
return res;
}
}
76. Minimum Window Substring
class Solution {
public String minWindow(String s, String t) {
String res="";
if(s.length()<t.length()) return res;
int[] hash = new int[128];
int cnt = 0;
for(Character c : t.toCharArray()){
hash[c-'A']++;
}
for(int num : hash){
if(num>0) cnt++;
}
char[] chars = s.toCharArray();
for(int i=0,j=0,c=0;i<chars.length;i++){
if(hash[chars[i]-'A']==1) c++;
hash[chars[i]-'A']--;
while(c==cnt && hash[chars[j]-'A']<0){
hash[chars[j++]-'A']++;
}
if(c==cnt){
if(res.isEmpty() || res.length()>i-j+1)
res=s.substring(j,i+1);
}
}
return res;
}
}
155. Min Stack
class MinStack {
Stack<Integer> stack = new Stack<>();
Stack<Integer> stack_min = new Stack<>();
/** initialize your data structure here. */
public MinStack() {
}
public void push(int x) {
stack.push(x);
if(stack_min.isEmpty()) stack_min.push(x);
else stack_min.push(Math.min(x,stack_min.peek()));
}
public void pop() {
stack.pop();
stack_min.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return stack_min.peek();
}
}
84. Largest Rectangle in Histogram
class Solution {
public int largestRectangleArea(int[] heights) {
Stack<Integer> stack = new Stack<>();
int n = heights.length;
int[] left = new int[n];
int[] right = new int[n];
for(int i=0;i<n;i++){
while(!stack.isEmpty() && heights[stack.peek()]>=heights[i]) stack.pop();
if(stack.isEmpty()) left[i]=-1;
else left[i]=stack.peek();
stack.push(i);
}
stack.clear();
for(int i=n-1;i>=0;i--){
while(!stack.isEmpty() && heights[stack.peek()]>=heights[i]) stack.pop();
if(stack.isEmpty()) right[i]=n;
else right[i]=stack.peek();
stack.push(i);
}
int res=0;
for(int i=0;i<n;i++){
res=Math.max(res,heights[i]*(right[i]-left[i]-1));
}
return res;
}
}
42. Trapping Rain Water
class Solution {
public int trap(int[] height) {
if(height.length==0) return 0;
int n = height.length;
int[] left = new int[n];
int[] right = new int[n];
int res = 0;
left[0]=height[0];right[n-1]=height[n-1];
for(int i=1;i<n;i++){
left[i]=Math.max(left[i-1],height[i]);
}
for(int i=n-2;i>=0;i--){
right[i]=Math.max(right[i+1],height[i]);
}
for(int i=0;i<n;i++){
res+=Math.min(left[i],right[i])-height[i];
}
return res;
}
}
239. Sliding Window Maximum
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length==0) return new int[]{};
int[] res = new int[nums.length-k+1];
Deque<Integer> deque = new LinkedList<>();
for(int i=0;i<nums.length;i++){
if(!deque.isEmpty() && deque.peekFirst()<i-k+1) deque.pollFirst();
while(!deque.isEmpty() && nums[deque.peekLast()]<=nums[i]) deque.pollLast();
deque.offer(i);
if(i>=k-1)
res[i-k+1]= nums[deque.peekFirst()];
}
return res;
}
}
918. Maximum Sum Circular Subarray
class Solution {
public int maxSubarraySumCircular(int[] A) {
int n = A.length;
int[] sum = new int[2*n];
for(int i=1;i<2*n;i++){
sum[i]=sum[i-1]+A[(i-1)%n];
}
int res = Integer.MIN_VALUE;
Deque<Integer> deque = new LinkedList<>();
deque.offerLast(0);
for(int i=1;i<2*n;i++){
if (!deque.isEmpty() && deque.peekFirst()<i-n) deque.pollFirst();
res=Math.max(res,sum[i]-sum[deque.peekFirst()]);
while(!deque.isEmpty() && sum[deque.peekLast()]>=sum[i]) deque.pollLast();
deque.offerLast(i);
}
return res;
}
}