zhousilverbullet / dailyalgorithms Goto Github PK
View Code? Open in Web Editor NEW做题做题
做题做题
class Solution {
public int[] sortedSquares(int[] A) {
int len = A.length;
int[] ans = new int[len];
for (int i = 0; i < len; i++) {
int value = A[i] * A[i];
int j = i;
int temp = value;
while (j - 1 >= 0 && ans[j - 1] > value) {
ans[j] = ans[j-1];
j--;
}
ans[j] = temp;
}
return ans;
}
}
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) {
return null;
}
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow && fast != null) {
ListNode ptr = head;
while (ptr != slow) {
ptr = ptr.next;
slow = slow.next;
}
return ptr;
}
}
return null;
}
}
class Solution {
public Node connect(Node root) {
if (root == null) {
return root;
}
LinkedList<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
if (size == 1) {
queue.get(0).next = null;
} else {
for (int i = 0; i < size; i++) {
if (i == size - 1) {
queue.get(i).next = null;
} else {
queue.get(i).next = queue.get(i + 1);
}
}
}
while(size > 0) {
Node node = queue.removeFirst();
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
size--;
}
}
return root;
}
}
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) {
return null;
}
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow && fast != null) {
ListNode ptr = head;
while (ptr != slow) {
ptr = ptr.next;
slow = slow.next;
}
return ptr;
}
}
return null;
}
}
时间复杂度 o(n) 空间复杂度o(n + 递归的n)
class Solution {
int maxValue = 0;
public int getMinimumDifference(TreeNode root) {
List<Integer> list = new ArrayList<>();
tranvsal(root, list);
int min = Integer.MAX_VALUE;
int maxIndex = 0;
for (int i = 0; i < list.size(); i++) {
int value = list.get(i);
for (int j = 0; j < list.size(); j++) {
if(i != j) {
min = Math.min(min, Math.abs(value - list.get(j)));
}
}
}
return min;
}
private void tranvsal(TreeNode root, List<Integer> list) {
if (root == null) {
return;
}
maxValue = Math.max(root.val, maxValue);
list.add(root.val);
if (root.left != null) {
tranvsal(root.left, list);
}
if (root.right != null) {
tranvsal(root.right, list);
}
}
}
class Solution {
int pre = 0;
int min = 0;
public int getMinimumDifference(TreeNode root) {
pre = -1;
min = Integer.MAX_VALUE;
tranvsal(root);
return min;
}
private void tranvsal(TreeNode root) {
if (root == null) {
return;
}
if (root.left != null) {
tranvsal(root.left);
}
if (pre == -1) {
pre = root.val;
} else {
//这里不用abs,需要自己再想想为什么
min = Math.min(min, root.val - pre);
pre = root.val;
}
if (root.right != null) {
tranvsal(root.right);
}
}
}
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(slow == fast) {
return true;
}
}
return false;
}
}
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode next = head.next;
head.next = swapPairs(next.next);
next.next = head;
return next;
}
}
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyNode = new ListNode(0);
ListNode temp = dummyNode;
temp.next = head;
while (temp != null && temp.next != null && temp.next.next != null) {
ListNode node1 = temp.next;
ListNode node2 = temp.next.next;
temp.next = node2;
node1.next = node2.next;
node2.next = node1;
temp = node1;
}
return dummyNode.next;
}
}
class Solution {
public Node connect(Node root) {
if(root == null) {
return null;
}
LinkedList<Node> queue = new LinkedList<Node>();
queue.push(root);
while(!queue.isEmpty()) {
int size = queue.size();
while(size > 0) {
Node node = queue.removeLast();
if(node.left != null) {
queue.addFirst(node.left);
}
if(node.right != null) {
queue.addFirst(node.right);
}
if(size > 1) {
node.next = queue.peekLast();
}
size--;
}
}
return root;
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.