Compare commits
11 Commits
75c4bd8473
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc82d7eb10 | ||
|
|
3c203342a5 | ||
|
|
d910c716e6 | ||
|
|
6dc597fa75 | ||
|
|
0540e83113 | ||
|
|
9639d043e8 | ||
|
|
d0478198a9 | ||
|
|
f06095ad7e | ||
|
|
59f245dda7 | ||
|
|
d568671b85 | ||
|
|
7bbd47f65f |
68
1034.coloring-a-border.java
Normal file
68
1034.coloring-a-border.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* @lc app=leetcode id=1034 lang=java
|
||||
*
|
||||
* [1034] Coloring A Border
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int[][] colorBorder(int[][] grid, int row, int col, int color) {
|
||||
int[][] track = new int[grid.length][grid[0].length];
|
||||
clearState(track);
|
||||
int[][] cgrid = copyGrid(grid);
|
||||
colorBorder(cgrid, grid, track, row, col, color, grid[row][col]);
|
||||
return cgrid;
|
||||
}
|
||||
|
||||
private void colorBorder(int[][] cgrid, int[][] grid, int[][] track, int i, int j, int borderColor, int componentColor) {
|
||||
if(track[i][j] != 0 || grid[i][j] != componentColor) {
|
||||
return;
|
||||
}
|
||||
if(isBorder(grid, i, j, componentColor)) {
|
||||
cgrid[i][j] = borderColor;
|
||||
}
|
||||
track[i][j] = 1;
|
||||
if(i+1 < grid.length && track[i+1][j] == 0) {
|
||||
colorBorder(cgrid, grid, track, i+1, j, borderColor, componentColor);
|
||||
}
|
||||
if(i-1 >= 0 && track[i-1][j] == 0) {
|
||||
colorBorder(cgrid, grid, track, i-1, j, borderColor, componentColor);
|
||||
}
|
||||
if(j+1 < grid[0].length && track[i][j+1] == 0) {
|
||||
colorBorder(cgrid, grid, track, i, j+1, borderColor, componentColor);
|
||||
}
|
||||
if(j-1 >= 0 && track[i][j-1] == 0) {
|
||||
colorBorder(cgrid, grid, track, i, j-1, borderColor, componentColor);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isBorder(int[][] grid, int i, int j, int componentColor) {
|
||||
if(i == 0 || i == grid.length-1 || j == 0 || j == grid[0].length - 1) {
|
||||
return true;
|
||||
}
|
||||
if(i + 1 < grid.length && grid[i+1][j] != componentColor || i-1 >= 0 && grid[i-1][j] != componentColor || j+1 < grid[0].length && grid[i][j+1] != componentColor || j-1 >=0 && grid[i][j-1] != componentColor) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void clearState(int[][] g) {
|
||||
for(int i = 0; i < g.length; i++) {
|
||||
for (int j = 0; j < g[i].length; j++) {
|
||||
g[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int[][] copyGrid(int[][] grid) {
|
||||
int[][] copy = new int[grid.length][grid[0].length];
|
||||
for(int i = 0; i<grid.length; i++) {
|
||||
for(int j = 0; j<grid[0].length; j++) {
|
||||
copy[i][j] = grid[i][j];
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
25
1037.valid-boomerang.java
Normal file
25
1037.valid-boomerang.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* @lc app=leetcode id=1037 lang=java
|
||||
*
|
||||
* [1037] Valid Boomerang
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
/**
|
||||
* y1 / x1 = y0 / x0
|
||||
* x0*y1 = x1*y0
|
||||
*
|
||||
* @param points
|
||||
* @return
|
||||
*/
|
||||
public boolean isBoomerang(int[][] points) {
|
||||
int x0 = points[1][0] - points[0][0];
|
||||
int y0 = points[1][1] - points[0][1];
|
||||
int x1 = points[2][0] - points[0][0];
|
||||
int y1 = points[2][1] - points[0][1];
|
||||
return x0 * y1 != x1 * y0;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
24
1046.last-stone-weight.java
Normal file
24
1046.last-stone-weight.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @lc app=leetcode id=1046 lang=java
|
||||
*
|
||||
* [1046] Last Stone Weight
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int lastStoneWeight(int[] stones) {
|
||||
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b)->b-a);
|
||||
for(int i = 0; i < stones.length; i++) {
|
||||
pq.add(stones[i]);
|
||||
}
|
||||
int a, b;
|
||||
while(pq.size() > 1) {
|
||||
a = pq.poll();
|
||||
b = pq.poll();
|
||||
pq.add(a < b?b-a:a-b);
|
||||
}
|
||||
return pq.peek();
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
58
109.convert-sorted-list-to-binary-search-tree.java
Normal file
58
109.convert-sorted-list-to-binary-search-tree.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* @lc app=leetcode id=109 lang=java
|
||||
*
|
||||
* [109] Convert Sorted List to Binary Search Tree
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public TreeNode sortedListToBST(ListNode head) {
|
||||
return buildTree(head);
|
||||
}
|
||||
|
||||
private TreeNode buildTree(ListNode head) {
|
||||
if(head == null || head.next == null) {
|
||||
return head == null ? null : new TreeNode(head.val, null, null);
|
||||
}
|
||||
ListNode dummy = new ListNode(0);
|
||||
dummy.next = head;
|
||||
ListNode slow = dummy, fast = dummy, prev = dummy;
|
||||
while(fast != null && fast.next != null) {
|
||||
fast = fast.next.next;
|
||||
prev = slow;
|
||||
slow = slow.next;
|
||||
}
|
||||
prev.next = null;
|
||||
TreeNode tree = new TreeNode(slow.val, null, null);
|
||||
tree.left = buildTree(dummy.next);
|
||||
tree.right = buildTree(slow.next);
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
75
1138.alphabet-board-path.java
Normal file
75
1138.alphabet-board-path.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* @lc app=leetcode id=1138 lang=java
|
||||
*
|
||||
* [1138] Alphabet Board Path
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public String alphabetBoardPath(String target) {
|
||||
char cur = 'a';
|
||||
StringBuilder bd = new StringBuilder();
|
||||
for(int i = 0; i < target.length(); i++) {
|
||||
bd.append(actions(cur, target.charAt(i)));
|
||||
cur = target.charAt(i);
|
||||
}
|
||||
return bd.toString();
|
||||
}
|
||||
|
||||
private int[] distance(char a, char b) {
|
||||
int[] pa = new int[] {
|
||||
(a-'a') / 5,
|
||||
(a-'a') % 5
|
||||
};
|
||||
int[] pb = new int[] {
|
||||
(b - 'a') / 5,
|
||||
(b - 'a') % 5
|
||||
};
|
||||
return new int[] {
|
||||
pb[0] - pa[0],
|
||||
pb[1] - pa[1]
|
||||
};
|
||||
}
|
||||
|
||||
private String actions(char s, char d) {
|
||||
int[] move = distance(s, d);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if(move[0] == 0 && move[1] == 0) {
|
||||
builder.append('!');
|
||||
} else {
|
||||
if(s == 'z') {
|
||||
builder.append(rowActions(move[0]));
|
||||
builder.append(colActions(move[1]));
|
||||
} else {
|
||||
builder.append(colActions(move[1]));
|
||||
builder.append(rowActions(move[0]));
|
||||
}
|
||||
builder.append('!');
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private String rowActions(int x) {
|
||||
if(x == 0) {
|
||||
return "";
|
||||
}
|
||||
return x > 0 ? repeatChar('D', x):repeatChar('U', -x);
|
||||
}
|
||||
|
||||
private String colActions(int x) {
|
||||
if(x == 0) {
|
||||
return "";
|
||||
}
|
||||
return x > 0 ? repeatChar('R', x):repeatChar('L', -x);
|
||||
}
|
||||
|
||||
private String repeatChar(char ch, int n) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
for(int i = 0; i<n; i++) {
|
||||
s.append(ch);
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
23
122.best-time-to-buy-and-sell-stock-ii.java
Normal file
23
122.best-time-to-buy-and-sell-stock-ii.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* @lc app=leetcode id=122 lang=java
|
||||
*
|
||||
* [122] Best Time to Buy and Sell Stock II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int maxProfit(int[] prices) {
|
||||
if(prices == null || prices.length <= 1) {
|
||||
return 0;
|
||||
}
|
||||
int total = 0;
|
||||
for(int i = 1; i<prices.length; i++) {
|
||||
if(prices[i] - prices[i-1] > 0) {
|
||||
total += prices[i] - prices[i-1];
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
49
125.valid-palindrome.java
Normal file
49
125.valid-palindrome.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* @lc app=leetcode id=125 lang=java
|
||||
*
|
||||
* [125] Valid Palindrome
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public boolean isPalindrome(String s) {
|
||||
int i = 0, j = s.length()-1;
|
||||
boolean r = true;
|
||||
while(i < j) {
|
||||
while(i<j && !isAlphanumberic(s.charAt(i))) {
|
||||
i++;
|
||||
}
|
||||
while(i<j && !isAlphanumberic(s.charAt(j))) {
|
||||
j--;
|
||||
}
|
||||
if(i>=j) {
|
||||
break;
|
||||
}
|
||||
if(!isSameLeterIgnoreCase(s.charAt(i), s.charAt(j))) {
|
||||
r = false;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private boolean isAlphanumberic(char c) {
|
||||
return isAlpha(c) || isNumberic(c);
|
||||
}
|
||||
|
||||
private boolean isAlpha(char c) {
|
||||
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
|
||||
}
|
||||
|
||||
private boolean isNumberic(char c) {
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
private boolean isSameLeterIgnoreCase(char a, char b) {
|
||||
return isNumberic(a) && a == b || isAlpha(a) && isAlpha(b) && Character.toLowerCase(a) == Character.toLowerCase(b);
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
36
134.gas-station.java
Normal file
36
134.gas-station.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @lc app=leetcode id=134 lang=java
|
||||
*
|
||||
* [134] Gas Station
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
/**
|
||||
* 本题核心为:若从i开始,计算到j时发现不行,那么从(i, j]范围内任一站点开始也不行,易进行论证
|
||||
*
|
||||
* @param gas
|
||||
* @param cost
|
||||
* @return
|
||||
*/
|
||||
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||
int start = 0, sum = 0, curIdx = 0;
|
||||
while(start < gas.length) {
|
||||
if(curIdx - start == gas.length) {
|
||||
return start;
|
||||
}
|
||||
int pos = curIdx % gas.length;
|
||||
if(sum + gas[pos] - cost[pos] < 0) {
|
||||
curIdx++;
|
||||
start = curIdx;
|
||||
sum = 0;
|
||||
} else {
|
||||
sum += gas[pos] - cost[pos];
|
||||
curIdx++;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
26
136.single-number.java
Normal file
26
136.single-number.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @lc app=leetcode id=136 lang=java
|
||||
*
|
||||
* [136] Single Number
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
/**
|
||||
* 可以通过对数组中所有整数进行异或来找出该数,
|
||||
* 因为对于出现两次的整数,异或之后都为0,
|
||||
* 而对于只出现过一次的整数,和0异或之后为其本身
|
||||
*
|
||||
* @param nums
|
||||
* @return
|
||||
*/
|
||||
public int singleNumber(int[] nums) {
|
||||
int r = 0;
|
||||
for(int i = 0; i < nums.length; i++) {
|
||||
r = r ^ nums[i];
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
46
138.copy-list-with-random-pointer.java
Normal file
46
138.copy-list-with-random-pointer.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* @lc app=leetcode id=138 lang=java
|
||||
*
|
||||
* [138] Copy List with Random Pointer
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/*
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node random;
|
||||
|
||||
public Node(int val) {
|
||||
this.val = val;
|
||||
this.next = null;
|
||||
this.random = null;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public Node copyRandomList(Node head) {
|
||||
if(head == null) {
|
||||
return null;
|
||||
}
|
||||
Map<Node, Node> ref = new HashMap<>();
|
||||
Node cur = head;
|
||||
while(cur != null) {
|
||||
Node cp = new Node(cur.val);
|
||||
ref.put(cur, cp);
|
||||
cur = cur.next;
|
||||
}
|
||||
cur = head;
|
||||
while(cur != null) {
|
||||
Node nd = ref.get(cur);
|
||||
nd.next = ref.get(cur.next);
|
||||
nd.random = ref.get(cur.random);
|
||||
cur = cur.next;
|
||||
}
|
||||
return ref.get(head);
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
32
14.longest-common-prefix.java
Normal file
32
14.longest-common-prefix.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* @lc app=leetcode id=14 lang=java
|
||||
*
|
||||
* [14] Longest Common Prefix
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public String longestCommonPrefix(String[] strs) {
|
||||
if(strs == null || strs.length == 0) {
|
||||
return "";
|
||||
}
|
||||
String r = strs[0];
|
||||
for(int i=1; i<strs.length;i++) {
|
||||
r = longestCommonPrefix(r, strs[i]);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private String longestCommonPrefix(String a, String b) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int i = 0; i<a.length() && i<b.length();i++) {
|
||||
if(a.charAt(i) != b.charAt(i)) {
|
||||
break;
|
||||
}
|
||||
sb.append(a.charAt(i));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
41
141.linked-list-cycle.java
Normal file
41
141.linked-list-cycle.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* @lc app=leetcode id=141 lang=java
|
||||
*
|
||||
* [141] Linked List Cycle
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
/**
|
||||
* 快慢指针,若链表中存在环,则快指针一次动两步,慢指针一次动一步,快指针一定会赶上慢指针
|
||||
*
|
||||
* @param head
|
||||
* @return
|
||||
*/
|
||||
public boolean hasCycle(ListNode head) {
|
||||
ListNode slow = head, fast = head;
|
||||
boolean isCycle = false;
|
||||
while(fast != null && fast.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
if(fast == slow) {
|
||||
isCycle = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return isCycle;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
49
142.linked-list-cycle-ii.java
Normal file
49
142.linked-list-cycle-ii.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* @lc app=leetcode id=142 lang=java
|
||||
*
|
||||
* [142] Linked List Cycle II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
/**
|
||||
* 2 * k - s - (k - s) = p * r
|
||||
* 2 * k - s - k + s = p * r
|
||||
* k = p * r, 且 k > s
|
||||
*
|
||||
* s + p * r + m - 2m = q * r
|
||||
* s = q * r - p * r + m
|
||||
*
|
||||
* @param head
|
||||
* @return
|
||||
*/
|
||||
public ListNode detectCycle(ListNode head) {
|
||||
if(head == null) {
|
||||
return null;
|
||||
}
|
||||
Set<ListNode> tracked = new HashSet<>();
|
||||
ListNode cur = head, r = null;
|
||||
while(cur != null) {
|
||||
if(tracked.contains(cur)) {
|
||||
r = cur;
|
||||
break;
|
||||
}
|
||||
tracked.add(cur);
|
||||
cur = cur.next;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
52
143.reorder-list.java
Normal file
52
143.reorder-list.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* @lc app=leetcode id=143 lang=java
|
||||
*
|
||||
* [143] Reorder List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public void reorderList(ListNode head) {
|
||||
if(head == null || head.next == null) {
|
||||
return;
|
||||
}
|
||||
// reverse the right side and merge
|
||||
ListNode d1 = new ListNode(0);
|
||||
d1.next = head;
|
||||
ListNode slow = d1, fast = d1;
|
||||
while(fast!=null && fast.next!=null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
ListNode rh = slow.next, lh = head;
|
||||
slow.next = null;
|
||||
// reverse the right half
|
||||
ListNode prev = null, cur = rh, t;
|
||||
while(cur != null) {
|
||||
t = cur.next;
|
||||
cur.next = prev;
|
||||
prev = cur;
|
||||
cur = t;
|
||||
}
|
||||
rh = prev;
|
||||
// merge the two linkedlist
|
||||
while(rh != null) {
|
||||
t = lh.next;
|
||||
lh.next = rh;
|
||||
lh = rh;
|
||||
rh = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
42
147.insertion-sort-list.java
Normal file
42
147.insertion-sort-list.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* @lc app=leetcode id=147 lang=java
|
||||
*
|
||||
* [147] Insertion Sort List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode insertionSortList(ListNode head) {
|
||||
ListNode dummy = new ListNode(0);
|
||||
dummy.next = null;
|
||||
ListNode t;
|
||||
while(head != null) {
|
||||
t = head;
|
||||
head = head.next;
|
||||
ListNode prev = dummy;
|
||||
while(true) {
|
||||
if(prev.next == null || prev.next.val > t.val) {
|
||||
t.next = prev.next;
|
||||
prev.next = t;
|
||||
break;
|
||||
} else {
|
||||
prev = prev.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dummy.next;
|
||||
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
63
148.sort-list.java
Normal file
63
148.sort-list.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* @lc app=leetcode id=148 lang=java
|
||||
*
|
||||
* [148] Sort List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode sortList(ListNode head) {
|
||||
if(head == null || head.next == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode slow = head, fast = head, prev = null;
|
||||
while(fast != null && fast.next != null) {
|
||||
fast = fast.next.next;
|
||||
prev = slow;
|
||||
slow = slow.next;
|
||||
}
|
||||
prev.next = null;
|
||||
ListNode l1 = sortList(head);
|
||||
ListNode l2 = sortList(slow);
|
||||
return merge(l1, l2);
|
||||
}
|
||||
|
||||
private ListNode merge(ListNode l1, ListNode l2) {
|
||||
ListNode dummy = new ListNode(0), tail = dummy, tmp;
|
||||
dummy.next = null;
|
||||
while(l1 != null || l2 != null) {
|
||||
if(l1 == null || l2 == null) {
|
||||
if(l1 != null) {
|
||||
tail.next = l1;
|
||||
} else {
|
||||
tail.next = l2;
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
if(l1.val <= l2.val) {
|
||||
tmp = l1;
|
||||
l1 = l1.next;
|
||||
} else {
|
||||
tmp = l2;
|
||||
l2 = l2.next;
|
||||
}
|
||||
}
|
||||
tail.next = tmp;
|
||||
tail = tail.next;
|
||||
tail.next = null;
|
||||
}
|
||||
return dummy.next;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
59
160.intersection-of-two-linked-lists.java
Normal file
59
160.intersection-of-two-linked-lists.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* @lc app=leetcode id=160 lang=java
|
||||
*
|
||||
* [160] Intersection of Two Linked Lists
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
if(headA == null || headB == null) {
|
||||
return null;
|
||||
}
|
||||
int la = len(headA), lb = len(headB);
|
||||
ListNode cura = headA, curb = headB;
|
||||
if(la < lb) {
|
||||
// move curB
|
||||
for(int i=0; i<lb-la; i++) {
|
||||
curb = curb.next;
|
||||
}
|
||||
} else {
|
||||
// move curA
|
||||
for(int i = 0; i<la-lb; i++) {
|
||||
cura = cura.next;
|
||||
}
|
||||
}
|
||||
ListNode r = null;
|
||||
while(cura!=null) {
|
||||
if(cura == curb) {
|
||||
r = cura;
|
||||
break;
|
||||
}
|
||||
cura = cura.next;
|
||||
curb = curb.next;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private int len(ListNode head) {
|
||||
int l = 0;
|
||||
while(head != null) {
|
||||
l++;
|
||||
head = head.next;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
51
19.remove-nth-node-from-end-of-list.java
Normal file
51
19.remove-nth-node-from-end-of-list.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* @lc app=leetcode id=19 lang=java
|
||||
*
|
||||
* [19] Remove Nth Node From End of List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
/**
|
||||
* 在删除链表的倒数第n个node时,如果想在一次遍历中删除,可以使用两个指针:slow, fast
|
||||
* 其中,fast指针领先slow指针n个位置,之后fast指针和slow指针以相同的pace向后移动
|
||||
* 当fast到达链表尾部时,slow则是倒数第n个位置
|
||||
* 可以通过prev记录slow的前一个位置,然后删除prev节点
|
||||
*
|
||||
* @param head
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
public ListNode removeNthFromEnd(ListNode head, int n) {
|
||||
if(head == null) {
|
||||
return null;
|
||||
}
|
||||
ListNode prev = null, slow = head, fast = head;
|
||||
for(int i = 0; i < n; i++) {
|
||||
fast = fast.next;
|
||||
}
|
||||
while(fast != null) {
|
||||
fast = fast.next;
|
||||
prev = slow;
|
||||
slow = slow.next;
|
||||
}
|
||||
if(prev == null) {
|
||||
head = head.next;
|
||||
} else {
|
||||
prev.next = slow.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
56
2.add-two-numbers.java
Normal file
56
2.add-two-numbers.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* @lc app=leetcode id=2 lang=java
|
||||
*
|
||||
* [2] Add Two Numbers
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
|
||||
boolean propagated = false;
|
||||
ListNode r = null, cur = null;
|
||||
while(l1!=null ||l2!=null) {
|
||||
int v1 = l1!=null?l1.val:0;
|
||||
int v2 = l2!=null?l2.val:0;
|
||||
int v = v1 + v2 + (propagated?1:0);
|
||||
propagated = v/10!=0;
|
||||
v = v %10;
|
||||
// add node to result
|
||||
ListNode t = new ListNode(v);
|
||||
t.next = null;
|
||||
if(r == null) {
|
||||
r = t;
|
||||
cur = t;
|
||||
} else {
|
||||
cur.next = t;
|
||||
cur = cur.next;
|
||||
}
|
||||
|
||||
if(l1!=null) {
|
||||
l1 = l1.next;
|
||||
}
|
||||
if(l2!=null) {
|
||||
l2 = l2.next;
|
||||
}
|
||||
}
|
||||
if(propagated) {
|
||||
ListNode t = new ListNode(1);
|
||||
t.next = null;
|
||||
cur.next = t;
|
||||
cur = cur.next;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
38
203.remove-linked-list-elements.java
Normal file
38
203.remove-linked-list-elements.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* @lc app=leetcode id=203 lang=java
|
||||
*
|
||||
* [203] Remove Linked List Elements
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode removeElements(ListNode head, int val) {
|
||||
if(head == null) {
|
||||
return null;
|
||||
}
|
||||
while(head!=null && head.val == val) {
|
||||
head = head.next;
|
||||
}
|
||||
ListNode cur = head;
|
||||
while(cur != null && cur.next != null) {
|
||||
if(cur.next.val == val) {
|
||||
cur.next = cur.next.next;
|
||||
} else {
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
31
206.reverse-linked-list.java
Normal file
31
206.reverse-linked-list.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* @lc app=leetcode id=206 lang=java
|
||||
*
|
||||
* [206] Reverse Linked List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode prev = null, cur = head, t;
|
||||
while(cur != null) {
|
||||
t = cur.next;
|
||||
cur.next = prev;
|
||||
prev = cur;
|
||||
cur = t;
|
||||
}
|
||||
return prev;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
54
234.palindrome-linked-list.java
Normal file
54
234.palindrome-linked-list.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* @lc app=leetcode id=234 lang=java
|
||||
*
|
||||
* [234] Palindrome Linked List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
/**
|
||||
* linked list求中点方案:快慢指针:
|
||||
* 当fast移动n次时,列表节点个数为1+2n或2n
|
||||
* 此时,slow移动n次时遍历节点个数为1+n
|
||||
* 即,节点总个数为奇数时,slow刚好位于中点
|
||||
* 当节点总个数为偶数时,slow位于n+1节点,即下半部分链表的开始节点
|
||||
* @param head
|
||||
* @return
|
||||
*/
|
||||
public boolean isPalindrome(ListNode head) {
|
||||
ListNode slow = head, fast = head, prev = null, t;
|
||||
while(fast != null && fast.next != null) {
|
||||
fast = fast.next.next;
|
||||
t = slow.next;
|
||||
slow.next = prev;
|
||||
prev = slow;
|
||||
slow = t;
|
||||
}
|
||||
// 此时,prev为上半部分开始节点,cur为下半部分开始节点
|
||||
// 如果fast不为空,节点总数为奇数,fast为空,节点总数为偶数
|
||||
if(fast != null) {
|
||||
slow = slow.next;
|
||||
}
|
||||
boolean f = true;
|
||||
while(slow != null) {
|
||||
if(slow.val != prev.val) {
|
||||
f = false;
|
||||
}
|
||||
slow = slow.next;
|
||||
prev = prev.next;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
33
237.delete-node-in-a-linked-list.java
Normal file
33
237.delete-node-in-a-linked-list.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* @lc app=leetcode id=237 lang=java
|
||||
*
|
||||
* [237] Delete Node in a Linked List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public void deleteNode(ListNode node) {
|
||||
if(node == null || node.next == null) {
|
||||
return;
|
||||
}
|
||||
ListNode cur = node, prev = null;
|
||||
while(cur!=null && cur.next!=null) {
|
||||
cur.val = cur.next.val;
|
||||
prev = cur;
|
||||
cur = cur.next;
|
||||
}
|
||||
if(prev != null) {
|
||||
prev.next = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
37
24.swap-nodes-in-pairs.java
Normal file
37
24.swap-nodes-in-pairs.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* @lc app=leetcode id=24 lang=java
|
||||
*
|
||||
* [24] Swap Nodes in Pairs
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode swapPairs(ListNode head) {
|
||||
ListNode dummy = new ListNode(0);
|
||||
dummy.next = head;
|
||||
ListNode prev = dummy, cur = head, next = head == null ? null : head.next, t;
|
||||
while(cur !=null && next != null) {
|
||||
prev.next = next;
|
||||
t = next.next;
|
||||
next.next = cur;
|
||||
cur.next = t;
|
||||
|
||||
prev = cur;
|
||||
cur = cur.next;
|
||||
next = cur == null ? null : cur.next;
|
||||
}
|
||||
return dummy.next;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
24
26.remove-duplicates-from-sorted-array.java
Normal file
24
26.remove-duplicates-from-sorted-array.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @lc app=leetcode id=26 lang=java
|
||||
*
|
||||
* [26] Remove Duplicates from Sorted Array
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int removeDuplicates(int[] nums) {
|
||||
if(nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int i = 0;
|
||||
for(int j = 0; j< nums.length; j++) {
|
||||
if(nums[j] != nums[i]) {
|
||||
i++;
|
||||
nums[i] = nums[j];
|
||||
}
|
||||
}
|
||||
return i+1;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
21
27.remove-element.java
Normal file
21
27.remove-element.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* @lc app=leetcode id=27 lang=java
|
||||
*
|
||||
* [27] Remove Element
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int removeElement(int[] nums, int val) {
|
||||
int i = 0;
|
||||
for(int j = 0; j < nums.length; j++) {
|
||||
if(nums[j] != val) {
|
||||
nums[i] = nums[j];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
24
28.find-the-index-of-the-first-occurrence-in-a-string.java
Normal file
24
28.find-the-index-of-the-first-occurrence-in-a-string.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @lc app=leetcode id=28 lang=java
|
||||
*
|
||||
* [28] Find the Index of the First Occurrence in a String
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int strStr(String haystack, String needle) {
|
||||
int idx = -1;
|
||||
if(haystack.length() < needle.length()) {
|
||||
return idx;
|
||||
}
|
||||
for(int i = 0; i < haystack.length(); i++) {
|
||||
if(i + needle.length() <= haystack.length() && haystack.substring(i, i + needle.length()).equals(needle)) {
|
||||
idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
39
3.longest-substring-without-repeating-characters.java
Normal file
39
3.longest-substring-without-repeating-characters.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* @lc app=leetcode id=3 lang=java
|
||||
*
|
||||
* [3] Longest Substring Without Repeating Characters
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
if(s.length() == 0) {
|
||||
return 0;
|
||||
}
|
||||
Set<Character> chs = new HashSet<>();
|
||||
int i = 0, j = 0, r = 0, cur = 0;
|
||||
while(j < s.length()) {
|
||||
if(!chs.contains(s.charAt(j))) {
|
||||
chs.add(s.charAt(j));
|
||||
cur++;
|
||||
if(cur > r) {
|
||||
r = cur;
|
||||
}
|
||||
} else {
|
||||
while(s.charAt(i) != s.charAt(j)) {
|
||||
chs.remove(s.charAt(i));
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
cur = j - i + 1;
|
||||
if(cur > r) {
|
||||
r = cur;
|
||||
}
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
44
328.odd-even-linked-list.java
Normal file
44
328.odd-even-linked-list.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* @lc app=leetcode id=328 lang=java
|
||||
*
|
||||
* [328] Odd Even Linked List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode oddEvenList(ListNode head) {
|
||||
if(head == null || head.next == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode oddDummy = new ListNode(0), oddTail = oddDummy, evenDummy = new ListNode(0), evenTail = evenDummy, cur = head;
|
||||
int i = 1;
|
||||
while(cur != null) {
|
||||
if(i % 2 == 1) {
|
||||
oddTail.next = cur;
|
||||
cur = cur.next;
|
||||
oddTail = oddTail.next;
|
||||
oddTail.next = null;
|
||||
} else {
|
||||
evenTail.next = cur;
|
||||
cur = cur.next;
|
||||
evenTail = evenTail.next;
|
||||
evenTail.next = null;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
oddTail.next = evenDummy.next;
|
||||
return oddDummy.next;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
20
392.is-subsequence.java
Normal file
20
392.is-subsequence.java
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* @lc app=leetcode id=392 lang=java
|
||||
*
|
||||
* [392] Is Subsequence
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int i = 0;
|
||||
for(int j = 0; j < t.length(); j++) {
|
||||
if(i < s.length() && t.charAt(j) == s.charAt(i)) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return i == s.length();
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
70
445.add-two-numbers-ii.java
Normal file
70
445.add-two-numbers-ii.java
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* @lc app=leetcode id=445 lang=java
|
||||
*
|
||||
* [445] Add Two Numbers II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
|
||||
l1 = reverse(l1);
|
||||
l2 = reverse(l2);
|
||||
return reverse(addByLittleEnd(l1, l2));
|
||||
}
|
||||
|
||||
private ListNode addByLittleEnd(ListNode l1, ListNode l2) {
|
||||
ListNode dummy = new ListNode(0), tail = dummy;
|
||||
dummy.next = null;
|
||||
boolean flow = false;
|
||||
while(l1 != null || l2 != null) {
|
||||
int v1 = l1==null?0:l1.val;
|
||||
int v2 = l2==null?0:l2.val;
|
||||
int v = v1 + v2 + (flow?1:0);
|
||||
flow = v>=10;
|
||||
ListNode tmp = new ListNode(v%10, null);
|
||||
tail.next = tmp;
|
||||
tail = tail.next;
|
||||
tail.next = null;
|
||||
if(l1 != null) {
|
||||
l1 = l1.next;
|
||||
}
|
||||
if(l2 != null) {
|
||||
l2 = l2.next;
|
||||
}
|
||||
}
|
||||
if(flow) {
|
||||
ListNode tmp = new ListNode(1, null);
|
||||
tail.next = tmp;
|
||||
tail = tail.next;
|
||||
tail.next = null;
|
||||
}
|
||||
return dummy.next;
|
||||
}
|
||||
|
||||
private ListNode reverse(ListNode l) {
|
||||
if(l == null) {
|
||||
return l;
|
||||
}
|
||||
ListNode dummy = new ListNode(0), tmp;
|
||||
dummy.next = null;
|
||||
while(l != null) {
|
||||
tmp = l.next;
|
||||
l.next = dummy.next;
|
||||
dummy.next = l;
|
||||
l = tmp;
|
||||
}
|
||||
return dummy.next;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
26
455.assign-cookies.java
Normal file
26
455.assign-cookies.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @lc app=leetcode id=455 lang=java
|
||||
*
|
||||
* [455] Assign Cookies
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int findContentChildren(int[] g, int[] s) {
|
||||
Arrays.sort(g);
|
||||
Arrays.sort(s);
|
||||
int i = g.length - 1, j = s.length - 1, r = 0;
|
||||
while(i>=0 && j>=0) {
|
||||
if(s[j] >= g[i]) {
|
||||
j--;
|
||||
i--;
|
||||
r++;
|
||||
} else {
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
23
55.jump-game.java
Normal file
23
55.jump-game.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* @lc app=leetcode id=55 lang=java
|
||||
*
|
||||
* [55] Jump Game
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public boolean canJump(int[] nums) {
|
||||
int cur = 0;
|
||||
for(int i=0;i<nums.length;i++) {
|
||||
if(i > cur) {
|
||||
return false;
|
||||
}
|
||||
if(nums[i]>0) {
|
||||
cur = Math.max(cur, i+nums[i]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
52
567.permutation-in-string.java
Normal file
52
567.permutation-in-string.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* @lc app=leetcode id=567 lang=java
|
||||
*
|
||||
* [567] Permutation in String
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public boolean checkInclusion(String s1, String s2) {
|
||||
if(s1.length() > s2.length()) {
|
||||
return false;
|
||||
}
|
||||
int[] c1 = strCompose(s1);
|
||||
int[] c2 = strCompose(s2.substring(0, s1.length()));
|
||||
boolean f = isComposeSame(c1, c2);
|
||||
int j = s1.length();
|
||||
while(!f && j < s2.length()) {
|
||||
c2[s2.charAt(j - s1.length())-'a']--;
|
||||
c2[s2.charAt(j)-'a']++;
|
||||
if(isComposeSame(c1, c2)) {
|
||||
f = true;
|
||||
break;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
private int[] strCompose(String s) {
|
||||
int[] c = new int[26];
|
||||
for(int i = 0; i < s.length(); i++) {
|
||||
c[s.charAt(i)-'a']++;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
private boolean isComposeSame(int[] a, int[] b) {
|
||||
if(a.length != b.length) {
|
||||
return false;
|
||||
}
|
||||
boolean matches = true;
|
||||
for(int i=0; i<a.length; i++) {
|
||||
if(a[i] != b[i]) {
|
||||
matches = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
30
58.length-of-last-word.java
Normal file
30
58.length-of-last-word.java
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* @lc app=leetcode id=58 lang=java
|
||||
*
|
||||
* [58] Length of Last Word
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int lengthOfLastWord(String s) {
|
||||
if(s == null || s.isBlank()) {
|
||||
return 0;
|
||||
}
|
||||
int i = s.length() - 1;
|
||||
while(i >= 0 && s.charAt(i) == ' ') {
|
||||
i--;
|
||||
}
|
||||
int r = 0;
|
||||
while(i >= 0) {
|
||||
if(s.charAt(i) != ' ') {
|
||||
r++;
|
||||
i--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
45
61.rotate-list.java
Normal file
45
61.rotate-list.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* @lc app=leetcode id=61 lang=java
|
||||
*
|
||||
* [61] Rotate List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode rotateRight(ListNode head, int k) {
|
||||
if(head == null) {
|
||||
return head;
|
||||
}
|
||||
int len = 0;
|
||||
ListNode cur = head;
|
||||
while(cur != null) {
|
||||
len++;
|
||||
if(cur.next == null) {
|
||||
cur.next = head;
|
||||
break;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
k = k % len;
|
||||
ListNode prev = cur;
|
||||
cur = head;
|
||||
for(int i = 0; i < len - k; i++) {
|
||||
cur = cur.next;
|
||||
prev = prev.next;
|
||||
}
|
||||
prev.next = null;
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
23
69.sqrt-x.java
Normal file
23
69.sqrt-x.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* @lc app=leetcode id=69 lang=java
|
||||
*
|
||||
* [69] Sqrt(x)
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int mySqrt(int x) {
|
||||
long l = 0, r = Integer.MAX_VALUE;
|
||||
while(r-l > 1) {
|
||||
long mid = l + (r-l)/2;
|
||||
if(mid * mid <= (long)x) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid;
|
||||
}
|
||||
}
|
||||
return (int)l;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
53
725.split-linked-list-in-parts.java
Normal file
53
725.split-linked-list-in-parts.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* @lc app=leetcode id=725 lang=java
|
||||
*
|
||||
* [725] Split Linked List in Parts
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode[] splitListToParts(ListNode head, int k) {
|
||||
int l = len(head);
|
||||
int n = l / k, m = l % k, j = 0;
|
||||
ListNode cur = head;
|
||||
ListNode[] r = new ListNode[k];
|
||||
for(int i = 0; i<k; i++) {
|
||||
int p = n;
|
||||
if(j < m) {
|
||||
j++;
|
||||
p++;
|
||||
}
|
||||
r[i] = cur;
|
||||
if(p>0) {
|
||||
for(int u = 0; u < p-1; u++) {
|
||||
cur = cur.next;
|
||||
}
|
||||
ListNode tmp = cur.next;
|
||||
cur.next = null;
|
||||
cur = tmp;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
private int len(ListNode head) {
|
||||
int r = 0;
|
||||
while(head != null) {
|
||||
r++;
|
||||
head = head.next;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
38
766.toeplitz-matrix.java
Normal file
38
766.toeplitz-matrix.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* @lc app=leetcode id=766 lang=java
|
||||
*
|
||||
* [766] Toeplitz Matrix
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public boolean isToeplitzMatrix(int[][] matrix) {
|
||||
for(int i=0; i<matrix.length; i++) {
|
||||
if(!sameElementsLTRP(matrix, i, 0)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for(int j = 0; j<matrix[0].length; j++) {
|
||||
if(!sameElementsLTRP(matrix, 0, j)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean sameElementsLTRP(int[][] matrix, int i, int j) {
|
||||
int e = matrix[i][j];
|
||||
boolean f = true;
|
||||
while(i < matrix.length && j < matrix[0].length) {
|
||||
if(matrix[i][j] != e) {
|
||||
f = false;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
26
80.remove-duplicates-from-sorted-array-ii.java
Normal file
26
80.remove-duplicates-from-sorted-array-ii.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @lc app=leetcode id=80 lang=java
|
||||
*
|
||||
* [80] Remove Duplicates from Sorted Array II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int removeDuplicates(int[] nums) {
|
||||
int i = -1, freq = 0;
|
||||
for(int j = 0; j< nums.length; j++) {
|
||||
if(i<0 || nums[i] == nums[j] && freq < 2) {
|
||||
i++;
|
||||
nums[i] = nums[j];
|
||||
freq++;
|
||||
} else if(nums[i] != nums[j]) {
|
||||
freq = 1;
|
||||
i++;
|
||||
nums[i] = nums[j];
|
||||
}
|
||||
}
|
||||
return i+1;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
42
82.remove-duplicates-from-sorted-list-ii.java
Normal file
42
82.remove-duplicates-from-sorted-list-ii.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* @lc app=leetcode id=82 lang=java
|
||||
*
|
||||
* [82] Remove Duplicates from Sorted List II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode deleteDuplicates(ListNode head) {
|
||||
ListNode dummy = new ListNode(0);
|
||||
ListNode tail = dummy;
|
||||
dummy.next = null;
|
||||
ListNode cur = head;
|
||||
int v;
|
||||
while(cur != null) {
|
||||
if(cur.next == null || cur.val != cur.next.val) {
|
||||
tail.next = cur;
|
||||
cur = cur.next;
|
||||
tail = tail.next;
|
||||
tail.next = null;
|
||||
} else {
|
||||
v = cur.val;
|
||||
while(cur != null && cur.val == v) {
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dummy.next;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
36
83.remove-duplicates-from-sorted-list.java
Normal file
36
83.remove-duplicates-from-sorted-list.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @lc app=leetcode id=83 lang=java
|
||||
*
|
||||
* [83] Remove Duplicates from Sorted List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode deleteDuplicates(ListNode head) {
|
||||
if(head == null || head.next == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode cur = head;
|
||||
while(cur!=null && cur.next != null) {
|
||||
if(cur.val == cur.next.val) {
|
||||
// remove cur.next
|
||||
cur.next = cur.next.next;
|
||||
} else {
|
||||
cur = cur.next;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
57
838.push-dominoes.java
Normal file
57
838.push-dominoes.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* @lc app=leetcode id=838 lang=java
|
||||
*
|
||||
* [838] Push Dominoes
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public String pushDominoes(String dominoes) {
|
||||
final int N = dominoes.length();
|
||||
int[] states = new int[dominoes.length()];
|
||||
for(int i = 0; i<dominoes.length(); i++) {
|
||||
if(dominoes.charAt(i) == 'L') {
|
||||
states[i] -= N;
|
||||
int j = i-1;
|
||||
int fluence = N;
|
||||
while(j>=0) {
|
||||
--fluence;
|
||||
states[j] -= fluence;
|
||||
if(dominoes.charAt(j) !='.') {
|
||||
break;
|
||||
}
|
||||
j--;
|
||||
}
|
||||
} else if(dominoes.charAt(i) == 'R') {
|
||||
states[i] += N;
|
||||
int j = i+1;
|
||||
int fluence = N;
|
||||
while(j < dominoes.length()) {
|
||||
--fluence;
|
||||
states[j] += fluence;
|
||||
if(dominoes.charAt(j) != '.') {
|
||||
break;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int i = 0; i<states.length; i++) {
|
||||
if(states[i] < 0) {
|
||||
sb.append('L');
|
||||
} else if(states[i] > 0) {
|
||||
sb.append('R');
|
||||
} else {
|
||||
sb.append('.');
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
43
86.partition-list.java
Normal file
43
86.partition-list.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* @lc app=leetcode id=86 lang=java
|
||||
*
|
||||
* [86] Partition List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode partition(ListNode head, int x) {
|
||||
ListNode tf = new ListNode(0),
|
||||
tb = new ListNode(0);
|
||||
tf.next = null;
|
||||
tb.next = null;
|
||||
ListNode cur = head, tailf = tf, tailb = tb;
|
||||
while(cur != null) {
|
||||
if(cur.val < x) {
|
||||
tailf.next = cur;
|
||||
cur = cur.next;
|
||||
tailf = tailf.next;
|
||||
tailf.next = null;
|
||||
} else {
|
||||
tailb.next = cur;
|
||||
cur = cur.next;
|
||||
tailb = tailb.next;
|
||||
tailb.next = null;
|
||||
}
|
||||
}
|
||||
tailf.next = tb.next;
|
||||
return tf.next;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
26
908.smallest-range-i.java
Normal file
26
908.smallest-range-i.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @lc app=leetcode id=908 lang=java
|
||||
*
|
||||
* [908] Smallest Range I
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int smallestRangeI(int[] nums, int k) {
|
||||
if(nums == null || nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int min = nums[0], max = nums[0];
|
||||
for(int i = 1; i < nums.length; i++) {
|
||||
if(nums[i] < min) {
|
||||
min = nums[i];
|
||||
}
|
||||
if(nums[i] > max) {
|
||||
max = nums[i];
|
||||
}
|
||||
}
|
||||
return max - min > 2 * k ? max - min - 2 * k : 0;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
45
92.reverse-linked-list-ii.java
Normal file
45
92.reverse-linked-list-ii.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* @lc app=leetcode id=92 lang=java
|
||||
*
|
||||
* [92] Reverse Linked List II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
/**
|
||||
* 链表逆序:头插法
|
||||
* @param head
|
||||
* @param left
|
||||
* @param right
|
||||
* @return
|
||||
*/
|
||||
public ListNode reverseBetween(ListNode head, int left, int right) {
|
||||
ListNode dummy = new ListNode(0);
|
||||
dummy.next = head;
|
||||
ListNode prev = dummy;
|
||||
for(int i = 0; i< left-1; i++) {
|
||||
prev = prev.next;
|
||||
}
|
||||
ListNode cur = prev.next;
|
||||
ListNode tail = cur, tmp;
|
||||
for(int i = 0; i < right-left; i++) {
|
||||
cur = tail.next;
|
||||
tail.next = cur.next;
|
||||
cur.next = prev.next;
|
||||
prev.next = cur;
|
||||
}
|
||||
return dummy.next;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user