java create array_Java创建具有可能性集合的数组的快速方法(Java Quick Way to Create an Array with Set of Possibilities)...

Java创建具有可能性集合的数组的快速方法(Java Quick Way to Create an Array with Set of Possibilities)

什么是创建具有可能性的二维数组的快速简单的方法?

例如,如果数字是5,我希望数组为:

{{0,1},{0,2},{0,3},{0,4},{1,2},{1,3},{1,4},{2,3},{2,4},{3,4}}

如果数字是6,

{{0,1},{0,2},{0,3},{0,4},{0,5},{1,2},{1,3},{1,4},{1,5},{2,3},{2,4},{2,5},{3,4},{3,5},{4,5}}

提前致谢。

What would be a quick and simple way to create a two-dimensional array with set of possibilities?

For example, if the number is 5, I want the array to be:

{{0,1},{0,2},{0,3},{0,4},{1,2},{1,3},{1,4},{2,3},{2,4},{3,4}}

if the number is 6,

{{0,1},{0,2},{0,3},{0,4},{0,5},{1,2},{1,3},{1,4},{1,5},{2,3},{2,4},{2,5},{3,4},{3,5},{4,5}}

Thanks in advance.

原文:https://stackoverflow.com/questions/10239047

更新时间:2019-12-25 15:08

最满意答案

List a = new ArrayList();

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

a.add(new int[]{i, j});

}

}

return (int[][])a.toArray(new int[0][0]);

List a = new ArrayList();

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

a.add(new int[]{i, j});

}

}

return (int[][])a.toArray(new int[0][0]);

2012-04-20

相关问答

从动态规划答案开始,您可以使用递归解决方案,然后使用记忆来缓存结果,自上而下的方法与Amit的自下而上相反。 vector subsetSum(vector& nums)

{

vector ans;

generateSubsetSum(ans,0,nums,0);

return ans;

}

void generateSubsetSum(vector& ans, int sum, vector& nums, int i)

...

在评论中跟踪对话之后,此代码应该有效 struct Drink {

var name: String

var haveSugar: Bool

var haveAlcohol: Bool

init (name: String, haveSugar: Bool, haveAlcohol: Bool) {

self.name = name

self.haveSugar = haveSugar

self.haveAlcoho

...

嵌套的foreach循环。 foreach($greetings as greeting)

foreach($titles as title)

foreach($names as $name)

echo $greeting,' to you, ',$title,' ',$name;

您可以通过预先对数组进行排序并更改前三行的顺序来调整它们的显示顺序 UPDATE 这就是我使用递归函数提出的 它假设你使用正则表达式得到这样的数据并且爆炸这应该是非常简

...

没有100%的方法可以阻止人们阅读文件,但是你可以让它变得更加困难。 您需要做的是加密文件以防止随意阅读,或签署文件以便您可以检测真实性和篡改。 以下概述了如何使用公钥/私钥加密对文件进行加密。 您需要的工具是: GPG 。 您将使用此方法创建密钥 BouncyCastle PGP 。 这是用于实现OpenPGP规范的Java加密API。 您将使用GPG的命令行加密文件,并使用BouncyCastle(BC)从Java读取它们。 以安全的方式执行此操作涉及的步骤是。 创建一个唯一的公钥/私钥对,用

...

在这种情况下,您需要从输入数组中选择streamSize distinct元素。 你可以使用Random#ints(randomNumberOrigin, randomNumberBound)方法: 返回伪随机int值的有效无限流,每个符合给定原点(包含)和绑定(独占)。 这将返回指定范围内的随机整数流。 为了确保不同的值,调用distinct()并且limit(...)只允许保留我们想要的元素的数量。 Random random = new Random();

Player[] subset =

...

List a = new ArrayList();

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

a.add(new int[]{i, j});

}

}

return (int[][])a.toArray(new int[0][0]);

List a = new ArrayList();

