剑指offer系列0:替换空格&从头到尾打印链表

这是最前面的几个题,我之前写在GitHub上了,现在为了统一补回来。

面试题4:替换空格

字符串的名字是一个指针变量,里面存的内容是其第一个字符的地址。如字符串a[10],则&a,a得到的均相同,都是a数组的第一个元素的地址。

 1 #include<iostream>
 2 using namespace std;
 3 class Solution {
 4 public:
 5     void replaceSpace(char *str, int length)
 6     {
 7         if (str == NULL || length <= 0)
 8         {
 9             return;
10         }
11          int count = 0,mylength=0;
12         for (int i = 0; i < length; i++)
13         {
14             if (str[i] != '\0')
15             {
16                 mylength++;
17             }
18         }
19         for (int i = 0; i < mylength; i++)
20         {
21             if (str[i] == ' ')
22             {
23                 count++;
24             }
25         }
26         int newLen = mylength + 2 *count;
27         char* newPtr;                                 //新建一个指向扩展后的字符串末位的指针
28         newPtr = str + newLen;
29         *newPtr = '\0';
30         newPtr--;
31         //newLen -= 1;
32 
33         for (int i = mylength - 1; i >= 0 && newLen != i; --i)
34         {
35             if (str[i] == ' ')
36             {
37                 *(newPtr--) = '0';
38                 *(newPtr--) = '2';
39                 *(newPtr--) = '%';
40                 newLen -= 3;
41             }
42             else
43             {
44                 *(newPtr--) = str[i];
45                 newLen -= 1;
46             }
47         }
48         return;
49     }
50 };
51 int main()
52 {
53     Solution a;
54     char as[20] = "hello world";
55     a.replaceSpace(as, 20);
56     cout << as<<endl ;
57     return 058  }

面试题5:从头到尾打印链表

 1 #include<iostream>
 2 #include<vector>
 3 #include<stack>
 4 using namespace std;
 5 struct ListNode {
 6 public:
 7        int val;
 8        struct ListNode *next;
 9        /*
10        ListNode(int x) :
11               val(x), next(NULL) {
12        }  */
13  };
14 class Solution {
15 public:
16     vector<int> printListFromTailToHead(struct ListNode* head)
17     {
18         stack<int> st;
19         ListNode *cur = head;
20         while (cur!= NULL)                                                    //压栈
21         {
22             //cout << cur->val << "in stack" << endl;
23             st.push(cur->val);
24             cur = cur->next;
25         }
26         int len = st.size();
27         //cout << "len=" << len << endl;
28         vector<int> res(len);
29         for (int i = 0; i < len && st.empty()!=true; i++)                    //从栈顶取值即可
30         {
31             //cout << st.top() << "in vector" << endl;
32             res[i] = st.top();
33             st.pop();
34         }
35         return res;
36     }
37 };
38 int main()
39 {
40     ListNode list[4];
41     list[0].val = 0;
42     list[0].next = &list[1];
43     list[1].val = 1;
44     list[1].next = &list[2];
45     list[2].val = 2;
46     list[2].next =&list[3];
47     list[3].val= 3;
48     list[3].next = NULL;
49 
50     Solution so;
51     vector<int> r = so.printListFromTailToHead(list);
52     for (int i = 0; i < r.size(); i++)
53     {
54         cout << r[i] << endl;
55     }
56     return 0;
57 }
58 //难度不大,细节东西别错

 

posted @ 2019-08-15 14:53  妮妮熊  阅读(171)  评论(0编辑  收藏  举报