京东编程:找幸运数
#include <iostream> using namespace std; int f(int x) { int sum = 0; while (x != 0) { sum += x % 10; x /= 10; } return sum; } int g(int x) { int sum = 0; int flag = 1; while (flag) { if (flag&x) sum++; flag = flag << 1; } return sum; } int main() { int n; cin >> n; int *p = new int[n]; for (int i = 0; i < n; i++) { cin >> p[i]; } for (int i = 0; i < n; i++) { int count = 0; for (int j = 1; j <= p[i]; j++) { if (f(j) == g(j)) count++; } cout << count << endl; } system("pause"); return 0; }
无序数组中找出和为N的两个数 Two Sum
Hash方法:可以考虑使用hash。将数组的每个元素以元素值位key,下标为value存入hash表。然后第二次遍历时,在hash中查询target做减法后的差值,hash表中存在该差值就算找到了。注意一点:当数组中存在重复元素时,注意hash表该如何查找;两个数不能为同一个数。
1 class Solution { 2 public: 3 vector<int> twoSum(vector<int> &numbers, int target) { 4 unordered_map<int, int> hashtable; 5 vector<int> result; 6 int n = numbers.size(); 7 for(int i=0;i<n;i++) 8 hashtable[numbers[i]] = i+1; 9 10 for(int i=0;i<n;i++) 11 { 12 int other = target - numbers[i]; 13 if(hashtable.find(other) != hashtable.end()) 14 { 15 int j = hashtable[other]; 16 if(i+1 == j) 17 continue; 18 result.push_back(i+1); 19 result.push_back(j); 20 break; 21 } 22 } 23 return result; 24 } 25 };
滴滴
二叉搜索树合法性,leetcode:http://www.nowcoder.com/practice/fd7f880072914464a13b89af242c0ce5?tpId=46&tqId=29080&rp=3&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking
中序,判断是否递增,不要用递归,容易出错
class Solution { public: void inOrderTree(TreeNode *root,vector<int> &vec) { if(root == NULL) return; inOrderTree(root->left,vec); vec.push_back(root->val); inOrderTree(root->right,vec); } bool isValidBST(TreeNode *root) { if(root == NULL) return true; vector<int> vec; inOrderTree(root,vec); int len = vec.size(); if(len == 1) return true; for(int i = 0; i < len - 1;i++) { if(vec[i] >= vec[i+1]) return false; } return true; } };
滴滴
合并两个无序数组到有序,并去重
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <functional> using namespace std; int partition(int *array, int low, int high) { int pivot = array[low]; while (low < high) { while (low < high && array[high] >= pivot) high--; swap(array[low], array[high]); while (low < high && array[low] <= pivot) low++; swap(array[low], array[high]); } return low; } void QuickSort(int *nums, int low, int high) { int index; if (low < high) { index = partition(nums, low, high); QuickSort(nums, low, index - 1); QuickSort(nums, index + 1, high); } } vector<int> mergeArray(int *a, int len_a, int *b, int len_b) { QuickSort(a, 0, len_a - 1); QuickSort(b, 0, len_b - 1); vector<int> result; int i = 0; int j = 0; while(i < len_a && j < len_b) { if (a[i] < a[j]) { result.push_back(a[i]); i++; } else if (a[i] > a[j]) { result.push_back(a[j]); j++; } else { int tmp = a[i]; //处理重复元素 result.push_back(tmp); i++; j++; while (i < len_a && a[i] == tmp) i++; while (j < len_b && b[j] == tmp) j++; } } while (i < len_a) { result.push_back(a[i]); i++; } while (j < len_b) { result.push_back(b[j]); j++; } return result; } int main() { int a[] = { 3,2,1}; int b[] = { 3, 33, 2, 1 }; vector<int> vec = mergeArray(a, 3, b, 4); system("pause"); return 0; }
中兴,用两个线程,从1+100
#include <iostream> #include <vector> using namespace std; #include <thread> void fun1(int &sum) { for (int i = 1; i <= 50; i++) sum += i; } void fun2(int &sum) { for (int i = 51; i <= 100; i++) sum += i; } int main() { int sum1 = 0; int sum2 = 0; thread t1(fun1, ref(sum1)); thread t2(fun2, ref(sum2)); t1.join(); t2.join(); cout << sum1 + sum2 << endl; system("pause"); return 0; }