力扣---2020.9.28
117. 填充每個節點的下一個右側節點指標 II
class Solution {
public Node connect(Node root) {
if(root == null){
return null;
}
if(root.left != null && root.right != null){
root.left.next = root.right;
}
if(root.left != null && root.right == null){
root.left.next = getnext(root.next);
}
if(root.right != null){
root.right.next = getnext(root.next);
}
connect(root.right);
connect(root.left);
return root;
}
public Node getnext(Node root){
if(root==null) return null;
if(root.left != null){
return root.left;
}
if(root.right != null){
return root.right;
}
if(root.next != null){
return getnext(root.next);
}
return null;
}
}
class Solution {
public Node connect(Node root) {
if (root == null) {
return null;
}
Queue<Node> queue = new LinkedList<Node>();
queue.offer(root);
while (!queue.isEmpty()) {
int n = queue.size();
Node last = null;
for (int i = 1; i <= n; ++i) {
Node f = queue.poll();
if (f.left != null) {
queue.offer(f.left);
}
if (f.right != null) {
queue.offer(f.right);
}
if (i != 1) {
last.next = f;
}
last = f;
}
}
return root;
}
}
581. 最短無序連續子陣列
public class Solution {
public int findUnsortedSubarray(int[] nums) {
int[] snums = nums.clone();
Arrays.sort(snums);
int start = snums.length, end = 0;
for (int i = 0; i < snums.length; i++) {
if (snums[i] != nums[i]) {
start = Math.min(start, i);
end = Math.max(end, i);
}
}
return (end - start >= 0 ? end - start + 1 : 0);
}
}
class Solution {
public int findUnsortedSubarray(int[] nums) {
Stack<Integer> stack = new Stack<Integer>();
int min = nums.length;
int max = 0;
for(int i = 0;i < nums.length;i++){
while(!stack.isEmpty() && nums[stack.peek()] > nums[i]){
min = Math.min(min,stack.pop());
}
stack.push(i);
}
for(int i = nums.length - 1;i >= 0;i--){
while(!stack.isEmpty() && nums[stack.peek()] < nums[i]){
max = Math.max(max,stack.pop());
}
stack.push(i);
}
return max>=min ? (max-min+1):0;
}
}
//大佬的程式碼
public class Solution {
public int findUnsortedSubarray(int[] nums) {
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
boolean flag = false;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1])
flag = true;
if (flag)
min = Math.min(min, nums[i]);
}
flag = false;
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] > nums[i + 1])
flag = true;
if (flag)
max = Math.max(max, nums[i]);
}
int l, r;
for (l = 0; l < nums.length; l++) {
if (min < nums[l])
break;
}
for (r = nums.length - 1; r >= 0; r--) {
if (max > nums[r])
break;
}
return r - l < 0 ? 0 : r - l + 1;
}
}
208. 實現 Trie (字首樹)
class Trie {
class TireNode {
private boolean isEnd;
TireNode[] next;
public TireNode() {
isEnd = false;
next = new TireNode[26];
}
}
private TireNode root;
public Trie() {
root = new TireNode();
}
public void insert(String word) {
TireNode node = root;
for (char c : word.toCharArray()) {
if (node.next[c - 'a'] == null) {
node.next[c - 'a'] = new TireNode();
}
node = node.next[c - 'a'];
}
node.isEnd = true;
}
public boolean search(String word) {
TireNode node = root;
for (char c : word.toCharArray()) {
node = node.next[c - 'a'];
if (node == null) {
return false;
}
}
return node.isEnd;
}
public boolean startsWith(String prefix) {
TireNode node = root;
for (char c : prefix.toCharArray()) {
node = node.next[c - 'a'];
if (node == null) {
return false;
}
}
return true;
}
}
你知道的越多,你不知道的越多。
相關文章
- 力扣---2020.7.30力扣
- 力扣---2020.9.27力扣
- 力扣---2020.9.29力扣
- 力扣---2020.9.3力扣
- 力扣---2020.9.4力扣
- 力扣2713 2024.6.19力扣
- 力扣2589 5.16力扣
- 力扣1542 2024.5.22力扣
- 力扣題解力扣
- 力扣(LeetCode)543力扣LeetCode
- 力扣(LeetCode)934力扣LeetCode
- 力扣(LeetCode)103力扣LeetCode
- 力扣(LeetCode)513力扣LeetCode
- 力扣(LeetCode)389力扣LeetCode
- 力扣(LeetCode)796力扣LeetCode
- 力扣(LeetCode)863力扣LeetCode
- 力扣(LeetCode)310力扣LeetCode
- 力扣(LeetCode)130力扣LeetCode
- 力扣(LeetCode)965力扣LeetCode
- 力扣社群開通力扣
- 力扣-9.23-680力扣
- 力扣之按身高排序力扣排序
- 力扣之移動零力扣
- 力扣之兩數之和力扣
- 教你如何玩轉力扣力扣
- 力扣oj-字串相乘力扣字串
- 力扣最長公共字首力扣
- 力扣-48 旋轉影像力扣
- 力扣27. 移除元素力扣
- 力扣-376. 擺動序列力扣
- 力扣 22. 括號生成力扣
- 力扣-231. 2 的冪力扣
- 力扣-283. 移動零力扣
- 力扣-54. 螺旋矩陣力扣矩陣
- leetcode力扣 213. 打家劫舍 IILeetCode力扣
- 力扣之有效的迴文力扣
- 力扣之存在重複元素力扣
- 力扣#43 字串相乘(C++)力扣字串C++