Youwang Deng

I'm a software developer, familiar with C#, Java, JavaScript, focus on full stack development.

Leetcode Array Notes

13 Jun 2019 » Java, Leetcode, Leetcode-Array

1.Two Sum

  • array null, length and bound check
  • use hashmap to decrease time complexity
  • two pointer to solve sorted array problems
  • consider no result condition

4.Median of Two Sorted Arrays

  • abstract the problem to getKthMin problem
  • consider even and odd length of array
  • consider k VS length in getKthMin function
  • compare k/2 - 1 position in both array to abort half of k elements in either array
  • log(m+n) time complexity

11. Container With Most Water

  • two pointer to solve unsorted problems
  • two pointer to find max problem
  • move pointer strategy
  • container store min bound water

15. 3Sum

  • List<List<Integer>> res
  • skip duplicate answers outside for loop and inside for loop
  • reduce three sum to two sum
  • Arrays.asList(a,b,c) return List<Integer>
  • O(n^2) time complexity

16. 3Sum Closest

  • use two pointer strategy
  • update minDiff in each loop

18. 4Sum

  • use function to make code more readable
  • skip duplicate
  • check bound to skip or end loop

26. Remove Duplicates from Sorted Array

  • confirm the return type
  • in place modification of array
  • move pointer strategy

27. Remove Element

  • move pointer strategy
  • check bound

31. Next Permutation

  • find patter of permutation
  • back traverse find peak, then find change location, swap change location and reverse the back part
  • consider special case