leetcode
Weijia_Song
这个作者很懒,什么都没留下…
展开
-
leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stockclass 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 · 209 阅读 · 0 评论 -
leetcode 130. Surrounded Regions
leetcode 130. Surrounded Regionsclass 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 · 167 阅读 · 0 评论 -
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 · 98 阅读 · 0 评论 -
leetcode 128. Longest Consecutive Sequence
leetcode 128. Longest Consecutive Sequenceclass 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 · 81 阅读 · 0 评论 -
leetcode 1910. Remove All Occurrences of a Substring
1910. Remove All Occurrences of a Substringclass 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 · 213 阅读 · 0 评论 -
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 · 99 阅读 · 0 评论 -
leetcode 16. 3Sum Closest
leetcode 16. 3Sum Closestclass 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 · 174 阅读 · 0 评论 -
leetcode 1903. Largest Odd Number in String
leetcode 1903. Largest Odd Number in Stringgreedy algorithmstarting 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 · 251 阅读 · 0 评论 -
leetcode 1005. Maximize Sum Of Array After K Negations
leetcode 1005. Maximize Sum Of Array After K Negationsclass 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 · 148 阅读 · 0 评论 -
leetcode 1046. Last Stone Weight
leetcode 1046. Last Stone Weightclass 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 · 87 阅读 · 0 评论 -
MergeSort Algorithm
MergeSortSRC: 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 · 252 阅读 · 0 评论 -
leetcode 169. Majority Element
leetcode 169. Majority ElementBoyer-Moore majority vote algorithmBy 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 · 201 阅读 · 0 评论 -
Leetcode 23. Merge k Sorted Lists
Leetcode 23. Merge k Sorted ListsYou 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 · 97 阅读 · 0 评论