随笔分类 - 剑指offer
摘要:1. 利用栈:后进先出 将链表从头到尾压入栈中,再从栈中pop出来,对链表从头到尾赋值。 1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : 6 val(x), next(NULL) { 7
阅读全文
摘要:1. 递归 可过,耗时长。 1 class Solution { 2 public: 3 int jumpFloor(int number) { 4 if(number==0) 5 return 0; 6 if(number==1) 7 return 1; 8 if(number==2) 9 ret
阅读全文
摘要:1. 递归 过不了!超时! 1 class Solution { 2 public: 3 int Fibonacci(int n) { 4 if(n==0) 5 return 0; 6 if(n==1) 7 return 1; 8 if(n==2) 9 return 1; 10 if(n>2) 11
阅读全文
摘要:思路: 栈——LIFO(后进先出);队列——FIFO(先进先出)。 用两个栈实现队列的根本点是要保证最先进来的(最老的)元素出栈时处于栈顶位置。 使用栈1来入栈,栈2来出栈。具体流程如下: 1. 入栈操作:用栈1入栈,直接push到栈1顶部;(元素全部在栈1,栈2为空) 2. 出栈操作:先将栈1中的
阅读全文
摘要:思路: 统计空格数,算出替换后串的大小,从最后一个开始往后移动,遇到空格,替换为"%20"。 (在串的最后面设置结束符'\0') 1 class Solution { 2 public: 3 void replaceSpace(char *str,int length) { 4 int len_st
阅读全文
摘要:1. 使用栈 1 class Solution { 2 public: 3 vector<int> printListFromTailToHead(ListNode* head) { 4 vector<int> ArrayList; 5 stack<int> s; 6 ListNode* p=hea
阅读全文

浙公网安备 33010602011771号