https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
在已排序的陣列使其數值相加等於目標值,並返回其索引但須注意並非從0開始。
利用2個for迴圈判斷其相加是否等於目標值,但須注意因為並非從0開始所以在其得到索引位置後都需加一。
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
int *reval = (int*)malloc(sizeof(int)*2);
*returnSize = 2;
int left, right;
left = 0; right = numbersSize-1;
for(;;) {
if(numbers[left] + numbers[right] > target)
right--;
else if(numbers[left] + numbers[right] < target)
left++;
else {
reval[0] = left+1;
reval[1] = right+1;
return reval;
}
}
}
var twoSum = function(numbers, target) {
var result = [];
for(var i = 0; i<numbers.length; i++)
{
for(var j = i+1; j<numbers.length; j++)
{
if((numbers[i] + numbers[j]) == target)
{
result.push(i+1)
result.push(j+1)
return result;
}
}
}
};
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
Every time you encounter frustration, when you meet people who laugh at you and hit you, please tell yourself you can’t borrow their wings and fly to the sky you want to go, just like they can’t replace you that living a life you don’t want.
每次遇到挫折的時候、遇到那些嘲笑你和打擊你的人的時候, 請告訴自己,你沒有辦法借他們的翅膀,飛向你想去的天空, 就像他們無法代替你,過你不想要的人生。