当前位置: 首页 > 知识库问答 >
问题:

Brute强制二和算法

岳涵煦
2023-03-14

两个问题是:

给定nums=[2,7,11,15],目标=9,因为nums[0]+nums[1]=2+7=9,返回[0,1]。

public class TwoSum 
{
//static int[] arraynums = new int[]{2, 7, 11, 15};
//static int target = 9;

//public static void main(String args[])
//{
    //TwoSum name = new TwoSum();
    //name.twoSums(arraynums, target);
//}
    public int twoSums(int[] nums, int target) 
    {
        int sum = 0;

        for (int i = 0; i < nums.length; i++)
        {
            for (int j = 0; j < nums.length; j++)
            {   
                sum = nums[i] + nums[j];
                if (sum == target)
                {
                    System.out.println(i + " " + j);
                    return new int[] {i,j};
                }

            }
        }
        return new int[] {};
    }
}

我知道我需要一个return语句,但我不确定应该返回什么,也不确定是否需要我的main方法

共有1个答案

段干浩荡
2023-03-14

@Alfabravo是对的。您需要返回一个整数数组。如果你不想归还任何东西。只需使用

 public void twoSum(int[] nums, int target)
 类似资料:
  • Brute A simple and lightweight Entity Component System library for writing games with Clojure or ClojureScript. The aim of this project was to use basic Clojure building blocks to form an Entity Syste

  • Subsets 描述 Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For exampl

  • Permutations 描述 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 代码 递归 /

  • Combinations 描述 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3],

  • NiX Brute Forcer是一款支持多种协议的蛮力破解工具,它设计的目的是证明使用强壮密码的重要性。NiX Brute Forcer支持的协议或服务包括:MySQL, SSH, FTP, IMAP。另外,NiX Brute Forcer基于NiX Proxy Checker。

  • 我一直在尝试为我的网站配置这个,但是做不到。 我以前有一个cond。htaccess对主域名强制www,对子域什么都不用,但是自从我得到了一个SSL,我遇到了一些问题。 这是一个通配符SSL。 我需要的是在主域上强制HTTPS://WWW,在子域上强制HTTPS://。 即:http://www.domain.com - 有什么规定吗?谢谢! 编辑 现在我用乔恩贴的 问题是,当我在主域上键入 HT