class Solution {
public boolean isValid ( String s) {
Stack < Character > stack = new Stack < > ( ) ;
for ( char c : s. toCharArray ( ) ) {
if ( c == '(' || c == '{' || c == '[' ) {
stack. push ( c) ;
} else {
if ( stack. isEmpty ( ) ) {
return false ;
}
Character top = stack. pop ( ) ;
if ( ! mathch ( c, top) ) {
return false ;
}
}
}
return stack. isEmpty ( ) ;
}
public boolean mathch ( char c, char top) {
return ( c == ')' && top == '(' ) || ( c == '}' && top == '{' ) || ( c == ']' && top == '[' ) ;
}
}
class Solution {
public ListNode mergeTwoLists1 ( ListNode list1, ListNode list2) {
if ( list1 == null ) return list2;
if ( list2 == null ) return list1;
if ( list1 == null && list2 == null ) return null ;
ListNode res = list1. val <= list2. val ? list1 : list2;
res. next = mergeTwoLists ( res. next, list1. val > list2. val? list1 : list2) ;
return res;
}
public ListNode mergeTwoLists2 ( ListNode list1, ListNode list2) {
if ( list1 == null && list2 == null ) return null ;
if ( list1 == null ) return list2;
if ( list2 == null ) return list1;
ListNode dummy = new ListNode ( 0 ) ;
ListNode head = dummy;
while ( list1 != null && list2 != null ) {
if ( list1. val <= list2. val) {
head. next = list1;
list1 = list1. next;
} else {
head. next = list2;
list2 = list2. next;
}
head = head. next;
}
while ( list1 != null ) {
head. next = list1;
list1 = list1. next;
head = head. next;
}
while ( list2 != null ) {
head. next = list2;
list2 = list2. next;
head = head. next;
}
return dummy. next;
}
}
class Solution {
private List < String > pathList = new LinkedList < > ( ) ;
private int maxPairCount = 0 ;
public List < String > generateParenthesis ( int n) {
maxPairCount = n;
dfs ( "" , 0 , 0 ) ;
return pathList;
}
private void dfs ( String path, int leftCount, int rightCount) {
if ( path. length ( ) == ( maxPairCount << 1 ) ) {
pathList. add ( path) ;
return ;
}
if ( path. length ( ) == ( maxPairCount << 1 ) ) {
pathList. add ( path) ;
return ;
}
if ( leftCount < maxPairCount) {
dfs ( path + "(" , leftCount + 1 , rightCount) ;
}
if ( rightCount < leftCount) {
dfs ( path + ")" , leftCount, rightCount + 1 ) ;
}
}
}
class Solution {
public ListNode mergeKLists ( ListNode [ ] lists) {
if ( lists. length == 0 ) return null ;
Queue < ListNode > queue = new PriorityQueue < > ( Comparator . comparingInt ( x -> x. val) ) ;
for ( ListNode head : lists) {
while ( head != null ) {
queue. offer ( head) ;
head = head. next;
}
}
ListNode dummy = new ListNode ( 0 ) ;
ListNode head = dummy;
while ( ! queue. isEmpty ( ) ) {
head. next = queue. poll ( ) ;
head = head. next;
if ( queue. isEmpty ( ) ) {
head. next = null ;
}
}
return dummy. next;
}
}
class Solution {
public void nextPermutation ( int [ ] nums) {
int n = nums. length;
if ( n == 1 ) return ;
int i = n - 2 ;
while ( i >= 0 && nums[ i] >= nums[ i + 1 ] ) {
i-- ;
}
if ( i < 0 ) {
reverse ( nums, 0 , n - 1 ) ;
} else {
int j = n - 1 ;
while ( i < j && nums[ j] <= nums[ i] ) {
j-- ;
}
swap ( nums, i, j) ;
reverse ( nums, i + 1 , n - 1 ) ;
}
}
private void reverse ( int [ ] nums, int start, int end) {
while ( start <= end) {
swap ( nums, start, end) ;
start++ ;
end-- ;
}
}
private void swap ( int [ ] nums, int i, int j) {
if ( i == j) return ;
nums[ i] = nums[ i] ^ nums[ j] ;
nums[ j] = nums[ i] ^ nums[ j] ;
nums[ i] = nums[ i] ^ nums[ j] ;
}
暂时不写
class Solution {
public int search1 ( int [ ] nums, int target) {
for ( int i = 0 ; i < nums. length; i++ ) {
if ( target == nums[ i] ) return i;
}
return - 1 ;
}
public int search2 ( int [ ] nums, int target) {
int n = nums. length;
if ( n == 0 ) return - 1 ;
if ( n == 1 ) return nums[ 0 ] == target ? 0 : - 1 ;
int cutoff = - 1 ;
for ( int i = 0 ; i < n - 1 ; i++ ) {
if ( nums[ i] > nums[ i + 1 ] ) {
cutoff = i;
break ;
}
}
int left = binarySearch ( nums, 0 , cutoff, target) ;
int right = binarySearch ( nums, cutoff + 1 , n - 1 , target) ;
return left == - 1 ? right : left;
}
private int binarySearch ( int [ ] nums, int start, int end, int target) {
while ( start <= end) {
int mid = start + ( ( end - start) >>> 1 ) ;
if ( nums[ mid] == target) return mid;
else if ( nums[ mid] < target) start = mid + 1 ;
else {
end = mid - 1 ;
}
}
return - 1 ;
}
}
public int [ ] searchRange1 ( int [ ] nums, int target) {
int n = nums. length;
int first = findFirst ( nums, target, 0 , n - 1 ) ;
int last = findLast ( nums, target, Math . max ( first, 0 ) , n - 1 ) ;
return new int [ ] { first, last} ;
}
private int findFirst ( int [ ] nums, int target, int start, int end) {
while ( start <= end) {
int mid = start + ( ( end - start) >>> 1 ) ;
if ( nums[ mid] == target) {
if ( mid >= 1 && nums[ mid - 1 ] == nums[ mid] ) {
end = mid - 1 ;
} else {
return mid;
}
} else if ( nums[ mid] > target) {
end = mid - 1 ;
} else {
start = mid + 1 ;
}
}
return - 1 ;
}
private int findLast ( int [ ] nums, int target, int start, int end) {
while ( start <= end) {
int mid = start + ( ( end - start) >>> 1 ) ;
if ( nums[ mid] == target) {
if ( mid <= end - 1 && nums[ mid] == nums[ mid + 1 ] ) {
start = mid + 1 ;
} else {
return mid;
}
} else if ( nums[ mid] > target) {
end = mid - 1 ;
} else {
start = mid + 1 ;
}
}
return - 1 ;
}
class Solution {
private List < List < Integer > > pathList = new ArrayList < > ( ) ;
List < Integer > path = new ArrayList < > ( ) ;
int sum = 0 ;
public List < List < Integer > > combinationSum ( int [ ] candidates, int target) {
int n = candidates. length;
dfs ( candidates, target, 0 , n) ;
return pathList;
}
public void dfs ( int [ ] candidates, int target, int start, int end) {
if ( sum > target) {
return ;
}
if ( sum == target) {
pathList. add ( new ArrayList < > ( path) ) ;
return ;
}
while ( start < end) {
path. add ( candidates[ start] ) ;
sum += candidates[ start] ;
dfs ( candidates, target, start, end) ;
sum -= candidates[ start] ;
path. remove ( path. size ( ) - 1 ) ;
start++ ;
}
}
}
class Solution {
public int trap ( int [ ] height) {
int res = 0 ;
int maxHeightIndex = 0 ;
int maxHeight = 0 ;
for ( int i = 0 ; i < height. length; i++ ) {
if ( height[ i] >= maxHeight) {
maxHeight = height[ i] ;
maxHeightIndex = i;
}
}
for ( int i = 0 ; i < maxHeightIndex; i++ ) {
for ( int j = i + 1 ; j <= maxHeightIndex; j++ ) {
if ( height[ i] > height[ j] ) {
res += height[ i] - height[ j] ;
} else {
i = j;
}
}
}
for ( int i = height. length - 1 ; i > maxHeightIndex; i-- ) {
for ( int j = i - 1 ; j >= maxHeightIndex; j-- ) {
if ( height[ i] > height[ j] ) {
res += height[ i] - height[ j] ;
} else {
i = j;
}
}
}
return res;
}
}