- 博客(96)
- 收藏
- 关注
原创 Java函数式接口四部曲之Consumer
modify Consumer 接收一个 List, 将每个元素乘以2,displayList 打印每个元素,用andThen连起来。它表示一个接受单个输入参数并且不返回任何结果的操作。通常用于需要对输入参数执行某些操作但不产生返回值的场景。接口还提供了一个默认方法。是一个函数式接口,位于。的参数,并对其执行操作。
2025-03-10 22:57:23
228
原创 Optional 的用法
如果ofNullable不为空,orElse 会有value输出, orElseGet则不会有值。这是因为orElseGet是懒计算,notNullName有值,method不会被调用,而orElse则不是。orElse 和 orElseGet 源码,orElse入参是一个泛型T,orElseGet入参是一个Supplier。创建的Option实例可以为空。get获取Optional的值,如果为空的话,会报。用of 创建的Option实例必须为非空,用。创建一个为空的optional实例。
2024-03-24 22:27:01
241
原创 Java里的泛型<T> T 的运用
因为我总是写一些CRUD的代码,泛型的知识都忘得差不多了,今天特地来写一下。如果方法返回的是空的话,需要在public 和 void 之间加上 <T>在类的名称后面加上 <T>,构建类的时候在<>里写明类型。泛型可以认为是 把类型参数化,方便代码重用。我有两个数组,写一个方法得到最后的一个元素。
2024-03-09 19:53:38
450
1
原创 Leetcode My calender
My calendar IImplement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.Your class will have the method, book(int start,
2017-11-20 05:06:33
278
原创 Leetcode 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.给一个BST, 找到第 K 小的数方法
2017-10-01 08:53:12
194
原创 Leetcode Combination
Combination Sum 1Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated
2017-08-29 12:27:40
539
原创 recursive and iterative ways to reverse a LinkedList
public ListNode reverseList(ListNode head) { return helper(head, null); } private ListNode helper(ListNode head, ListNode newHead) { if (head == null) return newHead; L
2017-08-29 04:58:02
311
原创 Read N Characters Given Read4
The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the
2017-08-16 19:55:34
250
原创 Leetcode 174. Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially p
2017-08-16 16:57:47
161
原创 Leetcode 269. Alien Dictionary
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted le
2017-08-16 00:59:20
438
原创 Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,
2017-08-15 16:20:29
192
原创 Leetcode 658. Find K Closest Elements
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always p
2017-08-14 22:45:16
908
原创 Leetcode 126. Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a timeEac
2017-08-05 18:44:34
288
原创 word break/pattern/ladder
139. Word BreakGiven a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dic
2017-08-05 18:36:24
391
原创 Leetcode Best Time to Buy and Sell Stock 全集
Best Time to Buy and Sell Stock ISay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy
2017-08-03 01:53:51
452
1
原创 Leetcode 395. Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.Example 1:Input:s = "aaabb", k =
2017-08-02 22:35:20
189
原创 Leetcode 410. Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m sub
2017-08-01 15:14:37
195
原创 Leetcode 149. Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.思路是:用两个循环去查看所有情况same 表示两个点完全重合sameX 表示两个点有相同的x坐标public int maxPoints(Point[] poin
2017-08-01 01:23:26
197
原创 Leetcode 464. Can I Win
In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.What if we change the game s
2017-07-30 22:28:23
239
原创 Leetcode 368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.If there are multiple solution
2017-07-30 15:57:59
191
原创 Leetcode 315. Count of Smaller Numbers After Self
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].Example
2017-07-26 18:06:43
401
原创 Leetcode 310. Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called mini
2017-07-26 16:05:12
181
原创 Leetcode 300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], ther
2017-07-25 22:00:25
214
原创 Leetcode 373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.Define a pair (u,v) which consists of one element from the first array and one element from the second a
2017-07-21 14:54:00
188
原创 Leetcode 621. Task Scheduler
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could b
2017-07-21 14:37:34
421
原创 Leetcode Basic Calculator I && II
Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and em
2017-07-20 01:04:04
189
原创 Leetcode 282. Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or *between the digits so they evaluate to the target value.Exam
2017-07-19 20:04:31
194
原创 Leetcode 261. Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.For example:Given n = 5 an
2017-07-19 00:05:53
814
原创 Leetcode 115. Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of S which equals T.A subsequence of a string is a new string which is formed from the original string by deleting some (
2017-07-18 22:14:15
220
原创 Leetcode 305. Number of Islands II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to opera
2017-07-18 18:23:25
396
原创 Leetcode 547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, the
2017-07-18 17:22:36
360
原创 Leetcode 133. Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for each
2017-07-18 15:05:26
200
原创 Course Schedule I && II 总结
There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as
2017-07-17 21:36:30
247
原创 Leetcode 72. Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:
2017-07-17 16:28:30
160
原创 Leetcode 159. Longest Substring with At Most Two Distinct Characters
Given a string, find the length of the longest substring T that contains at most 2 distinct characters.For example, Given s = “eceba”,T is "ece" which its length is 3. public in
2017-07-17 01:30:20
657
原创 Leetcode 76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BAN
2017-07-16 23:25:19
444
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人