看背包问题 复习了下leetcode 的72 动态规划

class Solution {
public:
int minDistance(string word1, string word2) {
int n1 = word1.length();
int n2 = word2.length();
vector<vector <int>> result(n1 + 1, vector<int> (n2 + 1, 0));//bug处, 最开始没有初始化 疑问 vector没有初始化处bug的原因
for(int i = 0; i != n1 + 1; ++i)
result[i][0] = i;
for(int i = 0; i != n2 + 1; ++i)
result[0][i] = i;
for(int i = 1; i != n1 + 1; ++i)
for(int j = 1; j != n2 + 1; ++j) {
if(word1[i - 1] == word2[j - 1])
result[i][j] = result[i - 1][j - 1];
else
result[i][j] = min(result[i][j - 1], min(result[i - 1][j], result[i - 1][j - 1])) + 1;
}
return result[n1][n2];
}
};

206翻转链表

数据结构第三章基础题 不过忘了 原理就是保留前中后三个指针 不断逐个变换 

这是递归解法

struct ListNode* reverseList(struct ListNode* head) {
if(!head || !(head -> next)) return head;
struct ListNode* p = head -> next;
head -> next = NULL;
struct ListNode* pw = reverseList(p);  //一直没想出传入函数的p就代表前一个pre对象 一直纠结怎么构造pre
p -> next = head;                //只能改变p 不能改变pw pw是新的头
return pw;                                         //只能返回pw 而不是p 
}

posted on 2017-09-23 01:36  bloomingFlower  阅读(69)  评论(0编辑  收藏  举报