Home > Back-end >  Leetcode 1 for help
Leetcode 1 for help

Time:04-27


 public class leetcode001 {
Static int [] nums={-, 10-1, 18, 19} -;
Static int target=- 19;
//static int [] nums={3, 4-trichlorobenzene};
//static int target=6;
//static int [] nums={3, 3};
//static int target=6;
Public static void main (String [] args) {
Solution a=new Solution ();
Int [] result=a.t woSum (nums, target);
System. The out. Println (result [0]).
System. The out. Println (result [1]).
}
}

The class Solution {
Public int [] twoSum (int [] nums, int target) {
Int head=0, tail=nums. Length - 1.
int temp;
//first to sort an array, but before another array to record the location of the
Int [] order=new int [nums. Length].
for(int i=0; iThe order [I]=I;
}
for(int i=0; iFor (int j=I + 1; jIf (nums [I] & gt; Nums [j]) {
Temp=nums [I];
Nums=nums [I] [j];
[j] nums=temp;
Temp=order [I];
The order [I]=order [j];
The order [j]=temp;
}
}
}
While (true) {
If (nums [head] + nums==[tail] target) {
If (order [head] & gt; The order (tail)) {
Temp=order [head];
The order [head]=order (tail);
The order (tail)=temp;
}
Int [] result={order [head], order (tail)};
return result;
}
Else if (nums nums [head] + [tail] Head++;
}
Else if (nums [head] + nums (tail) & gt; Target) {
Tail -;
}
}
}
}
  • Related