反轉鏈表java
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head == null) {
return null;
}
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}
複製代碼
合併兩個有序鏈表node
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if (list1 == null && list2 == null) {
return null;
}
ListNode curr = new ListNode(0);
ListNode dummy = curr;
while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
curr.next = list1;
list1 = list1.next;
}
else {
curr.next = list2;
list2 = list2.next;
}
curr = curr.next;
}
if (list1 == null) {
curr.next = list2;
}
if (list2 == null) {
curr.next = list1;
}
return dummy.next;
}
}
複製代碼
兩個棧實現隊列數組
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if (!stack2.isEmpty()) {
return stack2.pop();
}
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
if (!stack2.isEmpty()) {
return stack2.pop();
}
return -1;
}
}
複製代碼
前序和中序重建二叉樹函數
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if (pre == null || in == null || pre.length == 0 || in.length == 0) {
return null;
}
int n = pre.length;
TreeNode root = helper(pre, in, 0, n - 1, 0, n - 1);
return root;
}
private TreeNode helper(int[] pre, int[] in, int preStart, int preEnd, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) {
return null;
}
TreeNode root = new TreeNode(pre[preStart]);
int val = root.val;
int index = 0;
for (int i = inStart; i <= inEnd; i++) {
if (in[i] == val) {
index = i;
}
}
root.left = helper(pre, in, preStart + 1, preEnd, inStart, index - 1);
root.right = helper(pre, in, preStart + (index - inStart) + 1, preEnd, index + 1, inEnd);
return root;
}
}
複製代碼
斐波那契數列spa
public class Solution {
public int Fibonacci(int n) {
if (n <= 1) {
return n;
}
int fib = 0;
int lastfib = 1;
int secfib = 0;
int count = 1;
while (count < n) {
fib = lastfib + secfib;
secfib = lastfib;
lastfib = fib;
count++;
}
return fib;
}
}
複製代碼
跳臺階3d
public class Solution {
public int JumpFloor(int target) {
if (target == 1) {
return 1;
}
if (target == 2) {
return 2;
}
int oneStepBefore = 2;
int twoStepsBefore = 1;
int total = 0;
for (int i = 3; i <= target; i++) {
total = oneStepBefore + twoStepsBefore;
twoStepsBefore = oneStepBefore;
oneStepBefore = total;
}
return total;
}
}
複製代碼
矩形覆蓋指針
public class Solution {
public int RectCover(int target) {
if (target < 1) {
return 0;
} else if (target == 1 || target == 2) {
return target;
} else {
return RectCover(target-1) + RectCover(target-2);
}
}
}
複製代碼
二進制中1的個數code
public class Solution {
public int NumberOf1(int n) {
int count = 0;
for (int i = 0; i < 32; i++) {
count += (n >> i) ^ 1; // 這裏是 用的'與'操做 必須是1 & 1 ==> 1
}
return count;
}
}
複製代碼
數值的整數次方cdn
public class Solution {
public double Power(double base, int n) {
double res = 1,curr = base;
int exponent;
if(n > 0){
exponent = n;
}else if(n < 0){
if(base == 0)
throw new RuntimeException("分母不能爲0");
exponent = -n;
curr = 1 / curr;
}else{// n==0
return 1;// 0的0次方
}
while(exponent != 0){
if((exponent & 1) == 1)
res *= curr;
curr* = curr;// 翻倍
exponent >>= 1;// 右移一位 等價於 exponent /= 2
}
return res;
}
}
複製代碼
同向雙指針模板 ? 調整數組順序使奇數位於偶數前面blog
複製代碼