- leetcode73题
- 两数相加(猿辅导面试题,我的两个同学都遇到了这个题)
leetcode2
思路:唯一需要考虑的是进位问题,还有长链表多余的部分/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {//主要就是进位问题 int carry=0;//每次加上进位 ListNode *head=new ListNode(-1); ListNode *dumy=head;//记录头节点位置 int tmp=0; while(l1 && l2){//对于两个链表公共部分进行相加 tmp=l1->val+l2->val+carry; carry=tmp/10; ListNode *cur=new ListNode(tmp%10); head->next=cur; head=cur; l1=l1->next; l2=l2->next; } while(l1){//长链表多余部分直接加上进位再深拷贝 tmp=carry+l1->val; carry=tmp/10; ListNode *cur=new ListNode(tmp%10); head->next=cur; head=cur; l1=l1->next; } while(l2){//长链表多余部分直接加上进位再深拷贝 tmp=carry+l2->val; carry=tmp/10; ListNode *cur=new ListNode(tmp%10); head->next=cur; head=cur; l2=l2->next; } if(carry){//最终可能还是会有进位 ListNode *cur=new ListNode(carry); head->next=cur; head=cur; } return dumy->next; } };
- leetcode445
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {//必须借助其他数据结构来存储数据了 if(l1 ==nullptr && l2==nullptr) return nullptr; if(l1->val==0){ return l2; } if(l2->val==0){ return l1; } stack<int> S1,S2; ListNode *p1=l1; ListNode *p2=l2; while(p1){//链表l1的数据推入栈中 S1.push(p1->val); p1=p1->next; } while(p2){//链表l2的数据推入栈中 S2.push(p2->val); p2=p2->next; } int carry=0; ListNode *head=nullptr; while(!S1.empty() && !S2.empty()){//计算公共部分 int tmp=S1.top()+S2.top()+carry; S1.pop();S2.pop(); carry=tmp/10; ListNode *cur=new ListNode(tmp%10); cur->next=head; head=cur; } while(!S1.empty()){//较长链表剩余部分加上进位单独入栈 int tmp=S1.top()+carry; S1.pop(); carry=tmp/10; ListNode *cur=new ListNode(tmp%10); cur->next=head; head=cur; } while(!S2.empty()){//较长链表剩余部分加上进位单独入栈 int tmp=S2.top()+carry; S2.pop(); carry=tmp/10; ListNode *cur=new ListNode(tmp%10); cur->next=head; head=cur; } if(carry){//进位还存在,单独创建 ListNode *cur=new ListNode(carry); cur->next=head; head=cur; } return head;//返回头部 } };
- 两个链表的公共节点
链表相交情况很复杂
思路:set<int,int>
不会写。 - 实现atoi函数
思路:字符串是空就返回,不为空,找到第一个非空格字符,并判断正负号,满足字符转换条件(字符是数字)时,进行转换,其他情况直接返回0;字符串进行转换时,一旦当前字符不满足条件,退出,返回结果;满足条件时,要判断转换之后数字会不会溢出,2^31=2147483647,base>214748364,不能继续转换,base==214748364且str[i]>'7',不能转换。class Solution { public: int myAtoi(string str) { if (str.empty()) return 0; int sign = 1, base = 0, i = 0, n = str.size(); while (i < n && str[i] == ' ') ++i; if (i < n && (str[i] == '+' || str[i] == '-')) { sign = (str[i++] == '+') ? 1 : -1; } while (i < n && str[i] >= '0' && str[i] <= '9') { if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i]> '7')) { return (sign == 1) ? INT_MAX : INT_MIN; } base = 10 * base + (str[i++] - '0'); } return base * sign; } };
- 剑指offer28题
#include <vector> #include <string> #include <iostream> using namespace std; void func(char *pStr,char *pBegin){ if(*pBegin=='\0'){ printf("%s\n",pStr); return; }else{ for(char *pcur=pBegin;(*pcur)!='\0';++pcur){//pBegin指向的字符串与后面每个字符串交换一次 char t=*pcur; *pcur=*pBegin; *pBegin=t; func(pStr,pBegin+1); t=*pcur; *pcur=*pBegin; *pBegin=t; } } } void Permutation(char *pStr){ if(pStr==nullptr) return; vector<vector<char> >result; vector<char> tmp; func(pStr,pStr); } int main(){ char str[]="abcdef"; Permutation(str); return 0; }
猿辅导面试之手撕代码
最新推荐文章于 2022-12-06 12:07:34 发布