12/13、Integer to Roman/Roman to Integer 

题目

罗马数字规则:

符号 I V X L C D M
数字 1 5 10 50 100 500 1000

代码如下:

 

 1 class Solution {
 2 public:
 3     string intToRoman(int num) {
 4         string str;    
 5         string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};    
 6         int value[]=    {1000,900,500,400, 100, 90,  50, 40,  10, 9,   5,  4,   1};   
 7         for(int i=0;num!=0;++i)  
 8         {  
 9             while(num>=value[i])  
10             {  
11                 num-=value[i];  
12                 str+=symbol[i];  
13             }  
14         }  
15         return str;  
16 
17     }
18 };

举一反三,如果是将罗马数字转换为整数呢。思路是一样。参考代码如下:

 1 class Solution {
 2 public:
 3     int romanToInt(string s) {
 4         int ret = toNumber(s[0]);
 5         for (int i = 1; i < s.length(); i++) {
 6             if (toNumber(s[i - 1]) < toNumber(s[i])) {
 7                 ret += toNumber(s[i]) - 2 * toNumber(s[i - 1]);
 8             } else {
 9                 ret += toNumber(s[i]);
10             }
11         }
12         return ret;
13     }
14     
15     int toNumber(char ch) {
16         switch (ch) {
17             case 'I': return 1;
18             case 'V': return 5;
19             case 'X': return 10;
20             case 'L': return 50;
21             case 'C': return 100;
22             case 'D': return 500;
23             case 'M': return 1000;
24         }
25         return 0;
26     }
27 };

 

-------------------------------------------------------------------------------------------------分割线----------------------------------------------------------------------------

14、Longest Common Prefix

题目

这道题目比较简单,参考代码如下:

 1 class Solution {
 2 public:
 3     string longestCommonPrefix(vector<string> &strs) {
 4         int length = strs.size();
 5         string result="";
 6         int index=0,i;
 7         char temp;
 8         bool flag=true;
 9         if(0 == length)
10             return "";
11         while(flag)
12         {
13             temp=strs[0][index];
14 
15             for (i=0;i<length;i++)
16             {
17                 if (index>=strs[i].length() ||strs[i][index] != temp)
18                 {
19                     flag=false;
20                     break;
21                 }
22 
23             }
24             if(i==length)
25                 result += temp;
26             index++;
27 
28         }
29         return result;
30     }
31 };

 

----------------------------------------------------------------------------------------------分割线-------------------------------------------------------------------------------

15、3Sum

题目

这道题目和Leetcode第1题很相似,第1题中是对排序数组进行收尾夹逼求解。因此在这一题中,我们任然也可以采用收尾夹逼的准则去求解。在每次求解的过程中,先固定好一个数c,然后采用夹逼方法判断a+b ?= -c,如果不等且a+b<-c,a右移,相反的,b左移;

题目要求不能有重复的解。为了避免重复解,在每次固定c值时,如果该值之前已经做过判断,直接跳过。

代码如下:

 1 class Solution {
 2 public:
 3     vector<vector<int> > threeSum(vector<int> &num) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6        
 7         vector<vector<int> > ret;
 8          ret.clear();
 9         sort(num.begin(),num.end());
10         for(int i=0; i!=num.size();i++){
11             if(i > 0 && num[i]==num[i-1])
12                 continue;
13             int j,k;
14             j=i+1;
15             k=num.size()-1;
16             while(j<k){
17                 if(j>i+1&&num[j]==num[j-1]){ 
18                     j++;
19                     continue;
20                 }
21                 if(k<num.size()-1&& num[k]==num[k+1]){
22                     k--;
23                     continue;
24                 }
25                  int sum = num[i] + num[j] + num[k];
26                 if(sum>0){
27                     k--;
28                 }else if(sum<0){
29                     j++;
30                 }else{
31                     vector<int> tmp;
32                     tmp.push_back(num[i]);
33                     tmp.push_back(num[j]);
34                     tmp.push_back(num[k]);
35                     ret.push_back(tmp);
36                     j++;
37                 }
38             }
39         }
40         return ret;
41             
42     }
43 };

算法的时间复杂度是:排序O(nlogn)和遍历O(n*n).

 

--------------------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------

16、3Sum Closest

题目

 

这题和15题是一个意思,都是采用先排序,再首尾夹逼。代码如下:

 1 class Solution {
 2 public:
 3     int threeSumClosest(vector<int> &num, int target) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         sort(num.begin(), num.end());
 7         
 8         int ret;
 9         bool first = true;
10         
11         for(int i = 0; i < num.size(); i++)
12         {
13             int j = i + 1;
14             int k = num.size() - 1;
15             
16             while(j < k)
17             {
18                 int sum = num[i] + num[j] + num[k];
19                 if (first)
20                 {
21                     ret = sum;
22                     first = false;
23                 }
24                 else
25                 {
26                     if (abs(sum - target) < abs(ret - target))
27                         ret = sum;                                         
28                 }
29                 
30                 if (ret == target)
31                     return ret;
32                 
33                 if (sum > target)
34                     k--;
35                 else
36                     j++;
37             }
38         }
39         
40         return ret;
41     }
42 };