leetcode.docx
《leetcode.docx》由会员分享,可在线阅读,更多相关《leetcode.docx(19页珍藏版)》请在冰豆网上搜索。
leetcode
packagestudy;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Stack;
//实现二叉树的前序遍历
publicclassBinaryTreePreorderTraversal{
publicListpreorderTraveral(TreeNoderoot){
Stackstack=newStack();
Listpreorder=newArrayList();
if(root==null){
returnpreorder;
}
stack.push(root);
while(!
stack.empty()){
TreeNodenode=stack.pop();
preorder.add(node.val);
if(node.right!
=null){
stack.push(node.right);
}
if(node.left!
=null){
stack.push(node.right);
}
}
returnpreorder;
}
}
packagestudy;
importjava.util.ArrayList;
importjava.util.Stack;
/**
*Definitionforbinarytree
*publicclassTreeNode{
*intval;
*TreeNodeleft;
*TreeNoderight;
*TreeNode(intx){val=x;}
*实现而二叉树的后序遍历
*}
*/
classTreeNode{
intval;
TreeNodeleft;
TreeNoderight;
TreeNode(intx){val=x;}
}
publicclassBinaryTreePostorderTraversal{
publicstaticArrayListPostorderTraversal(TreeNoderoot){
ArrayListresult=newArrayList();
Stackstack=newStack();
TreeNodeprev=null;
TreeNodecurr=root;
if(root==null){
returnresult;
}
stack.push(root);
while(!
stack.empty()){
//查看栈顶对象,并赋值给curr
curr=stack.peek();
if(prev==null||prev.left==curr||prev.right==curr){//traversedownthetree
//一直找到最坐子节点为止
if(curr.left!
=null){
stack.push(curr.left);
}elseif(curr.right!
=null){
stack.push(curr.right);
}
}elseif(curr.left==prev){//traverseupthetreefromtheleft
if(curr.right!
=null){
stack.push(curr.right);
}
}else{//traverseupthetreefromtheright
result.add(curr.val);
stack.pop();
}
prev=curr;
}
returnresult;
}
}
packagestudy;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.Stack;
publicclassEvaluateReversePolishNotation{
/**
*@paramargs
*逆波兰式
*/
publicstaticintevalRPN(String[]tokens){
Stacks=newStack();
//把操作符放在operators里面去
Stringoperators="+-*/";
for(Stringtoken:
tokens){
if(!
operators.contains(token)){
s.push(Integer.valueOf(token));
continue;
}
inta=s.pop();
intb=s.pop();
if(token.equals("+")){
s.push(a+b);
}
elseif(token.equals("-")){
s.push(a-b);
}elseif(token.equals("*")){
s.push(a*b);
}elseif(token.equals("/")){
s.push(a/b);
}
}
returns.pop();
}
publicstaticvoidmain(String[]args)throwsIOException{
//TODOAuto-generatedmethodstub
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
Stringinput=br.readLine();
String[]s=newString[20];
s=input.split("");
System.out.println(evalRPN(s));
}
}
packagestudy;
publicclassInsertionSortList{
/**
*@paramargs
*插入排序
*/
publicstaticListNodeinsertionSortList(ListNodehead){
ListNodedummy=newListNode(0);
while(head!
=null){
ListNodenode=dummy;
while(node.next!
=null&&node.next.val
node=node.next;
}
//画图可知道
ListNodetemp=head.next;
head.next=node.next;
node.next=head;
head=temp;
}
returndummy.next;
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
ListNodelist1=newListNode(15);
ListNodelist2=newListNode(22);
list1.next=list2;
ListNodelist3=newListNode(33);
list2.next=list3;
ListNodelist4=newListNode(55);
list3.next=list4;
System.out.println(insertionSortList(list1).next.val);
}
}
classListNode2{
intval;
ListNode2next;
ListNode2(intx){
val=x;
next=null;
}
}
packagestudy;
importjava.util.HashMap;
publicclassLRU_Cache{
/**
*
*@paramargs
*/
privateclassNode{
//这是一个双向链表
Nodeprev;
Nodenext;
intkey;
intvalue;
publicNode(intkey,intvalue){
this.key=key;
this.value=value;
this.prev=null;
this.next=null;
}
}
privateintcapacity;
privateHashMaphs=newHashMap();
privateNodehead=newNode(-1,-1);
privateNodetail=newNode(-1,-1);
//有参构造方法
publicLRU_Cache(intcapacity){
this.capacity=capacity;
tail.prev=head;
head.next=tail;
}
publicintget(intkey){
if(!
hs.containsKey(key)){
return-1;
}
Nodecurrent=hs.get(key);
//把current移除
current.prev.next=current.next;
current.next.prev=current.prev;
//把current移动到末尾位置
move_to_tail(current);
returnhs.get(key).value;
}
publicvoidset(intkey,intvalue){
//如果你复制的的key已经存在了,那么重新给vlaue赋值,直接返回就行
if(get(key)!
=-1){
hs.get(key).value=value;
return;
}
//如果这个时候内存满了的话,就是hs满了的话,
if(hs.size()==capacity){
hs.remove(head.next.key);
head.next=head.next.next;
head.next.prev=head;
}
Nodeinsert=newNode(key,value);
hs.put(key,insert);
move_to_tail(insert);
}
publicvoidmove_to_tail(Nodecurrent){
//找到倒数第二个,让current的前驱指向原先的倒数第二个
current.prev=tail.prev;
//让倒数第一个前驱指向current
tail.prev=current;
//让current的的后继指向tail
current.next=tail;
//让原先的倒数第二个的后继指向current
current.prev.next=current;
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
}
}
packagestudy;
importjava.util.HashMap;
publicclassMaxPointsonaLine{
/**
*@paramargs
*/
publicstaticintmaxPoints(Point[]points){
if(points==null||points.length==0){
return0;
}
//注意key在这个面当作是斜率,而value的值是该斜率的个数
HashMapmap=newHashMap();
//注意max是全局变量
intmax=1;
for(inti=0;i//清除map
map.clear();
//初始化map,放入最小值
map.put((double)Integer.MIN_VALUE,1);
//初始化dup,有几个大小相同的点,大小相同了,斜率肯定也是相同的了
//注意dup是局部变量
intdup=0;
for(intj=i+1;jif(points[j].x==points[i].x&&points[j].y==points[i].x){
dup++;
continue;
}
//如果这两个点只是x的值相等但是y值不等,那么让key值等integer.max_value
doublekey=points[j].x-points[i].x==0?
Integer.MAX_VALUE:
0.0+(double)(points[j].y-points[i].y)/(double)(points[j].x-points[i].x);
if(map.containsKey(key)){
map.put(key,map.get(key)+1);
}else{
//这一步是怎么回事啊,为什么要放如2呢?
//第一次开始的时候,斜率k值还不存在,上面的式子说明存在连个点斜率相同,讲k值设置为2,如果是上面的式子中
//的k值没有相同的也没有关系,我把k值设置为了最大值integer。
maxint
map.put(key,2);
}
}
for(inttemp:
map.values()){
//可能存在重复
if(temp+dup>max){
max=temp+dup;
}
}
}
returnmax;
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Pointpoint1=newPoint(4,4);
Pointpoint2=newPoint(4,2);
Pointpoint3=newPoint(6,3);
Pointpoint4=newPoint(2,1);
Pointpoint5=newPoint(10,5);
Point[]s=newPoint[5];
s[0]=point1;
s[1]=point2;
s[2]=point3;
s[3]=point4;
s[4]=point5;
System.out.println(maxPoints(s));
}
}
classPoint{
intx;
inty;
Point(){x=0;y=0;}
Point(inta,intb){x=a;y=b;}
}
/*
*其实就是列表进行重新排序,显示分为两半,再将这两个列表进行重新排列,确定下一个,
*重新排序完成后,一定记着或许会留下来一个,因为可能这两半中有一个要比另一个大
*/
packagestudy;
publicclassReorderList{
publicListNode2reverse(ListNode2head){
ListNode2newHead=null;
while(head!
=null){
//得到最后的尾巴的值赋值为newHead
ListNode2temp=head.next;
head.next=newHead;
newHead=head;
head=temp;
//在这个里面就不需要再去对newHead的next进行赋值,因为么人个里面的dummy会做这件事
}
returnnewHead;
}
privatevoidmerage(ListNode2head1,ListNode2head2){
intindex=0;
ListNode2dummy=newListNode2(0);
while(head1!
=null&&head2!
=null){
if(index%2==0){
dummy.next=head1;
head1=head1.next;
}else{
dummy.next=head2;
head2=head2.next;
}
dummy=dummy.next;
index++;
}
if(head1!
=null){
dummy.next=head1;
}else{
dummy.next=head2;
}
}
privateListNode2findMid(ListNode2head){
ListNode2fast=head,slow=head;
while(fast!
=null&&fast.next!
=null){
fast=fast.next.next;
slow=slow.next;
}
returnslow;
}
publicvoidreorderList(ListNode2head){
if(head==null||head.next==null){
return;
}
ListNode2mid=findMid(head);
//注意这里是将后半部分进行反转;
ListNode2tail=reverse(mid.next);
mid.next=null;
merage(head,tail);
}
}
packagestudy;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
publicclassReverseWordsInAString{
publicstaticStringreverseWords(Strings){
if(s==null||s.length()==0){
return"";
}
String[]array=s.split("");
StringBuildersb=newStringBuilder();
for(inti=array.length-1;i>=0;--i){
if(!
array[i].equals("")){
sb.append(array[i]).append("");
}
}
returnsb.length()==0?
"":
sb.substring(0,sb.length()-1);
}
publicstaticvoidmain(Stringargs[])throwsIOException{
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
Stringinput=br.readLine();
Stringn=reverseWords(input);
System.out.println(n);
}
}
packagestudy;
publicclassSortList{
/**
*@paramargs
*/
privatestaticListNodefindMiddle(ListNodehead){
//找到中间节点,归并排序的第一步
ListNodeslow=head,fast=head.next;
while(fast!
=null&&fast.next!
=null){
//fast总是以比slow快2倍的速度去向下查找,这样就可以查找到中间节点
fast=fast.next.next;
slow=slow.next;
}
returnhead;
}
//归并排序第二步
privatestaticListNodemerge(ListNodehead1,ListNodehead2){
ListNodedummy=newListNode(0);
ListNodetail=dummy;
//建造一个类似与过渡变量的tail
while(head1!
=null&&head2!
=null){
if(head1.val//先对tail赋值给head1或者head2中比较小的节点
tail.next=head1;
head1=head1.next;
}else{
//在对head1和head2的next赋值给head1和head2,方便下次循环的比较
tail.next=head2;
head2=head1.next;
}
//最后让中间过渡变量留下原先的,继续做为下一次循环时使用的中间过渡变量