文章分类 - amazon
摘要://Delete a specific char from string,,,,char is 'F' in string= "YOUSUF".the resultant string will be "YOUSU".#include #include #include using namespace std;void DeleteCharFromString(string* str, char c) { if (!str || str->empty()) { throw exception(); } int pos = 0; in
阅读全文
摘要://example:Str="4142434546" Findout missing no 44.Add it to str;//Output:"414243444546". #include #include bool FixStr(const string& str, string* result) { if (str.empty()) return false; int len = str.length(); int first_len = 1; for (; first_len len) { matched = false; break;
阅读全文
摘要:dp,时间O(m*n),m为目标值,n为硬币数,空间O(m)/* * Given a list of 'N' coins, their values being in an array A[], return the minimum number of coins required to sum to 'S' (you can use as many coins you want). If it's not possible to sum to 'S', return -1 * * * Input #01: * Coin denomina
阅读全文
摘要://Write a function to check if two rectangles defined as below, have common area or not. The functions take the top left and bottom right coordinate as input and return 1 if they have common area, otherwise return 0.struct Point { int x; int y;};struct Rec { Point l_top; Point r_bot;};bool Che...
阅读全文
摘要:O(n)的算法#include #include #include #include using namespace std;int LongestPlalindrome(const string& str) { if (str.empty()) return 0; string recombined_str; max_len = 1; recombined_str.resize(2 * str.size() - 1); for (int i = 0; i pld_len; pld_len.resize(recombined_str.size(), 1); int farres...
阅读全文
摘要:// 检查一颗树是否为二叉查找树#include #include using namespace std;#define MIN_NUM -0x80000000struct Node { int val; Node* left; Node* right;};bool CheckBinarySearchTree(Node* root) { if (!root) return false; stack s; int pre = MIN_NUM; Node* cur = root; while (cur || !s.empty()) { while (cur) { ...
阅读全文
摘要:擦,花了很久才调成功,push_back放到了循环外,真tm想抽自己,烦躁!先排序再做,效率会比较高,同时避免输出重复,对于重复数字,如果有n个重复数字,使用dp就是从包含0个到包含n个该数字的所有组合。// 输出数组中和为某个整数的所有组合#include #include #include using namespace std;void Print(const vector& arr) { for (int i = 0; i > Process(const vector& arr, int pos, int sum) { vector > results; i
阅读全文
摘要:很简单,思路一定要清晰。//数组有N-2个数字,数字的范围为1 ... N,没有重复的元素,要求打印缺少的2个数字, 空间复杂度O(1)。 #include using namespace std;void PrintNotExistNum(int arr[], int n) { bool end = false; bool end_pre = false; for (int i = 0; i < n; ++i) { if (arr[i] == -1 || arr[i] == i + 1) continue; while (arr[i] != -1 && arr[i] !
阅读全文
摘要:利用数组存储,如果重复了在对应位置设置为-1,逻辑上考虑清楚,代码尽量清晰#include using namespace std;//数组有N+M个数字, 数字的范围为1 ... N, 打印重复的元素, 要求O(M + N), 空间复杂度O(1)。 void PrintRepeatedNum(int arr[], int n) { for (int pos = 0; pos < n; ++pos) { if (arr[pos] == -1) continue; while (arr[pos] != pos + 1) { int swap_pos = arr[pos] ...
阅读全文

浙公网安备 33010602011771号