class Solution {
public int threeSumClosest ( int [ ] nums, int target) {
Arrays. sort ( nums) ;
int n = nums. length;
int best = 10000000 ;
for ( int i = 0 ; i < n; ++ i) {
if ( i > 0 && nums[ i] == nums[ i - 1 ] ) {
continue ;
}
int j = i + 1 , k = n - 1 ;
while ( j < k) {
int sum = nums[ i] + nums[ j] + nums[ k] ;
if ( sum == target) {
return target;
}
if ( Math. abs ( sum - target) < Math. abs ( best - target) ) {
best = sum;
}
if ( sum > target) {
int k0 = k - 1 ;
while ( j < k0 && nums[ k0] == nums[ k] ) {
-- k0;
}
k = k0;
} else {
int j0 = j + 1 ;
while ( j0 < k && nums[ j0] == nums[ j] ) {
++ j0;
}
j = j0;
}
}
}
return best;
}
}
class Solution {
public boolean isValid ( String s) {
Stack< Character> stack = new Stack < Character> ( ) ;
int l = s. length ( ) ;
if ( l == 0 )
{
return true ;
}
for ( int i = 0 ; i < l; i++ )
{
char temp = s. charAt ( i) ;
if ( temp == '{' || temp == '[' || temp == '(' )
{
stack. push ( temp) ;
}
else if ( ! stack. isEmpty ( ) )
{
char pop = stack. pop ( ) ;
if ( ( temp == ')' && pop != '(' ) ||
( temp == ']' && pop != '[' ) ||
( temp == '}' && pop != '{' ) )
{
return false ;
}
}
else if ( stack. isEmpty ( ) )
{
return false ;
}
}
if ( stack. isEmpty ( ) )
{
return true ;
}
else
{
return false ;
}
}
}
class Solution {
public ListNode mergeTwoLists ( ListNode l1, ListNode l2) {
if ( l1 == null) {
return l2;
} else if ( l2 == null) {
return l1;
} else if ( l1. val < l2. val) {
l1. next = mergeTwoLists ( l1. next, l2) ;
return l1;
} else {
l2. next = mergeTwoLists ( l1, l2. next) ;
return l2;
}
}
}