大家好,我是
方圆
无它,唯手熟尔
目录
Hash相关
1. 两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 0;i < nums.length;i++) {
int temp = target - nums[i];
if(map.containsKey(temp)) {
res[0] = map.get(temp);
res[1] = i;
return res;
}
map.put(nums[i],i);
}
return res;
}
}
387. 字符串中的第一个唯一字符
class Solution {
public int firstUniqChar(String s) {
char[] chars = s.toCharArray();
HashMap<Character,Boolean> map = new HashMap<>();
for(int i = 0;i < chars.length;i++) {
map.put(chars[i],!map.containsKey(chars[i]));
}
for(int j = 0;j < chars.length;j++) {
if(map.get(chars[j])) return j;
}
return -1;
}
}
链表操作
2. 两数相加
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode pre = new ListNode(0);
ListNode cur = pre;
int carry = 0;
while(l1 != null || l2 != null) {
int x = l1 == null ? 0 : l1.val;
int y = l2 == null ? 0 : l2.val;
int sum = x + y + carry;
carry = sum / 10;
sum %= 10;
cur.next = new ListNode(sum);
cur = cur.next;
if(l1 != null) l1 = l1.next;
if(l2 != null) l2 = l2.next;
}
if(carry == 1) cur.next = new ListNode(carry);
return pre.next;
}
}
19. 删除链表的倒数第N个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pre = new ListNode(0);
pre.next = head;
ListNode cur = pre;
ListNode temp = pre;
while(n != 0) {
temp = temp.next;
n--;
}
while(temp.next != null) {
temp = temp.next;
cur = cur.next;
}
cur.next = cur.next.next;
return pre.next;
}
}
61. 旋转链表
//计算step这里总出错
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || head.next == null)
return head;
ListNode oldTail = head;
int length = 1;
while(oldTail.next != null) {
length++;
oldTail = oldTail.next;
}
oldTail.next = head;
ListNode newTail = head;
int step = length - k % length - 1;
for(int i = 0;i < step;i++) {
newTail = newTail.next;
}
ListNode res = newTail.next;
newTail.next = null;
return res;
}
}
138. 复制带随机指针的链表
class Solution {
HashMap<Node,Node> map = new HashMap<>();
public Node copyRandomList(Node head) {
if(head == null) return null;
if(map.containsKey(head)) return map.get(head);
Node node = new Node(head.val);
map.put(head,node);
node.next = copyRandomList(head.next);
node.random = copyRandomList(head.random);
return node;
}
}
206. 翻转链表
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
ListNode temp = null;
while(cur != null) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
双指针遍历(滑动窗口)
3. 无重复字符的最长子串
class Solution {
public int lengthOfLongestSubstring(String s) {
int res = 0;
int left = 0,right = 0;
char[] strs = s.toCharArray();
int len = strs.length;
HashMap<Character,Integer> map = new HashMap<>();
if(len == 0) return res;
while(right < len) {
if(map.containsKey(strs[right]))
left = Math.max(left,map.get(strs[right]) + 1);
map.put(strs[right],right);
res = Math.max(res,right - left + 1);
right++;
}
return res;
}
}
11. 盛最多水的容器
class Solution {
public int maxArea(int[] height) {
int res = 0;
int left = 0,right = height.length - 1;
while(left < right) {
res = Math.max(res,(right - left) * Math.min(height[left],height[right]));
if(height[left] < height[right]) left++;
else right--;
}
return res;
}
}
15. 三数之和
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length < 3) return res;
Arrays.sort(nums);
for(int i = 0;i < nums.length;i++) {
if(nums[i] > 0) break;
if(i > 0 && nums[i] == nums[i - 1]) continue;
int left = i + 1;
int right = nums.length - 1;
while(left < right) {
int sum = nums[i] + nums[left] + nums[right];
if(sum == 0) {
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
while(left < right && nums[left] == nums[left + 1]) left++;
while(left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
} else if(sum > 0) right--;
else left++;
}
}
return res;
}
}
16. 最接近的三数之和
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int res = nums[0] + nums[1] + nums[2];
for(int i = 0;i < nums.length;i++) {
int left = i + 1;
int right = nums.length - 1;
while(left < right) {
int temp = nums[i] + nums[left] + nums[right];
if(Math.abs(temp - target) < Math.abs(res - target)) {
res = temp;
}
if(temp > target) right--;
else if(temp < target) left++;
else return res;
}
}
return res;
}
}
26. 删除排序数组中的重复项
class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int left = 0,right = 1;
while(right < nums.length) {
if(nums[left] != nums[right]) {
nums[left + 1] = nums[right];
left++;
}
right++;
}
return left + 1;
}
}
42. 接雨水
class Solution {
public int trap(int[] height) {
int length = height.length;
int[] left = new int[length];
int[] right = new int[length];
int leftMax = 0,rightMax = 0;
int res = 0;
for(int i = 0;i < length;i++) {
if(height[i] > leftMax) leftMax = height[i];
left[i] = leftMax;
if(height[length - 1 - i] > rightMax)
rightMax = height[length - 1 - i];
right[length - 1 - i] = rightMax;
}
for(int j = 0;j < length;j++) {
if(height[j] < left[j] && height[j] < right[j])
res += Math.min(left[j],right[j]) - height[j];
}
return res;
}
}
121. 买卖股票的最佳时机
class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int res = 0;
for(int i = 0;i < prices.length;i++) {
if(prices[i] < minPrice) minPrice = prices[i];
else res = Math.max(res,prices[i] - minPrice);
}
return res;
}
}
209. 长度最小的子数组
class Solution {
public int minSubArrayLen(int s, int[] nums) {
if(nums == null || nums.length == 0) return 0;
int left = 0,right = 0,sum = 0;
int res = Integer.MAX_VALUE;
while(right < nums.length) {
sum += nums[right];
while(sum >= s) {
res = Math.min(res,right - left + 1);
sum -= nums[left];
left++;
}
right++;
}
return res == Integer.MAX_VALUE ? 0 : res;
}
}
加油儿!