标签:rom 使用 条件 list eric close 分享 整数 head
class Solution { public: int Sum_Solution(int n) { char a[n][n+1]; return sizeof(a)>>1; } };
class Solution { public: int Add(int num1, int num2) { if(num1 == 0) return num2; if(num2 == 0){ return num1; } return Add(num1^num2,(num1&num2)<<1); } };
class Solution { public: int StrToInt(string str) { if(str.length()==0){ return 0; } int symbol = 0; int val = 0; if(str[0]==‘+‘){ symbol=1; }else if(str[0]==‘-‘){ symbol=-1; }else if(str[0]-‘0‘>=0 && str[0]-‘0‘<10){ val = str[0]-‘0‘; symbol=1; } for(int i=1;i<str.length();i++){ if(str[i]-‘0‘>10 || str[i]-‘0‘<0){ return 0; } val = val*10 + str[i]-‘0‘; } return val*symbol; } };
class Solution { public: // Parameters: // numbers: an array of integers // length: the length of array numbers // duplication: (Output) the duplicated number in the array number // Return value: true if the input is valid, and there are some duplications in the array number // otherwise false bool duplicate(int numbers[], int length, int* duplication) { map<int,int>a; for(int i=0;i<length;i++){ a[numbers[i]]++; } for(auto it=a.begin();it!=a.end();it++){ if(it->second>1){ *duplication = it->first; return true; } } return false; } };
利用两个辅助数组,
第一个数组L依次保存A数组从
0
-length-
1
的乘积,
第二个数组h依次保存从length-
1
到
0
的乘积,
然后每一个要求的B[i]=L[i-
1
]*H[i+
1
].
//B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1] //从左到右算 B[i]=A[0]*A[1]*...*A[i-1] //从右到左算B[i]*=A[i+1]*...*A[n-1] class Solution { public: vector<int> multiply(const vector<int>& A) { int n=A.size(); vector<int> b(n); int ret=1; for(int i=0;i<n;ret*=A[i++]){ b[i]=ret; } ret=1; for(int i=n-1;i>=0;ret*=A[i--]){ b[i]*=ret; } return b; } };
class Solution { public: bool match(char* str, char* pattern) { if (*str == ‘\0‘ && *pattern == ‘\0‘) return true; if (*(pattern + 1) == ‘*‘) { // 如果已经到结尾了,把后面的*都匹配掉 if (*str == ‘\0‘) return match(str, pattern + 2); if ((*pattern == *str || *pattern == ‘.‘)) { // 尝试匹配一个,模式串往后移动 // 尝试匹配一个,模式串不往后移 // 一个都不匹配,模式串往后移动 // 3种当中有一种成功就可以了 return match(str + 1, pattern + 2) || match(str + 1, pattern) || match(str, pattern + 2); } else { // 因为是*所以不匹配也没事,直接跳到下一个 return match(str, pattern + 2); } } else if (*str == ‘\0‘) // 如果没有*了,但是模式串还没匹配完,那么失败 return false; else if (*pattern == ‘.‘ || *pattern == *str) // .或者模式串字符本来就匹配 { return match(str + 1, pattern + 1); } return false; } };
class Solution { public: bool isNumeric(char* str) { // 标记符号、小数点、e是否出现过 bool sign = false, decimal = false, hasE = false; for (int i = 0; i < strlen(str); i++) { if (str[i] == ‘e‘ || str[i] == ‘E‘) { if (i == strlen(str)-1) return false; // e后面一定要接数字 if (hasE) return false; // 不能同时存在两个e hasE = true; } else if (str[i] == ‘+‘ || str[i] == ‘-‘) { // 第二次出现+-符号,则必须紧接在e之后 if (sign && str[i-1] != ‘e‘ && str[i-1] != ‘E‘) return false; // 第一次出现+-符号,且不是在字符串开头,则也必须紧接在e之后 if (!sign && i > 0 && str[i-1] != ‘e‘ && str[i-1] != ‘E‘) return false; sign = true; } else if (str[i] == ‘.‘) { // e后面不能接小数点,小数点不能出现两次 if (hasE || decimal) return false; decimal = true; } else if (str[i] < ‘0‘ || str[i] > ‘9‘) // 不合法字符 return false; } return true; } };
class Solution { public: //Insert one char from stringstream string str; void Insert(char ch) { str += ch; } //return the first appearence once char in current stringstream char FirstAppearingOnce() { vector<char> a; map<char, int>b; for (int i = 0; i<str.size() ; i++){ a.push_back(str[i]); b[str[i]]++; } for (int i = 0; i<a.size();i++){ if (b[a[i]]>1){ continue; } else if (b[a[i]] == 1){ return a[i]; } } return ‘#‘; } };
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* EntryNodeOfLoop(ListNode* pHead) { //if(pHead == NULL)return NULL; ListNode* p = pHead; map<ListNode*,int>a; while(p){ if(++a[p] == 2) { break; } p = p->next; } return p; } };
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if (pHead == NULL || pHead->next == NULL) return pHead; /*---------先为链表创建一个头结点---------*/ int firstNumber = pHead->val; //假设我的头结点数值为-1 int myFirst = -1; //万一链表的头结点也为-1,那么我就改成-2 if (myFirst == firstNumber) { myFirst = -2; } ListNode *head = new ListNode(myFirst); head->next = NULL; head->next = pHead; ListNode *p = head; ListNode *q = head->next; while (q) { while (q->next && (q->next->val == q->val)) { q = q->next; } if (p->next != q) { q = q->next; p->next = q; } else { p = q; q = q->next; } } //返回的时候,注意去掉头结点(自己创建的辅助节点) return head->next; } };
标签:rom 使用 条件 list eric close 分享 整数 head
原文地址:http://www.cnblogs.com/yuguangyuan/p/6077950.html