04 2022 档案
896. Monotonic Array
摘要:My solution: class Solution { public boolean isMonotonic(int[] nums) { if(nums==null||nums.length<3){ return true; } int first = nums[0]; int second = 阅读全文
posted @ 2022-04-21 01:33 阳光明媚的菲越 阅读(19) 评论(0) 推荐(0)
**316. Remove Duplicate Letters
摘要:class Solution { public String removeDuplicateLetters(String s) { int[] lastIndex = new int[26]; for (int i = 0; i < s.length(); i++){ lastIndex[s.cha 阅读全文
posted @ 2022-04-20 13:54 阳光明媚的菲越 阅读(24) 评论(0) 推荐(0)
739. Daily Temperatures
摘要:This is the similar problem with: https://www.cnblogs.com/feiflytech/p/16169025.html class Solution { public int[] dailyTemperatures(int[] t) { int[] 阅读全文
posted @ 2022-04-20 13:43 阳光明媚的菲越 阅读(23) 评论(0) 推荐(0)
1019. Next Greater Node In Linked List
摘要:This problem is as same as https://www.cnblogs.com/feiflytech/p/16169025.html class Solution { public int[] nextLargerNodes(ListNode head) { List<Inte 阅读全文
posted @ 2022-04-20 13:24 阳光明媚的菲越 阅读(20) 评论(0) 推荐(0)
556. Next Greater Element III
摘要:This is a similiar problem with https://www.cnblogs.com/feiflytech/p/15862432.html The only differences are: 1. If no larger number can be returned, t 阅读全文
posted @ 2022-04-20 12:52 阳光明媚的菲越 阅读(31) 评论(0) 推荐(0)
1944. Number of Visible People in a Queue
摘要:This is the similiar problem with: https://www.cnblogs.com/feiflytech/p/16168587.html and https://www.cnblogs.com/feiflytech/p/16169025.html class Sol 阅读全文
posted @ 2022-04-20 11:59 阳光明媚的菲越 阅读(34) 评论(0) 推荐(0)
503. Next Greater Element II
摘要:Using stack, two pass: class Solution { public int[] nextGreaterElements(int[] nums) { Stack<Integer> stk = new Stack<>(); int[] res = new int[nums.le 阅读全文
posted @ 2022-04-20 11:11 阳光明媚的菲越 阅读(19) 评论(0) 推荐(0)
496. Next Greater Element I
摘要:My solution, time complexity O(m*n): class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { Map<Integer, Integer> map = new Hash 阅读全文
posted @ 2022-04-20 09:52 阳光明媚的菲越 阅读(34) 评论(0) 推荐(0)
34. Find First and Last Position of Element in Sorted Array
摘要:My binary search solution: class Solution { public int[] searchRange(int[] nums, int target) { if(nums==null || nums.length==0) return new int[]{-1,-1 阅读全文
posted @ 2022-04-20 07:25 阳光明媚的菲越 阅读(26) 评论(0) 推荐(0)
22. Generate Parentheses
摘要:My back tracking solution: class Solution { List<String> res = new ArrayList<>(); public List<String> generateParenthesis(int n) { backTracking(n, 0 , 阅读全文
posted @ 2022-04-19 11:23 阳光明媚的菲越 阅读(32) 评论(0) 推荐(0)
856. Score of Parentheses
摘要:Stack solution: class Solution { public int scoreOfParentheses(String s) { Stack<Integer> st = new Stack<>(); int score = 0; for(int i = 0; i < s.leng 阅读全文
posted @ 2022-04-19 10:19 阳光明媚的菲越 阅读(24) 评论(0) 推荐(0)
311. Sparse Matrix Multiplication
摘要:My first solution, time complexityO(m*n), where m is the cell number of mat1, and n is the cell number of mat2: class Solution { public int[][] multip 阅读全文
posted @ 2022-04-19 08:15 阳光明媚的菲越 阅读(28) 评论(0) 推荐(0)
1539. Kth Missing Positive Number
摘要:My first binary search solution: class Solution { public int findKthPositive(int[] arr, int k) { int l=0, r = arr.length-1; while(l+1<r){ int mid = (l 阅读全文
posted @ 2022-04-19 06:53 阳光明媚的菲越 阅读(23) 评论(0) 推荐(0)
1060. Missing Element in Sorted Array
摘要:My solution: class Solution { public int missingElement(int[] nums, int k) { int i=1; for(;i<nums.length;i++){ int diff = nums[i]-nums[i-1]-1; if(diff 阅读全文
posted @ 2022-04-14 11:43 阳光明媚的菲越 阅读(25) 评论(0) 推荐(0)
348. Design Tic-Tac-Toe
摘要:class TicTacToe { int n; int[] rows; int[] cols; int diag=0; int antiDiag = 0; public TicTacToe(int n) { this.n = n; rows = new int[n]; cols = new int 阅读全文
posted @ 2022-04-14 05:29 阳光明媚的菲越 阅读(27) 评论(0) 推荐(0)
863. All Nodes Distance K in Binary Tree
摘要:The key to solve this problem is to find the path from root to target, and put the length to target of every node from root to target to a map. Then e 阅读全文
posted @ 2022-04-14 05:11 阳光明媚的菲越 阅读(27) 评论(0) 推荐(0)
270. Closest Binary Search Tree Value
摘要:PreOrder: class Solution { double min = Integer.MAX_VALUE; int val; public int closestValue(TreeNode root, double target) { preOrder(root, target); re 阅读全文
posted @ 2022-04-14 02:47 阳光明媚的菲越 阅读(19) 评论(0) 推荐(0)
616. Add Bold Tag in String
摘要:This is a merge interval's variety: class Solution { public String addBoldTag(String s, String[] words) { List<int[]> intervals = new ArrayList<>(); f 阅读全文
posted @ 2022-04-14 02:29 阳光明媚的菲越 阅读(31) 评论(0) 推荐(0)
536. Construct Binary Tree from String
摘要:My solution, using stack: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode( 阅读全文
posted @ 2022-04-12 12:34 阳光明媚的菲越 阅读(27) 评论(0) 推荐(0)
778. Swim in Rising Water
摘要:BFS, using PriorityQueue to poll out the smallest number every time. The largest number you get will be the result. class Solution { public int swimIn 阅读全文
posted @ 2022-04-12 07:30 阳光明媚的菲越 阅读(17) 评论(0) 推荐(0)
273. Integer to English Words
摘要:class Solution { private String[] lessThanTwenty = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twe 阅读全文
posted @ 2022-04-12 07:04 阳光明媚的菲越 阅读(31) 评论(0) 推荐(0)
133. Clone Graph
摘要:This problem remember one thing, using HashMap as data structure: class Solution { Map<Integer, Node> map = new HashMap<>(); public Node cloneGraph(No 阅读全文
posted @ 2022-04-12 07:03 阳光明媚的菲越 阅读(21) 评论(0) 推荐(0)
129. Sum Root to Leaf Numbers
摘要:My first solution: class Solution { private int sum =0; public int sumNumbers(TreeNode root) { preOrder(root, ""); return sum; } private void preOrder 阅读全文
posted @ 2022-04-12 04:48 阳光明媚的菲越 阅读(19) 评论(0) 推荐(0)
140. Word Break II
摘要:My back tracking solution1: class Solution { List<String> res = new ArrayList<>(); Set<String> set = new HashSet<>(); public List<String> wordBreak(St 阅读全文
posted @ 2022-04-12 04:17 阳光明媚的菲越 阅读(29) 评论(0) 推荐(0)
1868. Product of Two Run-Length Encoded Arrays
摘要:My Solution: class Solution { public List<List<Integer>> findRLEArray(int[][] encoded1, int[][] encoded2) { int i=0,j=0; List<int[]> list = new ArrayL 阅读全文
posted @ 2022-04-12 04:01 阳光明媚的菲越 阅读(32) 评论(0) 推荐(0)
515. Find Largest Value in Each Tree Row
摘要:My BFS solution: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * Tr 阅读全文
posted @ 2022-04-12 01:22 阳光明媚的菲越 阅读(27) 评论(0) 推荐(0)
246. Strobogrammatic Number
摘要:class Solution { public boolean isStrobogrammatic(String num) { Map<Character, Character> map = new HashMap<>(); map.put('0', '0'); map.put('1', '1'); 阅读全文
posted @ 2022-04-09 13:34 阳光明媚的菲越 阅读(38) 评论(0) 推荐(0)
986. Interval List Intersections
摘要:My PriorityQueue Solution: class Solution { public int[][] intervalIntersection(int[][] firstList, int[][] secondList) { PriorityQueue<int[]> pq1 = ne 阅读全文
posted @ 2022-04-09 13:16 阳光明媚的菲越 阅读(23) 评论(0) 推荐(0)
23. Merge k Sorted Lists
摘要:My Solution 1: class Solution { public ListNode mergeKLists(ListNode[] lists) { PriorityQueue<ListNode> queue = new PriorityQueue<>((a,b)-> a.val-b.va 阅读全文
posted @ 2022-04-09 06:11 阳光明媚的菲越 阅读(18) 评论(0) 推荐(0)
415. Add Strings
摘要:class Solution { public String addStrings(String num1, String num2) { StringBuilder res = new StringBuilder(); int carry = 0; int i=num1.length()-1, j 阅读全文
posted @ 2022-04-09 05:55 阳光明媚的菲越 阅读(20) 评论(0) 推荐(0)
346. Moving Average from Data Stream
摘要:class MovingAverage { Queue<Integer> queue = new LinkedList<>(); int size = 0; double sum=0; public MovingAverage(int size) { this.size = size; } publ 阅读全文
posted @ 2022-04-09 05:25 阳光明媚的菲越 阅读(26) 评论(0) 推荐(0)
125. Valid Palindrome
摘要:class Solution { public boolean isPalindrome(String s) { s= s.toLowerCase(); int i=0, j=s.length()-1; while(i<j){ char a = s.charAt(i); char b = s.cha 阅读全文
posted @ 2022-04-09 04:32 阳光明媚的菲越 阅读(23) 评论(0) 推荐(0)
708. Insert into a Sorted Circular Linked List
摘要:We use two points to point to two successive nodes, we call them "pre" and "cur" We need to consider 3 situation need to be considered: 1. the inserte 阅读全文
posted @ 2022-04-09 02:34 阳光明媚的菲越 阅读(26) 评论(0) 推荐(0)
721. Accounts Merge
摘要:The most important things to solve this problem are: recognizing this is a graph problem. Select a correct data structure to store the graph: Map<Stri 阅读全文
posted @ 2022-04-08 03:59 阳光明媚的菲越 阅读(25) 评论(0) 推荐(0)
56. Merge Intervals
摘要:My PriorityQueue Solution: class Solution { public int[][] merge(int[][] intervals) { PriorityQueue<int[]> queue = new PriorityQueue<>((a,b)->a[0]-b[0 阅读全文
posted @ 2022-04-07 06:33 阳光明媚的菲越 阅读(31) 评论(0) 推荐(0)
301. Remove Invalid Parentheses
摘要:Just use BFS to solve this problem: 1. put the s to queue 2. if s is not a valid string, then remove a '(' or ')', and then put to the queue. 3. once 阅读全文
posted @ 2022-04-07 02:42 阳光明媚的菲越 阅读(39) 评论(0) 推荐(0)
65. Valid Number
摘要:For this problem, we need to consider the following cases: +23 - valid 23+5 - invalid 9e - invalid e6 - invalid 23e+5 - valid 9.6e2 - valid 9e6.2 - in 阅读全文
posted @ 2022-04-06 13:08 阳光明媚的菲越 阅读(11) 评论(0) 推荐(0)