//给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那...
/*//给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。#include<stdio.h>
#include<string.h>
void main(void)
{
int nums[] = { 2, 7, 11, 15 };
inttarget = 9;
int len = sizeof(nums) / sizeof(nums);
printf("%d", len);
for (int i = 0; i < len-1; i++) {
for (int j = i + 1; j < len; j++) {
if (nums == target - nums) {
printf("%d %d\n", i,j);
}
}
}
}
*/
/*
#include<stdio.h>
struct sum {
int exi;//是否存在
int local;//存在的位置
};
int nums[] = { 2, 7, 11, 15 };//定义无序无重复
int flag = 9;//两数相加目标值
int len = sizeof(nums) / sizeof(nums);//数组长度
intmain(void) {
struct sum sum1;//长度为最大数的值+1
int i;
for (i = 0; i < 20; i++) {//初始化
sum1.exi = 0;
sum1.local = 0;
}
int j,k;
for (j = 0; j < len; j++) {//给数组中存在的数赋值
sum1].exi = 1;
sum1].local = j;
}
for (k = 0; k < len; k++) {
int num = flag - nums;
if (sum1.exi == 1) {//哈希表查找
printf("%d,%d\n", k, sum1.local);
sum1.exi = 0;//查找结束将这两个加数删除
sum1].exi = 0;
}
}
return 0;
}*/
/*
#include<stdio.h>
struct sum {
int e;
int local;
};
int nums[] = { 2, 7, 11, 15 };
int flag = 9;
int len = sizeof(nums) / sizeof(nums);
intmain(void) {
struct sum sum1;
int i;
for (i = 0; i < 20; i++) {
sum1.e= 0;
sum1.local = 0;
}
int j, k;
for (j = 0; j < len; j++) {
sum1].e = 1;
sum1].local = j;
}
for (k = 0; k < len; k++) {
int num = flag - nums;
if (sum1.e == 1) {
printf("%d %d\n", k, sum1.local);
sum1.e = 0;
sum1].e = 0;
}
}
return 0;
}
*/
页:
[1]