常规题
经典题
class Solution {
public int [ ] twoSum ( int [ ] nums, int target) {
if ( nums == null || nums. length == 0 ) {
return nums;
}
Map < Integer , Integer > numMap = new HashMap < > ( ) ;
for ( int i = 0 ; i < nums. length; i++ ) {
int num = nums[ i] ;
if ( numMap. containsKey ( target - num) ) {
return new int [ ] { i, numMap. get ( target - num) } ;
}
numMap. put ( num, i) ;
}
return new int [ ] { } ;
}
}
class Solution {
public List < List < Integer > > threeSum ( int [ ] nums) {
int n = nums. length;
Arrays . sort ( nums) ;
List < List < Integer > > ans = new ArrayList < List < Integer > > ( ) ;
for ( int first = 0 ; first < n; ++ first) {
if ( first > 0 && nums[ first] == nums[ first - 1 ] ) {
continue ;
}
int third = n - 1 ;
int target = - nums[ first] ;
for ( int second = first + 1 ; second < n; ++ second) {
if ( second > first + 1 && nums[ second] == nums[ second - 1 ] ) {
continue ;
}
while ( second < third && nums[ second] + nums[ third] > target) {
-- third;
}
if ( second == third) {
break ;
}
if ( nums[ second] + nums[ third] == target) {
List < Integer > list = new ArrayList < Integer > ( ) ;
list. add ( nums[ first] ) ;
list. add ( nums[ second] ) ;
list. add ( nums[ third] ) ;
ans. add ( list) ;
}
}
}
return ans;
}
}
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 List < List < Integer > > fourSum ( int [ ] nums, int target) {
List < List < Integer > > quadruplets = new ArrayList < List < Integer > > ( ) ;
if ( nums == null || nums. length < 4 ) {
return quadruplets;
}
Arrays . sort ( nums) ;
int length = nums. length;
for ( int i = 0 ; i < length - 3 ; i++ ) {
if ( i > 0 && nums[ i] == nums[ i - 1 ] ) {
continue ;
}
if ( nums[ i] + nums[ i + 1 ] + nums[ i + 2 ] + nums[ i + 3 ] > target) {
break ;
}
if ( nums[ i] + nums[ length - 3 ] + nums[ length - 2 ] + nums[ length - 1 ] < target) {
continue ;
}
for ( int j = i + 1 ; j < length - 2 ; j++ ) {
if ( j > i + 1 && nums[ j] == nums[ j - 1 ] ) {
continue ;
}
if ( nums[ i] + nums[ j] + nums[ j + 1 ] + nums[ j + 2 ] > target) {
break ;
}
if ( nums[ i] + nums[ j] + nums[ length - 2 ] + nums[ length - 1 ] < target) {
continue ;
}
int left = j + 1 , right = length - 1 ;
while ( left < right) {
int sum = nums[ i] + nums[ j] + nums[ left] + nums[ right] ;
if ( sum == target) {
quadruplets. add ( Arrays . asList ( nums[ i] , nums[ j] , nums[ left] , nums[ right] ) ) ;
while ( left < right && nums[ left] == nums[ left + 1 ] ) {
left++ ;
}
left++ ;
while ( left < right && nums[ right] == nums[ right - 1 ] ) {
right-- ;
}
right-- ;
} else if ( sum < target) {
left++ ;
} else {
right-- ;
}
}
}
}
return quadruplets;
}
}
class Solution {
public int fourSumCount ( int [ ] nums1, int [ ] nums2, int [ ] nums3, int [ ] nums4) {
Map < Integer , Integer > countMap = new HashMap < > ( ) ;
for ( int i : nums1) {
for ( int j : nums2) {
countMap. put ( ( i + j) , countMap. getOrDefault ( ( i + j) , 0 ) + 1 ) ;
}
}
int count = 0 ;
for ( int i : nums3) {
for ( int j : nums4) {
int target = 0 - i - j;
if ( countMap. containsKey ( target) ) {
count+= countMap. getOrDefault ( target, 0 ) ;
}
}
}
return count;
}
}
滑动窗口
class Solution {
public boolean containsNearbyDuplicate ( int [ ] nums, int k) {
if ( nums == null || nums. length == 0 || k <= 0 ) {
return false ;
}
Set < Integer > numSet = new HashSet < > ( ) ;
for ( int i = 0 ; i < nums. length; i++ ) {
if ( numSet. contains ( nums[ i] ) ) {
return true ;
}
numSet. add ( nums[ i] ) ;
if ( numSet. size ( ) == k+ 1 ) {
numSet. remove ( nums[ i- k] ) ;
}
}
return false ;
}
}
class Solution {
public boolean containsNearbyAlmostDuplicate ( int [ ] nums, int k, int t) {
TreeSet < Long > numSet = new TreeSet < > ( ) ;
int n = nums. length;
for ( int i = 0 ; i < n; i++ ) {
Long ceiling = numSet. ceiling ( ( long ) nums[ i] - ( long ) t) ;
if ( ceiling != null && ceiling <= ( long ) nums[ i] + ( long ) t) {
return true ;
}
numSet. add ( ( long ) nums[ i] ) ;
if ( numSet. size ( ) == k + 1 ) {
numSet. remove ( ( long ) nums[ i - k] ) ;
}
}
return false ;
}
}