<>2019.7.15～2019.7.19 leetcode刷题总结

1 两数之和(#2)

target，请你在该数组中找出和为目标值的那两个整数，并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是，你不能重复利用这个数组中同样的元素。

public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new
HashMap<>(); for (int i = 0; i < nums.length; i++) { int temp = target -
nums[i]; if (map.containsKey(temp)) { return new int[]{map.get(temp), i}; }
else { map.put(nums[i], i); } } return new int[2]; }
2 无重复字符的最长子串（#3）

public int lengthOfLongestSubstring(String s) { int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(); for (int j = 0, i = 0; j < n;
j++) { if (map.containsKey(s.charAt(j))) { i = Math.max(map.get(s.charAt(j)),
i); } ans = Math.max(ans, j - i + 1); map.put(s.charAt(j), j + 1); } return
ans; }
3 寻找两个有序数组的中位数（#4）

public double findMedianSortedArrays(int[] A, int[] B) { int m = A.length; int
n = B.length; if (m > n) { // to ensure m<=n int[] temp = A; A = B; B = temp;
int tmp = m; m = n; n = tmp; } int iMin = 0, iMax = m, halfLen = (m + n + 1) /
2; while (iMin <= iMax) { int i = (iMin + iMax) / 2; int j = halfLen - i; if (i
< iMax && B[j-1] > A[i]){ iMin = i + 1; // i is too small } else if (i > iMin
&& A[i-1] > B[j]) { iMax = i - 1; // i is too big } else { // i is perfect int
maxLeft = 0; if (i == 0) { maxLeft = B[j-1]; } else if (j == 0) { maxLeft =
A[i-1]; } else { maxLeft = Math.max(A[i-1], B[j-1]); } if ( (m + n) % 2 == 1 )
{ return maxLeft; } int minRight = 0; if (i == m) { minRight = B[j]; } else if
(j == n) { minRight = A[i]; } else { minRight = Math.min(B[j], A[i]); } return
(maxLeft + minRight) / 2.0; } } return 0.0; }
4 回文数（#9）

public boolean isPalindrome(int x) { // x为负数或者最后一位为0，则肯定不是回文数 if (x < 0 || (x
!= 0 && x % 10 == 0)) { return false; } int reverse = 0; while (x > reverse) {
reverse = reverse * 10 + x % 10; x /= 10; } // 区分x的长度为奇偶的情况 return x == reverse
|| x == reverse / 10; }
5 合并两个有序链表（#21）

public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new
ListNode(0); ListNode temp = head; while (l1 != null && l2 != null) { if
= l1; } if (l2 != null) { head.next = l2; } return temp.next; }
6 删除排序数组中的重复项（#26）

O(1) 额外空间的条件下完成。

public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; int
i = 0; for (int j = 1; j < nums.length; j++) { if (nums[j] != nums[i]) { i++;
nums[i] = nums[j]; } } return i + 1; }
7 移除元素（#27）

public int removeElement(int[] nums, int val) { int i = 0; for (int j = 0; j <
nums.length; j++) { if (nums[j] != val) { nums[i] = nums[j]; i++; } } return i;
}
8 删除链表的倒数第N个节点（#19）

null) { return null; } ListNode temp = head; ListNode lastN = head; ListNode
res = lastN; int i = 1; while (temp != null) { temp = temp.next; if (i <= n +
1) { i++; } else { lastN = lastN.next; } } if (i == n + 1) { return head.next;
} if (lastN.next != null) { lastN.next = lastN.next.next; } else { lastN.next =
null; } return res; }
9 爬楼梯（#70）

= 1, n=2时，f(2) = 2。是不是很像高中时候做的数学题目， 哈哈，编程就是数学。

public int climbStairs(int n) { if (n == 1) { return 1; } if (n == 2) { return
2; } int a = 1; int b = 2; int c = 0; for (int i = 3; i <= n; i++) { c = a + b;
a = b; b = c; } return c; }
10 合并两个有序数组（#88）

public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m - 1; int
j = n - 1; int q = m + n - 1; while (i >= 0 && j >= 0) { nums1[q--] = nums1[i]
> nums2[j] ? nums1[i--] : nums2[j--]; } while (i >= 0) { nums1[q--] =
nums1[i--]; } while (j >= 0) { nums1[q--] = nums2[j--]; } }
11 从中序与后序遍历序列构造二叉树（#106）

public TreeNode buildTree(int[] inorder, int[] postorder) { return
build(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1); }
public TreeNode build(int[] in, int inBegin, int inEnd, int[] post, int
postBegin, int postEnd) { if (inBegin > inEnd || postBegin > postEnd) { return
(int i = inBegin; i <= inEnd; i++) { if (in[i] == headVal) { head.left =
build(in, inBegin, i - 1, post, postBegin, i - inBegin + postBegin - 1);
head.right = build(in, i + 1, inEnd, post, i - inBegin + postBegin, postEnd -
1); } } return head; }
12 有效的字母异位词（#242）

public boolean isAnagram(String s, String t) { if (s.length() != t.length()) {
return false; } int[] counter = new int[26]; for (int i = 0; i < s.length();
i++) { counter[s.charAt(i) - 'a']++; counter[t.charAt(i) - 'a']--; } for (int a
: counter) { if (a != 0) { return false; } } return true; }
13 3的幂(326#)

public boolean isPowerOfThree(int n) { if (n < 1) { return false; } while (n %
3 == 0) { n /= 3; } return n == 1; }
14 奇偶链表（#328）

public ListNode oddEvenList(ListNode head) { ListNode even = new ListNode(0);
ListNode evenTemp = even; ListNode odd = new ListNode(0); ListNode oddTemp =
odd; int i = 1; while (head != null) { if (i % 2 == 0) { even.next = head; even
} odd.next = evenTemp.next; even.next = null; return oddTemp.next; }
15 两数相加（#2）

new ListNode(0); ListNode p = l1, q = l2, curr = dummyHead; int carry = 0;
while (p != null || q != null) { int x = (p != null) ? p.val : 0; int y = (q !=
null) ? q.val : 0; int sum = carry + x + y; carry = sum / 10; curr.next = new
ListNode(sum % 10); curr = curr.next; if (p != null) p = p.next; if (q != null)
q = q.next; } if (carry > 0) { curr.next = new ListNode(carry); } return

Toolsou
API参考文档