今日题目
1、反转链表
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode pre=null;
ListNode cur=head;
while(cur!=null){
ListNode cnext=cur.next;
cur.next=pre;
pre=cur;
cur=cnext;
}
return pre;
}
}
2、设计LRU缓存结构
import java.util.*;
public class Solution {
/**
* lru design
* @param operators int整型二维数组 the ops
* @param k int整型 the k
* @return int整型一维数组
*/
LinkedList<Integer> linkedList=new LinkedList<>();
HashMap<Integer,Integer> map=new HashMap<>();
public int[] LRU (int[][] operators, int k) {
// write code here
ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<operators.length;i++){
if(operators[i][0]==1){//set操作
int key=operators[i][1];
int value=operators[i][2];
set(key,value,k);
}
if(operators[i][0]==2){//get操作
int key=operators[i][1];
list.add(get(key));
}
}
int[] res=new int[list.size()];
for(int i=0;i<list.size();i++){
res[i]=list.get(i);
}
return res;
}
public int get(int key){
if(map.containsKey(key)){
linkedList.remove(new Integer(key));
linkedList.addFirst(key);
return map.get(key);
}else{
return -1;
}
}
public void set(int key,int value,int k){
if(map.containsKey(key)){
linkedList.remove(new Integer(key));
linkedList.addFirst(key);
map.put(key,value);
}
linkedList.addFirst(key);
map.put(key,value);
//缓存不足
if(linkedList.size()>k){
int temp=linkedList.removeLast();
map.remove(temp);
}
}
}
3、判断链表有没有环
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast){
return true;
}
}
return false;
}
}
4、二叉树先序,中序和后序遍历
/**
* 中序遍历
* 左-根-右
*/
public class InOrder {
public static List<Integer> inOrder(TreeNode root){
ArrayList<Integer> res=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
TreeNode curr=root;
while(curr!=null||!stack.isEmpty()){
while (curr!=null){
//一直往左压栈
stack.push(curr);
curr=curr.left;
}
curr=stack.pop();
res.add(curr.val);
curr=curr.right;
}
return res;
}
}
/**
* 先序遍历
* 根-左-右
*/
public class PreOrder {
public static List<Integer> preOrder(TreeNode root){
ArrayList<Integer> res=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
if(root==null){
return res;
}
stack.push(root);
while(!stack.isEmpty()){
TreeNode node=stack.pop();
res.add(node.val);
if(node.right!=null) stack.push(node.right);
if(node.left!=null) stack.push(node.left);
}
return res;
}
}
/**
* 后序遍历
* 思路:逆前序遍历结果的逆序
* 取巧的方法。该写法的访问顺序并不是后序遍历,
* 而是利用先序遍历“根左右”的遍历顺序,
* 将先序遍历顺序更改为“根右左”,反转结果List,得到结果顺序为“左右根”。
*/
public class PostOrder {
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> res=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
if(root==null){
return res;
}
stack.push(root);
while(!stack.empty()){
TreeNode node=stack.pop();
if(node.left!=null) stack.push(node.left);
if(node.right!=null) stack.push(node.right);
res.add(0,node.val);//逆序添加
}
return res;
}
}
5、二分查找数组中第一个大于等于查找值的位置
import java.util.*;
public class Solution {
/**
* 二分查找
* @param n int整型 数组长度
* @param v int整型 查找值
* @param a int整型一维数组 有序数组
* @return int整型
*/
public int upper_bound_ (int n, int v, int[] a) {
// write code here
int left=0;
int right=n-1;
while(left<right){
int mid=(left+right)/2;
if(a[mid]>=v){
if(mid==0||a[mid-1]<v){
return mid+1;
}else{
right=mid-1;
}
}else{
left=mid+1;
}
}
return n+1;
}
}