for (int i = 0; i <

...

您可以使用提供默认值的条件来初始化变量,而不是使用if / else语句。 function apples(taste, num)

taste = taste or "yum"

amount = num or 0

-- ...

end

Lua or操作符评估并返回它的第一个操作数,除非它nil或为false ,否则它将评估并返回它的第二个操作数。 这导致了上面的默认值成语。 Instead of using if/else statements, you can initialize

...

首先:这是一个讨论而不是一个问题,所以永远不会有一个明确的“是”或“否”......总有优点和缺点超出客观论点( 开发人员不喜欢它 );-) 无论如何,让我首先确定您的情况对于所有类型的Web应用程序都是非常常见的,并且对于从架构角度考虑Web应用程序开发的每个人来说,您所描述的问题更为常见。 面对一年前几乎完全相同的情况,这是我们的架构: 带有JSF 2.0的Java EE 6,CDI(+ EJB 3和JPA,但这超出了本答案的范围)。 ViewScoped Beans,每个视图一个(JSF V

...

django orm limit应该是这样的: Entry.objects.all()[:5]

所以创建一个限制结果数量的新模型是没有意义的。 我看过django-autocomplete-light并在这里看到了limit_choices (没试过。 在任何情况下,它应该类似于此代码段中的第77行。 我希望这是有帮助的。 The only way to do this is to have a separate model with only those possibilities and t

...

如果不使用私有(未记录的)API,您可能无法实现的目标 。 希望Apple能提供这种功能(与用户相关)虽然我不认为这很快就会发生。 如果你想进一步研究这个话题,首先要检查的是iphone-wireless - Stumbler的主页,它显示了MobileApple80211一个很好的用法。 What you're trying to do is sadly not possible without the usage of private (undocumented) API-s. Hopeful

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Create a function pixel_flip(lst, orig_lst, budget, results, i=0) that uses recursion to generate all possible new unique images from the input orig_lst, following these rules: • The input lst is the current list being processed. Initially, this will be the same as orig_lst which is the original flattened image. • The input budget represents the number of pixels that can still be flipped. When the budget reaches 0, no more pixels can be flipped. • The input results is a list of resulting flattened images with flipped pixels. Initially, this will be an empty list. • The input i represents the index of the pixel being processed, by default set to 0, which is used to drive the recursive function towards its base case (i.e., initially starting from i=0). At termination of the function, the argument results should contain all possibilities of the input orig_lst by only flipping pixels from 0 to 1 under both the budget and the adjacency constraints. fill code at #TODO def pixel_flip(lst: list[int], orig_lst: list[int], budget: int, results: list, i: int = 0) -> None: """ Uses recursion to generate all possibilities of flipped arrays where a pixel was a 0 and there was an adjacent pixel with the value of 1. :param lst: 1D list of integers representing a flattened image . :param orig_lst: 1D list of integers representing the original flattened image. :param budget: Integer representing the number of pixels that can be flipped . :param results: List of 1D lists of integers representing all possibilities of flipped arrays, initially empty. :param i: Integer representing the index of the pixel in question. :return: None. """ #TODO def check_adjacent_for_one(flat_image: list[int], flat_pixel: int) -> bool: """ Checks if a pixel has an adjacent pixel with the value of 1. :param flat_image: 1D list of integers representing a flattened image . :param flat_pixel: Integer representing the index of the pixel in question. :return: Boolean. """ #TODO
05-25
Here's the code for the pixel_flip function and check_adjacent_for_one helper function: ``` def pixel_flip(lst: list[int], orig_lst: list[int], budget: int, results: list, i: int = 0) -> None: """ Uses recursion to generate all possibilities of flipped arrays where a pixel was a 0 and there was an adjacent pixel with the value of 1. :param lst: 1D list of integers representing a flattened image. :param orig_lst: 1D list of integers representing the original flattened image. :param budget: Integer representing the number of pixels that can be flipped. :param results: List of 1D lists of integers representing all possibilities of flipped arrays, initially empty. :param i: Integer representing the index of the pixel in question. :return: None. """ if budget == 0: # Base case: no more pixels can be flipped results.append(lst) return if i >= len(lst): # Base case: all pixels have been checked return if lst[i] == 0 and check_adjacent_for_one(orig_lst, i): # Flip the current pixel and recurse with a lower budget new_lst = lst.copy() new_lst[i] = 1 pixel_flip(new_lst, orig_lst, budget - 1, results, i + 1) # Recurse without flipping the current pixel pixel_flip(lst, orig_lst, budget, results, i + 1) def check_adjacent_for_one(flat_image: list[int], flat_pixel: int) -> bool: """ Checks if a pixel has an adjacent pixel with the value of 1. :param flat_image: 1D list of integers representing a flattened image. :param flat_pixel: Integer representing the index of the pixel in question. :return: Boolean. """ row_size = int(len(flat_image)**0.5) row = flat_pixel // row_size col = flat_pixel % row_size if row > 0 and flat_image[(row-1)*row_size+col] == 1: return True if row < row_size-1 and flat_image[(row+1)*row_size+col] == 1: return True if col > 0 and flat_image[row*row_size+(col-1)] == 1: return True if col < row_size-1 and flat_image[row*row_size+(col+1)] == 1: return True return False ``` The pixel_flip function takes in the current list being processed, orig_lst which is the original flattened image, budget representing the number of pixels that can still be flipped, results which is initially an empty list of resulting flattened images with flipped pixels, and i representing the index of the pixel being processed. The function uses recursion to generate all possibilities of flipped arrays where a pixel was a 0 and there was an adjacent pixel with the value of 1. It first checks if the budget is 0 or if all pixels have been checked, and returns accordingly. If the current pixel is 0 and has an adjacent pixel with the value of 1, it flips the current pixel and recurses with a lower budget. Otherwise, it recurses without flipping the current pixel. The check_adjacent_for_one helper function takes in the flattened image and the index of the pixel in question, and checks if the pixel has an adjacent pixel with the value of 1. It calculates the row and column of the pixel using integer division and modulus, and checks if the adjacent pixels in the vertical and horizontal directions have the value of 1. If any of the adjacent pixels have the value of 1, it returns True, otherwise it returns False.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值