- 博客(15)
- 收藏
- 关注
原创 leetcode 383. Ransom Note
class Solution { public boolean canConstruct(String ransomNote, String magazine) { //记录杂志字符串出现的次数 int[] arr = new int[26]; int temp; for (int i = 0; i < magazine.length(); i++) { temp = magazine.charAt(i).
2021-08-14 17:59:17
297
原创 leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock class Solution { public int maxProfit(int[] prices) { /* 考虑每次如何获取最大收益? 第i天的最大收益只需要知道前i天的最低点就可以算出来了。 而第i天以前(包括第i天)的最低点和i-1天的最低点有关: dp[i] = min(d[i-1],prices[i]) 其中dp[0
2021-08-14 16:09:32
261
原创 leetcode 130. Surrounded Regions
leetcode 130. Surrounded Regions class Solution { public void solve(char[][] board) { //[row][col] int rows=board.length; int columns=board[0].length; for (int i=0;i<rows;i++) { if (board[i][0]=='O') bou
2021-08-11 16:14:18
215
原创 leetcode 1222. Queens That Can Attack the King
we will start from the king position and check in each of the 8 directions (row 2, col 2, diagonal 2), if there is a queen in the line. Once we’ve found a queen in this direction, we break, cuz it will potentially block the way of other queens in the s...
2021-08-11 15:13:58
161
原创 leetcode 846. Hand of Straights
class Solution { public boolean isNStraightHand(int[] hand, int W) { PriorityQueue<Integer> minHeap = new PriorityQueue<>(); for(int i : hand){ minHeap.add(i); } while(minHeap.size() != 0) { .
2021-08-11 14:57:49
143
原创 leetcode 128. Longest Consecutive Sequence
leetcode 128. Longest Consecutive Sequence class Solution { public int longestConsecutive(int[] nums) { Set<Integer> set = new HashSet<Integer>(); for (int num: nums){ set.add(num); } int max_len
2021-08-11 14:50:28
124
原创 leetcode 1910. Remove All Occurrences of a Substring
1910. Remove All Occurrences of a Substring class Solution { public String removeOccurrences(String s, String part) { StringBuilder sb = new StringBuilder(); for (int i = 0; i<s.length(); i++){ sb.append(s.charAt(i));
2021-08-11 11:58:22
305
原创 leetcode 515. Find Largest Value in Each Tree Row
leetcode 515. Find Largest Value in Each Tree Row /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * Tr
2021-08-11 09:40:57
140
原创 leetcode 16. 3Sum Closest
leetcode 16. 3Sum Closest class Solution { public int threeSumClosest(int[] nums, int target) { int result=nums[0]+nums[1]+nums[nums.length-1]; Arrays.sort(nums); int i=0; for (i=0; i<nums.length-2; i++){
2021-08-11 08:50:42
260
原创 leetcode 1903. Largest Odd Number in String
leetcode 1903. Largest Odd Number in String greedy algorithm starting from the end, look for the last digit which is the odd number, then return from the start to that digit, would be the largest odd number. class Solution { public String largestOddNu
2021-08-10 16:47:55
329
原创 leetcode 1005. Maximize Sum Of Array After K Negations
leetcode 1005. Maximize Sum Of Array After K Negations class Solution { public int largestSumAfterKNegations(int[] nums, int k) { PriorityQueue<Integer> heap = new PriorityQueue(); for (int num: nums){ heap.offer(num);
2021-08-10 16:23:21
209
原创 leetcode 1046. Last Stone Weight
leetcode 1046. Last Stone Weight class Solution { public int lastStoneWeight(int[] stones) { PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>((a, b)-> b - a); for (int num: stones){ maxHeap.offer(
2021-08-10 12:30:28
135
原创 MergeSort Algorithm
MergeSort SRC: https://www.youtube.com/watch?v=6kQWyON6iBk&list=PLK0ZC7fyo01J-PAbYSTyiZCQXH2RSNOW_&index=3 // "static void main" must be defined in a public class. public class MergeSort { public static int[] mergeSort(int[] array){ int
2021-08-10 12:17:41
329
原创 leetcode 169. Majority Element
leetcode 169. Majority Element Boyer-Moore majority vote algorithm By definition, the majority element has to appear more than half of times, so there can only be one majority element. Using the Boyer-Moore majority vote algorithm, we keep count of the ti
2021-08-10 11:22:32
264
原创 Leetcode 23. Merge k Sorted Lists
Leetcode 23. Merge k Sorted Lists You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. 链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
2021-08-10 11:00:40
159
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