LeetCode刷题--点滴记录001
Posted 鲁棒最小二乘支持向量机
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode刷题--点滴记录001相关的知识,希望对你有一定的参考价值。
1. 两数之和
要求
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
解题— —暴力破解
C++版本
#include <iostream>
using namespace std;
#include <vector>
void printVector(vector<int>& v)
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
cout << *it << " ";
cout << endl;
class Solution
public:
vector<int> twoSum(vector<int>& nums, int target)
vector<int> ans;
for (int i = 0; i < nums.size(); i++)
for (int j = i + 1; j < nums.size(); j++)
if (nums[i] + nums[j] == target)
ans.push_back(i);
ans.push_back(j);
return ans;
return ans;
;
;
void test01()
Solution s;
vector<int> nums;
nums.push_back(2);
nums.push_back(7);
nums.push_back(11);
nums.push_back(15);
printVector(nums);
int target = 9;
vector<int> result;
result = s.twoSum(nums,target);
printVector(result);
void test02()
Solution s;
vector<int> nums;
nums.push_back(3);
nums.push_back(2);
nums.push_back(4);
printVector(nums);
int target = 6;
vector<int> result;
result = s.twoSum(nums, target);
printVector(result);
void test03()
Solution s;
vector<int> nums;
nums.push_back(3);
nums.push_back(3);
printVector(nums);
int target = 6;
vector<int> result;
result = s.twoSum(nums, target);
printVector(result);
int main()
test01();
cout << "------------------" << endl;
test02();
cout << "------------------" << endl;
test03();
system("pause");
return 0;
Python版本
class Solution:
def twoSum(self,nums,target):
for i in range(0,len(nums)):
for j in range(i+1,len(nums)):
if nums[i]+nums[j] == target:
return [i,j]
def test01():
nums = [2,7,11,15]
target = 9
s = Solution()
print(s.twoSum(nums,target))
def test02():
nums = [3,2,4]
target = 6
s = Solution()
print(s.twoSum(nums,target))
def test03():
nums = [3,3]
target = 6
s = Solution()
print(s.twoSum(nums,target))
if __name__=="__main__":
test01()
print("--------------")
test02()
print("--------------")
test03()
Java版本
package com.hailei_01;
public class TwoSum
public static void main(String[] args)
int[] nums1 = 2,7,11,15;
int target1 = 9;
int[] result1 = twoSum(nums1, target1);
System.out.println(arrayToString(result1));
System.out.println("-------------------");
int[] nums2 = 3,2,4;
int target2 = 6;
int[] result2 = twoSum(nums2, target2);
System.out.println(arrayToString(result2));
System.out.println("-------------------");
int[] nums3 = 3,3;
int target3 = 6;
int[] result3 = twoSum(nums3, target3);
System.out.println(arrayToString(result3));
public static int[] twoSum(int[] nums, int target)
int []ans=new int[2];
for(int i=0;i<nums.length;i++)
for(int j=(i+1);j<nums.length;j++)
if(nums[i]+nums[j]==target)
ans[0]=i;
ans[1]=j;
return ans;
return ans;
//把数组中的元素按照指定规则组成一个字符串
public static String arrayToString(int[] arr)
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++)
if (i == arr.length - 1)
sb.append(arr[i]);
else
sb.append(arr[i]).append(",");
sb.append("]");
String s = sb.toString();
return s;
解题— —升级
C++版本
#include <iostream>
using namespace std;
#include <vector>
#include <map>
void printVector(vector<int>& v)
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
cout << *it << " ";
cout << endl;
class Solution
public:
vector<int> twoSum(vector<int>& nums, int target)
map<int, int> a;
vector<int> b(2, -1);
for (int i = 0; i < nums.size(); i++)
a.insert(pair<int,int>(nums[i], i));
for (int i = 0; i < nums.size(); i++)
if (a.count(target - nums[i]) > 0 && (a[target - nums[i]] != i))
b[0] = i;
b[1] = a[target - nums[i]];
break;
return b;
;
;
void test01()
Solution s;
vector<int> nums;
nums.push_back(2);
nums.push_back(7);
nums.push_back(11);
nums.push_back(15);
printVector(nums);
int target = 9;
vector<int> result;
result = s.twoSum(nums,target);
printVector(result);
void test02()
Solution s;
vector<int> nums;
nums.push_back(3);
nums.push_back(2);
nums.push_back(4);
printVector(nums);
int target = 6;
vector<int> result;
result = s.twoSum(nums, target);
printVector(result);
void test03()
Solution s;
vector<int> nums;
nums.push_back(3);
nums.push_back(3);
printVector(nums);
int target = 6;
vector<int> result;
result = s.twoSum(nums, target);
printVector(result);
int main()
test01();
cout << "------------------" << endl;
test02();
cout << "------------------" << endl;
test03();
system("pause");
return 0;
Python版本
class Solution:
def twoSum(self, nums, target):
hashmap =
for ind,num in enumerate(nums):
hashmap[num] = ind
for i,num in enumerate(nums):
j = hashmap.get(target - num)
if j is not None and i!=j:
return [i,j]
def test01():
nums = [2,7,11,15]
target = 9
s = Solution()
print(s.twoSum(nums,target))
def test02():
nums = [3,2,4]
target = 6
s = Solution()
print(s.twoSum(nums,target))
def test03():
nums = [3,3]
target = 6
s = Solution()
print(s.twoSum(nums,target))
if __name__=="__main__":
test01()
print("--------------")
test02()
print("--------------")
test03()
Java版本
package com.hailei_02;
import java.util.HashMap;
public class TwoSum
public static void main(String[] args)
int[] nums1 = 2,7,11,15;
int target1 = 9;
int[] result1 = twoSum(nums1, target1);
System.out.println(arrayToString(result1));
System.out.println("-------------------");
int[] nums2 = 3,2,4;
int target2 = 6;
int[] result2 = twoSum(nums2, target2);
System.out.println(arrayToString(result2));
System.out.println("-------------------");
int[] nums3 = 3,3;
int target3 = 6;
int[] result3 = twoSum(nums3, target3);
System.out.println(arrayToString(result3));
public static int[] twoSum(int[] nums, int target)
int[] arr=new int[2];
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++以上是关于LeetCode刷题--点滴记录001的主要内容,如果未能解决你的问题,请参考以下文章