标签:
1.Two sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
解题思路:
1)尝试:两层循环,分别进行遍历(<Algorithm>中的find函数也是遍历),进行匹配,复杂度O(n^2),超时了
2)打开标签,显示hash_table,想到了STL中的关联容器,结合这里需要返回索引值,选定了multimap,代码如下:
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> res; //要返回的数组 int len = (int)nums.size(); multimap<int,int> m1; for(int i = 0; i < len; ++i) //首先把原数组中的值和(索引+1)存入multimap m1.insert(pair<int, int>(nums.at(i), i+1)); //这里不要使用m1::value_type() multimap<int,int>::iterator im1 = m1.end(); for(int i = 0; i < len; ++i){ im1 = m1.find(target - nums.at(i)); if(m1.end() != im1 && ((im1->second) -1) != i){ //这里还需判断下,找到的值是不是自身,例如{3,2,4} target-6,因为只有一种解决方案 int j = im1->second; if(i+1 < j){ //保证索引值小的在前 res.push_back(i+1); res.push_back(j); } else{ res.push_back(j); res.push_back(i+1); } return res; } } } };
2.Add two numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
解题思路:
1)为了直接相加,直接遍历每一条链表,求出其数值,2*10^0 + 4*10^1 + 3*10^2 +......,然后直接相加,再把值保存到一个新的链表中,用sum%10,sum/10的方法,测试的时候发现超出int范围的数就出错,没办法,只能一位一位加了;
2)设置一个进位标志flag,大的步骤是:a)当两个链表都没到尽头;b)其中一个链表到了头,继续加另外一个;c)最高位是否有进位,或者说看flag是否为1,有的话创建一个节点。具体代码如下:
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 13 if (l1 == NULL && l2 == NULL) 14 return NULL; 15 16 ListNode *head = NULL; 17 ListNode *pre = NULL; 18 int flag = 0; 19 20 while(l1 && l2) 21 { 22 ListNode *node = new ListNode((l1->val) + (l2->val) + flag); 23 flag = (node->val) / 10; 24 (node->val) %= 10; 25 26 if (head == NULL) 27 head = node; 28 else 29 pre->next = node; 30 31 pre = node; 32 33 l1 = l1->next; 34 l2 = l2->next; 35 } 36 37 while(l1) 38 { 39 ListNode *node = new ListNode((l1->val) + flag); 40 flag = (node->val) / 10; 41 (node->val) %= 10; 42 43 if (head == NULL) 44 head = node; 45 else 46 pre->next = node; 47 48 pre = node; 49 50 l1 = l1->next; 51 } 52 53 while(l2) 54 { 55 ListNode *node = new ListNode((l2->val) + flag); 56 flag = (node->val) / 10; 57 (node->val) %= 10; 58 59 if (head == NULL) 60 head = node; 61 else 62 pre->next = node; 63 64 pre = node; 65 66 l2 = l2->next; 67 } 68 69 if (flag > 0) 70 { 71 ListNode *node = new ListNode(flag); 72 pre->next = node; 73 } 74 75 return head; 76 } 77 };
发现的其他几个小问题:
1)多个struct类型变量指针的声明不能放在一行;(具体原因有待探讨)
2)函数内new出来的空间数据可以进行返回,我一开始还把所有节点指针定义为静态变量,后来发现不需要;(最好还是返回引用)
标签:
原文地址:http://www.cnblogs.com/xiaomin-is-fighting/p/4513388.html