3Sum closest total accepted:107572 total submissions:352697 difficulty:medium contributors:admin
Given an array S of n integers, find three integers in s such so the sum is closest to a Given number, target. Return the sum of the three integers. You may assume this each input would have exactly one solution.
For example, given array S = {-1 2 1-4}, and target = 1.
The sum is closest to the target is 2. (-1 + 2 + 1 = 2).
The problem is to figure out the sum of the closest three digits to target.
---------------------------to write a very stupid two ways, reluctantly passed the test------------------------------------------
Direct without brain circulation pass but, add a sentence ==target to pass ...
Class Solution {public
:
int threesumclosest (vector<int>& nums, int target) {
int result = Nums[0] + nums[1] + nums[2]; if (nums.size () <1000) {for
(int i = 0; I<nums.size ()-2; i++) {for
(int II = i + 1; ii<nums.size ( )-1; ii++) {for
(int III = II + 1; iii<nums.size (); iii++) {
if (Nums[i] + nums[ii] + NUMS[III] = = target) return TA Rget;
if (ABS (Nums[i] + nums[ii] + NUMS[III]-target) <abs (result-target)) result
= Nums[i] + nums[ii] + nums[iii];
}} Return to result
;
}
;
Write yourself the second way to sort by using the sort function
Sort sorts know how to move, such as selecting the first one, begin with the second, end as the last, and moving the end to the left if and greater than target until it is the same as the target or the move ends.
Class Solution {public
:
int threesumclosest (vector<int>& nums, int target) {
int result=nums[0] +NUMS[1]+NUMS[2];
Sort (Nums.begin (), Nums.end ()),//sort sort for
(int i=0;i<nums.size () -2;i++) {
int begin=i+1;
int End=nums.size ()-1;
int sum=nums[i]+nums[begin]+nums[end];
if (sum==target) return target;
while (Sum!=target&&end-1>=begin) {
sum=nums[i]+nums[begin]+nums[end];
result= ABS (Sum-target) <abs (result-target) Sum:result;
if (sum>target) {
end--;
}
if (sum==target) return target;
if (sum<target) {
begin++
}}} return result;
}
;
The result is as shown in figure:
Letter combinations to a Phone number total accepted:119023 total submissions:368508 difficulty:medium Co Ntributors:admin
Given a digit string, return all possible letter combinations this number could represent.
A mapping of Digit to letters (just as the telephone buttons) is given below.
Input:digit string "A"
Output: ["Ad", "AE", "AF", "BD", "Be", "BF", "CD", "CE", "CF"].
Note:
Although the above answer is in lexicographical order, your The answer could as the.
Naïve I thought * and # Also, wasted a lot of time
-------------------------------------my solution, but also for a long time actually---------------------------
Class Solution {public:vector<string> lettercombinations (string digits) {vector<string>result;
if (digits.size () = = 0) return result;
Unordered_map<char, int>mapping;
Unordered_map<char, int>mapping2;
for (int i = 0; i < i++) mapping[i+ ' 0 '] = 3;
for (int i = 0; i < i++) Mapping2[i + ' 0 '] = i;
mapping2[' * '] = 10;
mapping2[' # '] = 11;
mapping[' 0 '] = 1;
mapping[' 7 '] = 4;
mapping[' 9 '] = 4;
mapping[' * '] = 1;
mapping[' # '] = 1;
mapping[' 1 '] = 1; Char a[12][4] = {'},{}, {' A ', ' B ', ' C '},{' a ' + 3, ' B ' + 3, ' C ' + 3}, {' G ', ' h ', ' I '},{' j ', ' K ', ' l '}, {' m ', ' n ', '
O '},{' P ', ' Q ', ' R ', ' s '}, {' t ', ' u ', ' V '},{' w ', ' x ', ' y ', ' z '}, {' + '},{}};
initialize int size = 1;
for (int i = 0; i < digits.size (); i++) {size *= mapping[digits[i]];
} String useless = "";
for (int i = 0; i < size; i++) result.push_back (useless);
int round;
round = size; for (int i = 0; i < digits.size (); i++) {for (int II = 0; round!=0&ⅈ Size/round; ii++) {for (int j = 0; J < round; J +) {result[j + ii*round] = A[mapping2[digits[i]]][j*mapping[digits[i]]/
Round];
}} round/= mapping[digits[i]];
return result; }
};Run Result:
Top solution://is very concise, not really to run, look at the idea and expression, use string to store really beautiful too much
vector<string> lettercombinations (string digits) {
vector<string> res;
String Charmap[10] = {"0", "1", "abc", "Def", "Ghi", "JKL", "MnO", "PQRS", "TUV", "WXYZ"};
Res.push_back ("");
for (int i = 0; i < digits.size (); i++)
{
vector<string> tempres;
String chars = charmap[digits[i]-' 0 '];
for (int c = 0; c < chars.size (); C + +) for
(int j = 0; J < Res.size (); j + +)
Tempres.push_back (Res[j]+chars[c]) ;
res = tempres;
}
return res;
}
There is one more:
vector<string> lettercombinations (string digits) {
vector<string> result;
if (Digits.empty ()) return vector<string> ();
static const Vector<string> v = {"," "," abc "," Def "," Ghi "," JKL "," MnO "," PQRS "," TUV "," WXYZ "};
Result.push_back (""); Add a seed for the initial case for
(int i = 0; i < digits.size (); ++i) {
int num = digits[i]-' 0 ';
if (num < 0 | | | num > 9) break;
Const string& candidate = V[num];
if (Candidate.empty ()) continue;
vector<string> tmp;
for (int j = 0; J < candidate.size (); ++j) {for
(int k = 0; k < result.size (); ++k) {
Tmp.push_back (res ULT[K] + candidate[j]);
}
Result.swap (TMP);
}
return result;
}
It's 12 o'clock. Unexpectedly, go to eat a meal first, wish brush a question happy