3 sum allow number used multi times

class Solution
 {
 public:
     vector<vector<int> > ThreeSum(int a[], int n, int target)
     {
         vector<vector<int> > ret;
         for (int i = 0; i < n; i++)
         {
             if(i>0 && a[i] == a[i - 1])
                 continue;
             int begin = i, end = n - 1;
             while (begin <= end)
             {
                 int sum = a[i] + a[begin] + a[end];
                 if (sum == target)
                 {
                     vector<int> res = { a[i], a[begin], a[end] };
                     ret.push_back(res);
                     begin++;
                     end--;
                     while (begin < end && a[begin] == a[begin - 1])
                         begin++;
                     while (begin<end && a[end] == a[end + 1])
                         end--;
                 }
                 else if (sum>target)
                 {
                     end--;
                 }
                 else
                 {
                     begin++;
                 }

             }
         }
         return ret;
     }
};
时间: 2024-10-28 15:37:30

3 sum allow number used multi times的相关文章

1104. Sum of Number Segments (20)【数学题】——PAT (Advanced Level) Practise

题目信息 1104. Sum of Number Segments (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence {0.1, 0.2, 0.3, 0.4}, we have 10 segments: (0.1)

PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segments (20 分) Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3,

PAT 甲级 1104 Sum of Number Segments (20分)(有坑,int *int 可能会溢出)

1104 Sum of Number Segments (20分)   Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0

1104 Sum of Number Segments(二刷)

英文题目:1104 Sum of Number Segments 中文题目:1049 数列的片段和 1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 int n; 6 double t,sum = 0; 7 cin>>n; 8 for(int i = 0 ; i < n; ++i) { 9 cin>>t; 10 sum += (i+1)*t*(n-i); 11 } 12 printf(&q

PAT (Advanced Level) 1104. Sum of Number Segments (20)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n; double a[100000+10]; double b[100000+10]

1104 Sum of Number Segments (20)

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence {0.1, 0.2, 0.3, 0.4}, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0

1104 Sum of Number Segments

题意: 给出n个不大于1.0的小数序列,如{ 0.1, 0.2, 0.3, 0.4 },则共有10个分片(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) and (0.4).现要求计算每个分片之和,即0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0. 思路:数学题

Sky number

描述 key 天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进 制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22.key非常喜欢这种四位数(三种进制的和相等),由于他 的发现,所以这里我们命名其为key数.但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进制的四位数,是不是key数吧. 输入 输入含有一些四位正整数,如果为0,则输入结束. 输

【树】Path Sum II(递归)

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路: 递归求解,只是要保存当前的结果,并且