leetcode一些常用函数
6移位操作
“>> 右移,高位补符号位” 这里右移一位表示除2
“>>> 无符号右移,高位补0”; 与>>类似
“<< 左移” 左移一位表示乘2,二位就表示4,就是2的n次方
6树的各种操作
import java.util.ArrayDeque;
import java.util.Queue;
public class Solution{
public static void main(String[] args){
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = null;
root.left.right =null;
root.right.left = null;
root.right.right =null;
levelOrder(root);
}
//树的遍历
//先序遍历
public static void preOrder(TreeNode root){
if(root == null) return;
System.out.println(root.val);
preOrder(root.left);
preOrder(root.right);
}
//层次遍历
public static void levelOrder(TreeNode root){
Queue<TreeNode> q = new ArrayDeque<TreeNode>();
q.add(root);
while( !q.isEmpty()){
TreeNode tmp = q.poll();
System.out.println(tmp.val);
if(tmp.left != null ) q.add(tmp.left);
if(tmp.right != null ) q.add(tmp.right);
}
}
//树的高度
public static int treeHeight(TreeNode root){
int high = 0;
if(root == null) return 0;
high = 1 + Math.max(treeHeight(root.left), treeHeight(root.right));
return high;
}
//二叉排序树
public static boolean isBST(TreeNode root, int left, int right){
if(root == null) return true;
return (left < root.val && root.val < right && isBST(root.left,left ,root.val) && isBST(root.right,root.val,right));
}
public static boolean isBST(TreeNode root){
return isBST(root,Integer.MIN_VALUE, Integer.MAX_VALUE);
}
}
class TreeNode{
TreeNode left;
TreeNode right;
int val;
TreeNode(int x){
val = x;
}
}
5快速排序
public static void qSort(int[] a, int low, int high){
if(low >= high) return;
int first = low, last = high, key = a[first];
while(first < last){
while(first < last && a[last] >= key) --last;
a[first] = a[last];
while(first < last && a[first] <= key) ++first;
a[last] = a[first];
}
a[first] = key;
qSort(a, low, first - 1);
qSort(a, last + 1, high);
}
4,归并排序(背下来)。
public static void mergeSort(int[] a, int[] temp, int left, int right){
if(left >= right) return;
int mid = (left + right) / 2;
mergeSort(a, temp, left, mid);
mergeSort(a, temp, mid+1, right);
int p_final = left, p_left = left, p_right = mid + 1;
while(p_left <= mid || p_right <= right){
if(p_left <= mid && (p_right >right || a[p_left] <= a[p_right])){
temp[p_final++] = a[p_left++];
}
else{
temp[p_final++] = a[p_right++];
}
}
for ( int i = left; i <= right; i++){
a[i] = temp[i];
}
}
3,二分函数(背下来)。
public static int binSearch(int[] a, int key){
int left = 0;
int right = a.length - 1;
while(left <= right){
int mid = (left + right) / 2;
if(key < a[mid]){
right = mid -1;
}
else if (key > a[mid]){
left = mid + 1;
}
else{
return mid;
}
}
return -1;
}
1,java的swap函数。交换
public static void swap ( int [] data, int a, int b) {
int t = data [a];
data [a] = data [b];
data [b] = t;
}
2,数组翻转函数(reverse)
2,向右移动数组k位(rotate array);
public class Solution {
public void rotate(int[] nums, int k) {
if(nums.length <= 1){
return;
}
//step each time to move
int step = k % nums.length;
reverse(nums,0,nums.length - 1);//放到最后就是向左移
reverse(nums,step,nums.length - 1);
reverse(nums,0,step - 1);
}
//reverse int array from n to m
public void reverse(int[] nums, int n, int m){
while(n < m){
nums[n] ^= nums[m];
nums[m] ^= nums[n];
nums[n] ^= nums[m];
n++;
m--;
}
}
}
3,字符串转成字符数组
s.toCharArray();
方法1:直接在构造String时转换。
char[] data = {'a', 'b', 'c'};
String str = new String(data);
方法2:调用String类的方法转换。
String.valueOf(char[] ch)
4,字符串翻转(String reverse)
new String
----------整数数组转化成字符串
Arrays.toString(a)