您的位置:首页 > Web前端

剑指offer(26-30)编程题

2017-03-30 21:24 204 查看
二叉搜索树与双向链表

字符串的排列

数组中出现次数超过一半的数字

最小的K个数

连续子数组的最大和

26.输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的

1 /*
2 struct TreeNode {
3     int val;
4     struct TreeNode *left;
5     struct TreeNode *right;
6     TreeNode(int x) :
7             val(x), left(NULL), right(NULL) {
8     }
9 };*/
10 class Solution {
11 public:
12     vector<TreeNode*> doConvert(TreeNode* pRootOfTree) {
13         vector<TreeNode*> res(2);
14         if (pRootOfTree->left == nullptr) {
15             res[0] = pRootOfTree;
16         } else {
17             vector<TreeNode*> leftRes = doConvert(pRootOfTree->left);
18             res[0] = leftRes[0];
19             leftRes[1]->right = pRootOfTree;
20             pRootOfTree->left = leftRes[1];
21         }
22
23         if (pRootOfTree->right == nullptr) {
24             res[1] = pRootOfTree;
25         } else {
26             vector<TreeNode*> rightRes = doConvert(pRootOfTree->right);
27             res[1] = rightRes[1];
28             pRootOfTree->right = rightRes[0];
29             rightRes[0]->left = pRootOfTree;
30         }
31         return res;
32     }
33
34     TreeNode* Convert(TreeNode* pRootOfTree) {
35         if(pRootOfTree == nullptr) return nullptr;
36         vector<TreeNode* >res =  doConvert(pRootOfTree);
37         return res[0];
38     }
39
40 };


27.输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba

class Solution {
private:
void dfs(string str, int k, vector<string>& res) {
if (k == str.size() - 1) {
res.push_back(str);
return;
}

unordered_set<int> visited;
sort(str.begin()+k,str.end());
for (size_t i = k; i < str.size(); i++) {
if (visited.find(str[i]) == visited.end()) {
visited.insert(str[i]);
swap(str[k], str[i]);
dfs(str, k + 1, res);
swap(str[k], str[i]);
}
}
}
public:
vector<string> Permutation(string str) {
vector<string> res;
dfs(str, 0, res);
return res;
}
};


28.数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。

class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
int n = numbers.size();
if(n == 0) return 0;

int num = numbers[0],count = 1;
for(int i=1;i<n;i++){
if(numbers[i] == num) count++;
else count--;
if(count == 0){
num = numbers[i];
count = 1;
}
}
//verify
count = 0;
for(int i=0;i<n;i++){
if(numbers[i] == num) count++;
}
if(count*2 > n ) return num;
else return 0;  // not exist

}
};


29.输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。solution1在牛客网上超时,solution2能正常通过。

1 class Solution1 {
2 private:
3     int partition(vector<int>& input, int left, int right) {
4         if (left >= right)
5             return left;
6         int pivotVal = input[left];
7         while (left < right) {
8             while (left < right && input[right] >= pivotVal) {
9                 right--;
10             }
11             input[left] = input[right];
12             while (left < right && input[left] <= pivotVal) {
13                 left++;
14             }
15             input[right] = input[left];
16         }
17         input[left] = pivotVal;
18         return left;
19     }
20 public:
21     vector<int> GetLeastNumbers_Solution(vector<int>& input, int k) {
22         vector<int> res;
23         int n = input.size();
24
25         int index = partition(input, 0, n - 1);
26         while (index != k - 1) {
27             if (index > k - 1) {
28                 index = partition(input, 0, index - 1);
29             } else {
30                 index = partition(input, index + 1, n - 1);
31             }
32         }
33         for (int i = 0; i < k; i++) {
34             res.push_back(input[i]);
35         }
36         return res;
37     }
38 };
39
40 class Solution2 {
41 public:
42     vector<int> GetLeastNumbers_Solution(vector<int>& input, int k) {
43         int n = input.size();
44         if (n == 0 || n < k)
45             return vector<int>();
46         multiset<int, greater<int>> leastNums(input.begin(), input.begin() + k);
47
48         for (int i = k; i < n; i++) {
49             if (input[i] < *leastNums.begin()) {
50                 leastNums.erase(leastNums.begin());
51                 leastNums.insert(input[i]);
52             }
53         }
54         return vector<int>(leastNums.begin(), leastNums.end());
55     }
56 };


30HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?(子向量的长度至少是1)

class Solution {
public:
int FindGreatestSumOfSubArray(vector<int>& array) {
int n = array.size();
if (n == 0) return 0;

int* dp = new int
;
dp[0] = array[0];
int maxSum = dp[0];
for (int i = 1; i < n; i++) {
dp[i] = max(dp[i - 1] + array[i], array[i]);
if (dp[i] > maxSum) {
maxSum = dp[i];
}
}
delete [] dp;
return maxSum;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: