概述
1.暴力法
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return []
C++:
class Solution{
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n =nums.size();
for (int i=0;i<n;++i) {
for (int j =i+1; j<n;++j) {
if (nums[i] +nums[j] == target){
return {i,j};
}
}
}
return {};
}
};
Rust:
impl Solution {
pub fn two_sum
for i in 0..nums.len(){
for j in i+1..nums.len(){
if nums[i] +nums[j] ==target{
return vec![i as 32, j as i32];
}
}
}
vec![]
}
}
class Solution{
public:
vector<int> twoSum(vector
int n = nums.size();
for (int i=0;i<n; ++i)
for (int j =i+1;
if (nums[i]+ nums[j]
return {i,j};
}
}
}
return {};
}
};
class Solution:
def twoSum(self,nums:List[int], target:int ) -> int:
n = len(nums)
for i in range(n):
for j in range(i+1, n):
if nums[i] + nums[j] == target
return [i,j]
return []
最后
以上就是火星上期待为你收集整理的Array 两数之和two sum Rust python C++的全部内容,希望文章能够帮你解决Array 两数之和two sum Rust python C++所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复