![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
user2639
这个作者很懒,什么都没留下…
展开
-
LeetCode 53. Maximum Subarray
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-53-maximum-subarray/ 解答1[Java]: class Solution { public int maxSubArray(int[] nums) { if (nums.length <= 0) return 0; ...原创 2019-05-13 18:27:27 · 134 阅读 · 0 评论 -
LeetCode 167. Two Sum II - Input array is sorted
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-167-two-sum-ii-input-array-is-sorted/ 解答1[Java]: class Solution { public int[] twoSum(int[] numbers, int target) { int i = 0; int ...原创 2019-05-11 23:08:15 · 75 阅读 · 0 评论 -
LeetCode 209. Minimum Size Subarray Sum
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-209-minimum-size-subarray-sum/ 解答1[Java]: class Solution { public int minSubArrayLen(int s, int[] nums) { int left = 0; int right ...原创 2019-05-11 23:09:32 · 92 阅读 · 0 评论 -
LeetCode 215. Kth Largest Element in an Array
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-215-kth-largest-element-in-an-array/ 解答1[Java]: import java.util.Arrays; class Solution { public int findKthLargest(int[] nums, int k) { i...原创 2019-05-11 23:10:12 · 98 阅读 · 0 评论 -
LeetCode 76. Minimum Window Substring
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-76-minimum-window-substring/ 解答1[Java]: class Solution { public String minWindow(String s, String t) { int[] map = new int[128]; f...原创 2019-05-11 23:11:26 · 85 阅读 · 0 评论 -
LeetCode 349. Intersection of Two Arrays
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-349-intersection-of-two-arrays/ 解答1[Java]: import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; class Solution { public ...原创 2019-05-11 23:12:04 · 91 阅读 · 0 评论 -
LeetCode 438. Find All Anagrams in a String
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-438-find-all-anagrams-in-a-string/ 解法1[Java]: import java.util.ArrayList; import java.util.List; class Solution { public List<Integer> find...原创 2019-05-11 23:12:55 · 141 阅读 · 0 评论 -
LeetCode 1. Two Sum
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-1-two-sum/ 解答1[Java]: import java.util.HashMap; public class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer,...原创 2019-05-11 23:13:38 · 86 阅读 · 0 评论 -
20190308 - LeetCode 15
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-15-3sum/ 解答1[Java]: class Solution { public List<List<Integer>> threeSum(int[] num) { Arrays.sort(num); List<List&l...原创 2019-05-11 23:14:24 · 78 阅读 · 0 评论 -
LeetCode 18. 4Sum
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-18-4sum/ 解答1[Java]: class Solution { public List<List<Integer>> fourSum(int[] num, int target) { ArrayList<List<Integer&...原创 2019-05-11 23:14:58 · 80 阅读 · 0 评论 -
LeetCode 136. Single Number
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-136-single-number/ 解答1[Java]: class Solution { public int singleNumber(int[] nums) { } } 思路 If we take XOR of zero and some bit, it...原创 2019-05-12 16:16:26 · 90 阅读 · 0 评论 -
LeetCode 202. Happy Number
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-202-happy-number/ 解答1[Java]: class Solution { public boolean isHappy(int n) { Set<Integer> exist = new HashSet<>(); wh...原创 2019-05-12 16:17:11 · 109 阅读 · 0 评论 -
LeetCode 205. Isomorphic Strings
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-205-isomorphic-strings/ 解答1[Java]: class Solution { public boolean isIsomorphic(String s, String t) { int[] m1 = new int[256]; in...原创 2019-05-12 16:17:43 · 90 阅读 · 0 评论 -
LeetCode 242. Valid Anagram
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-242-valid-anagram/ 解答1[Java]: class Solution { public boolean isAnagram(String s, String t) { if(s.length() != t.length()) return false; ...原创 2019-05-12 16:18:21 · 97 阅读 · 0 评论 -
LeetCode 290. Word Pattern
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-290-word-pattern/ 解答1[Java]: class Solution { public boolean wordPattern(String pattern, String str) { String[] words = str.split(" "); ...原创 2019-05-12 16:18:54 · 100 阅读 · 0 评论 -
LeetCode 350. Intersection of Two Arrays II
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-350-intersection-of-two-arrays-ii/ 解答1[Java]: public class Solution { public int[] intersect(int[] nums1, int[] nums2) { HashMap<Intege...原创 2019-05-12 16:19:49 · 106 阅读 · 0 评论 -
LeetCode 451. Sort Characters By Frequency
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-451-sort-characters-by-frequency/ 解答1[Java]: class Solution { public String frequencySort(String s) { int[] map = new int[128]; fo...原创 2019-05-12 16:20:40 · 88 阅读 · 0 评论 -
LeetCode 169. Majority Element
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-169-majority-element/ 解答1[Java]: class Solution { public int majorityElement(int[] nums) { int count = 0; Integer candidate = null...原创 2019-05-12 16:21:17 · 95 阅读 · 0 评论 -
LeetCode 125. Valid Palindrome
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-125-valid-palindrome/ 解答1[Java]: class Solution { private static final char[] charMap = new char[256]; static { for (int i = 0; i <...原创 2019-05-11 23:07:10 · 81 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array
欢迎访问原文所在博客:52Heartz’s Blog 解答1[Java] class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] result = new int[m + n]; int index = 0; int i = 0; ...原创 2019-05-10 15:03:34 · 90 阅读 · 0 评论 -
LeetCode 80. Remove Duplicates from Sorted Array II
欢迎访问原文所在博客:52Heartz’s Blog 解答1[Java]: class Solution { public int removeDuplicates(int[] nums) { int i = 0; for (int n : nums) if (i < 2 || n > nums[i - 2]) ...原创 2019-05-10 15:02:58 · 57 阅读 · 0 评论 -
LeetCode 70. Climbing Stairs
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-70-climbing-stairs/ 解答1[Java]: public class Solution { public int climbStairs(int n) { if (n == 1) { return 1; } ...原创 2019-05-13 18:28:03 · 133 阅读 · 0 评论 -
LeetCode 121. Best Time to Buy and Sell Stock
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-121-best-time-to-buy-and-sell-stock/ 解答1[Java]: public class Solution { public int maxProfit(int prices[]) { int minprice = Integer.MAX_VA...原创 2019-05-13 18:28:38 · 135 阅读 · 0 评论 -
LeetCode 137. Single Number II
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-137-single-number-ii/ 核心思想 一个 int 型整数有32个二进制位。 对于每个二进制位,统计这一位上1的个数,然后对3取余,如果余数不为0,说明不重复的那个数的这个二进制位为1。 统计每一位上1的个数,通过和二进制1进行位与操作。 如果某个二进制位是1,通过和结果进行 或 ...原创 2019-05-13 18:30:51 · 105 阅读 · 0 评论 -
LeetCode 19. Remove Nth Node From End of List
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-19-remove-nth-node-from-end-of-list/ 链表定义 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; ...原创 2019-05-13 18:31:40 · 124 阅读 · 0 评论 -
LeetCode 141. Linked List Cycle
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-141-linked-list-cycle/ LeetCode 141. // 链表的结构 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; *...原创 2019-05-13 18:32:15 · 118 阅读 · 0 评论 -
LeetCode 2. Add Two Numbers
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-2-add-two-numbers/ 解答1[Java]: 核心思想 代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ...原创 2019-05-13 18:33:11 · 102 阅读 · 0 评论 -
LeetCode 155. Min Stack
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-155-min-stack&jianzhi-offer-30/ 解答1: 核心思想 使用两个栈,一个数据栈,一个最小值栈。push 的时候,如果小于等于当前最小值,同时压入两个栈,然后每次弹出的时候,数据栈弹出的数和最小值栈的栈顶作比较,如果相同,那么把最小值也弹出。 代码 import j...原创 2019-05-13 18:34:24 · 120 阅读 · 0 评论 -
LeetCode 225. Implement Stack using Queues
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-225-implement-stack-using-queues/ 解答1[Java]:使用两个队列 核心思想 使用两个队列 q1 和 q2,push 的时候 push 到 q1 中,pop的时候,先把 q1 的 n-1 个元素 push 到 q2 中,剩下最后一个元素,然后弹出这个元素并返回。然后...原创 2019-05-13 18:35:00 · 129 阅读 · 0 评论 -
LeetCode 232. Implement Queue using Stacks
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-232-implement-queue-using-stacks/ 解答[Java] import java.util.ArrayDeque; import java.util.Deque; class MyQueue { Deque<Integer> stack1; ...原创 2019-05-13 18:35:46 · 113 阅读 · 0 评论 -
LeetCode 104. Maximum Depth of Binary Tree
题目来源:https://leetcode.com/problems/maximum-depth-of-binary-tree/ LeetCode官方题解及解析:104. Maximum Depth of Binary Tree 题目 Given a binary tree, find its maximum depth. The maximum depth is the number of no...原创 2019-05-10 14:50:56 · 107 阅读 · 0 评论 -
LeetCode 905. Sort Array By Parity
欢迎访问原文所在博客:52Heartz’s Blog 题目来源:https://leetcode.com/problems/sort-array-by-parity/ LeetCode官方题解及解析:905. Sort Array By Parity 题目 Given an array A of non-negative integers, return an array consisting o...原创 2019-05-10 14:58:29 · 121 阅读 · 0 评论 -
LeetCode 43. Multiply Strings
欢迎访问原文所在博客:52Heartz’s Blog 解答1[Java]: import java.util.Scanner; public class Main { public String multiply(String num1, String num2) { int n1 = num1.length(), n2 = num2.length(); i...原创 2019-05-10 14:59:33 · 94 阅读 · 0 评论 -
LeetCode 27. Remove Element
欢迎访问原文所在博客:52Heartz’s Blog 这道题可以和第283题 Remove Zeros 使用同样的思路解决。 解答1[Java] : class Solution { public int removeElement(int[] nums, int val) { int i = 0; for (int j = 0; j < nums....原创 2019-05-10 15:00:00 · 98 阅读 · 0 评论 -
LeetCode 283. Move Zeros
欢迎访问原文所在博客:52Heartz’s Blog 解答1[Java]: import java.util.ArrayList; class Solution { public void moveZeroes(int[] nums) { ArrayList<Integer> nonZeroElements = new ArrayList<>(); ...原创 2019-05-10 15:00:33 · 65 阅读 · 0 评论 -
LeetCode 3. Longest Substring Without Repeating Characters
欢迎访问原文所在博客:52Heartz’s Blog 解答1[Java]: class Solution { public int lengthOfLongestSubstring(String s) { int[] freq = new int[256]; int l = 0; int r = -1; int res = 0...原创 2019-05-10 15:01:02 · 68 阅读 · 0 评论 -
LeetCode 26. Remove Duplicates form Sorted Array
欢迎访问原文所在博客:52Heartz’s Blog 解答1[Java]: class Solution { public int removeDuplicates(int[] nums) { if(nums.length == 0) return 0; int head = 0; int tail = 0; for (;t...原创 2019-05-10 15:01:38 · 65 阅读 · 0 评论 -
LeetCode 75. Sort Colors
欢迎访问原文所在博客:52Heartz’s Blog 解答1[Java]: class Solution { public void sortColors(int[] nums) { int zero = -1; int two = nums.length; int temp; for (int i = 0; i < ...原创 2019-05-10 15:02:33 · 70 阅读 · 0 评论 -
LeetCode 240. Search a 2D Matrix II
欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-240-search-a-2d-matrix-ii/ 解答1[Java]: class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length == 0){ ...原创 2019-05-12 16:21:57 · 101 阅读 · 0 评论