C语言实现给定一个数组nums,刷题3:给定一个数组 nums,判断 nums 中是否存在三个下标 a,b,c数相加等于targe且a,b,c不相等...

题目:

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,

下标 ,a ,b , c 对应数相加等于 targe 找出所有满足条件且不重复的三元组下标

解析:

在一个list里面找出来三个数字使这三个数字相加等于目标targe,

这里是一个list 我们去循环这里面的元素,我们利用for循环, 第一个取来,然后后剩下的元素分别取循环上一个循环剩下的元素。这样保证了不重复,最后验证下,如果找出来的数字的值满足a+b+c=targe ,且三个数不相等,我们认为查询正确。

那么我们看下python代码是如何实现的

def findthree(nums:list,targe:int):

if len(nums)<3:

return False

result=[]

for a in range(len(nums)):

for b in range(len(nums))[a:]:

for c in range(len(nums))[b:]:

try:

if nums[a]+nums[b]+nums[c]==targe and a!=b and b!=c and a!=c :

result.append((a,b,c))

except:

return False

return result

那么我们看下测试代码

import unittest

from findthree import findthree

class TestCae(unittest.TestCase):

def setUp(self) -> None:

pass

def tearDown(self) -> None:

pass

def testone(self):

reslt=findthree([],6)

self.assertFalse(reslt)

def testtwo(self):

reslt = findthree([1], 6)

self.assertFalse(reslt)

def testthree(self):

reslt = findthree([1,'2'], 6)

self.assertFalse(reslt)

def testFor(self):

reslt = findthree([1,'2',"2"], 6)

self.assertFalse(reslt)

def testfive(self):

reslt = findthree([1,2,3], 6)

self.assertEqual(reslt,[(0,1,2)])

def testsix(self):

reslt = findthree([1,2,3,3], 6)

self.assertEqual(reslt,[(0,1,2),(0,1,3)])

if __name__=="__main__":

unittest.main()

看下运行结果:

301a6d467ab1352272b273d6e7b1b8df.png

看下最后代码的覆盖率

d88e3152f14160ac4287beb79521abf4.png

这样我们就测试完毕我们写的代码了。 那么我们认为目前测试用例覆盖了百分之百路径下面所有的​分支,认为代码没有bug,测试通过。

接下来,我们看下java版本的实现

public class Fintrhee {

public List> find(List list, Integer targert){

List> resultList=new ArrayList<>();

if (list.size()<3){

return null;

}

for (int a=0;a

for(int b=a;b

for(int c=b;c

if (list.get(a)+list.get(b)+list.get(c)==targert && !new Integer(a).equals(new Integer(b))&&!new Integer(a).equals(new Integer(c))&&!new Integer(c).equals(new Integer(b))){

Map map=new HashMap<>();

map.put("a",a);

map.put("b",b);

map.put("c",c);

resultList.add(map);

}

}

}

}

return resultList;

}

}

测试代码:

public class FintrheeTest {

@Test

public void testFind() {

Fintrhee fintrhee=new Fintrhee();

List integerList=new ArrayList<>();

integerList.add(0);

List> maps=fintrhee.find(integerList,1);

assertEquals(null,maps);

}

@Test

public void test2Find() {

Fintrhee fintrhee=new Fintrhee();

List integerList=new ArrayList<>();

integerList.add(1);

integerList.add(2);

integerList.add(3);

List> maps=fintrhee.find(integerList,1);

List> mapList=new ArrayList<>();

assertEquals(maps,mapList);

}

@Test

public void test3Find() {

Fintrhee fintrhee=new Fintrhee();

List integerList=new ArrayList<>();

integerList.add(1);

integerList.add(2);

integerList.add(3);

List> maps=fintrhee.find(integerList,6);

List> mapList=new ArrayList<>();

Map map=new HashMap<>();

map.put("a",0);

map.put("b",1);

map.put("c",2);

mapList.add(map);

assertEquals(maps,mapList);

}

}

测试结果​:

df9bdb43dd25d5f451620a2159ab2b3e.png

​覆盖率:

8ab2d173d46d79b95f2f8b16b3bb4c03.png

​刷题还在继续,文章优化在下面公众号首发,

原文出处:https://www.cnblogs.com/leiziv5/p/11758222.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用单调栈来解决这个问题。具体实现如下: ```c #include <stdio.h> #include <stdlib.h> typedef struct { int value; int index; } Pair; typedef struct { Pair *data; int top; int size; } Stack; Stack *createStack(int size) { Stack *stack = (Stack *)malloc(sizeof(Stack)); stack->data = (Pair *)malloc(sizeof(Pair) * size); stack->top = -1; stack->size = size; return stack; } void push(Stack *stack, Pair pair) { if (stack->top == stack->size - 1) { return; } stack->top++; stack->data[stack->top] = pair; } Pair pop(Stack *stack) { if (stack->top == -1) { Pair pair = {-1, -1}; return pair; } Pair pair = stack->data[stack->top]; stack->top--; return pair; } void nextGreaterElement(int *nums, int numsSize, int *result) { Stack *stack = createStack(numsSize); for (int i = 0; i < numsSize; i++) { Pair pair = {nums[i], i}; while (stack->top != -1 && stack->data[stack->top].value < nums[i]) { Pair p = pop(stack); result[p.index] = i; } push(stack, pair); } while (stack->top != -1) { Pair p = pop(stack); result[p.index] = -1; } free(stack->data); free(stack); } int main() { int nums[] = {3, 1, 4, 2}; int numsSize = sizeof(nums) / sizeof(int); int *result = (int *)malloc(sizeof(int) * numsSize); nextGreaterElement(nums, numsSize, result); for (int i = 0; i < numsSize; i++) { printf("%d ", result[i]); } printf("\n"); free(result); return 0; } ``` 这个程序使用了单调栈来找到每个元素右边第一个比它大的元素。具体来说,我们从左到右遍历数组,对于每个元素,我们将它和它的下标打包成一个 Pair 结构体,然后将这个 Pair 入栈。如果栈顶元素的值比当前元素小,那么我们就弹出栈顶元素,并将它的下标作为当前元素右边第一个比它大的元素的下标。如果栈为空,说明当前元素右边没有比它大的元素,我们将它的下标设为 -1。最后,我们遍历一遍栈,将剩余的元素的下标设为 -1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值