LeetCode 热题 Hot 100
class Solution { public char nextGreatestLetter1(char[] letters, char target) { for (char letter : letters) { if (letter > target) return letter; } return letters[0]; } public char nextGreatestLetter2(char[] letters, char target) { int L = 0, R = letters.length - 1; while (L <= R) { int mid = L + ((R - L) >>> 1); if (letters[mid] <= target) { L = mid + 1; } else { R = mid - 1; } } return L < letters.length ? letters[L] : letters[0]; }}
class Solution { public int[] twoSum1(int[] nums, int target) { int n = nums.length; for (int i = 0; i < n; i++) { int l = nums[i]; for (int j = i + 1; j < n; j++) { int r = nums[j]; if (l + r == target) { return new int[]{i, j}; } } } return new int[]{}; } public int[] twoSum2(int[] nums, int target) { Map<Integer, Integer> hash = new HashMap<>(); int n = nums.length; for (int i = 0; i < n; i++) { if (hash.containsKey(target - nums[i])) { return new int[]{i, hash.get(target - nums[i])}; } else { hash.put(nums[i], i); } } return new int[]{}; }}
class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode resultHead = new ListNode(-1); ListNode resultTail = resultHead; int carry = 0; while (l1 != null || l2 != null) { int n1 = l1 != null ? l1.val : 0; int n2 = l2 != null ? l2.val : 0; int sum = n1 + n2 + carry; resultTail.next = new ListNode(sum % 10); resultTail = resultTail.next; carry = sum / 10; if (l1 != null) { l1 = l1.next; } if (l2 != null) { l2 = l2.next; } } if (carry > 0) { resultTail.next = new ListNode(carry); } return resultHead.next; }}
class Solution { public int lengthOfLongestSubstring(String s) { int l = 0, r = 0; int maxWindow = 0; Set<Character> windowSet = new HashSet<>(); while (l < s.length() && r < s.length()) { if (windowSet.contains(s.charAt(r))) { windowSet.remove(s.charAt(l)); l++; } else { windowSet.add(s.charAt(r)); r++; maxWindow = Math.max(r - l, maxWindow); } } return maxWindow; }}
class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int n = nums1.length + nums2.length; if (n % 2 == 0) { int l = findKth(nums1, 0, nums2, 0, n >>> 1); int r = findKth(nums1, 0, nums2, 0, (n >>> 1) + 1); return (l + r) / 2.0; } else { return findKth(nums1, 0, nums2, 0, (n >>> 1) + 1); } } public int findKth(int[] nums1, int i, int[] nums2, int j, int k) { if (nums1.length - i > nums2.length - j) { return findKth(nums2, j, nums1, i, k); } if (nums1.length == i) { return nums2[j + k - 1]; } if (k == 1) return Math.min(nums1[i], nums2[j]); int idx1 = Math.min(nums1.length, i + (k >>> 1)); int idx2 = j + k - (k >>> 1); if (nums1[idx1 - 1] < nums2[idx2 - 1]) { return findKth(nums1, idx1, nums2, j, k - (idx1 - i)); } else { return findKth(nums1, i, nums2, idx2, k - (idx2 - j)); } } }
暂时不写
class Solution { public int maxArea1(int[] height) { int res = 0; int n = height.length; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int area = (j - i) * Math.min(height[i], height[j]); res = Math.max(res, area); } } return res; } public int maxArea2(int[] height) { int n = height.length; int res = 0; int l = 0, r = n - 1; while (l < r) { int area = (r - l) * Math.min(height[l], height[r]); res = Math.max(res, area); if (height[l] < height[r]) { l++; } else { r--; } } return res; }}
public static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); int n = nums.length; if (n < 3) return res; Arrays.sort(nums); for (int i = 0; i < n - 2; i++) { if (i > 0 && nums[i - 1] == nums[i]) continue; int twoSum = -nums[i]; int l = i + 1, r = n - 1; while (l < r) { int currentSum = nums[l] + nums[r]; if (currentSum > twoSum) { r--; } else if (currentSum < twoSum) { l++; } else { res.add(Arrays.asList(nums[i], nums[l], nums[r])); l++; while (l < r && nums[l - 1] == nums[l]) { l++; } r--; while (l < r && nums[r] == nums[r + 1]) { r--; } } } } return res; }
class Solution { private String[] lettersOnDigit = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; private List<String> pathList = new LinkedList<>(); public List<String> letterCombinations(String digits) { if (digits == null || digits.length() == 0) return pathList; dfs(new StringBuilder(), digits); return pathList; } private void dfs(StringBuilder path, String digits) { if (path.length() == digits.length()) { pathList.add(path.toString()); return; } int currentDigit = digits.charAt(path.length()) - '0'; String currentLetters = lettersOnDigit[currentDigit]; for (char c : currentLetters.toCharArray()) { path.append(c); dfs(path, digits); path.deleteCharAt(path.length() - 1); } }}
class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(-1, head); int len = getLength(head); ListNode tmp = dummy; for (int i = 1; i <= (len - n); i++) { tmp = tmp.next; } tmp.next = tmp.next.next; return dummy.next; } private int getLength(ListNode node) { int len = 0; while (node != null) { len++; node = node.next; } return len; }}
哈尔滨保险