如題,有大佬知道怎么用java做出來嗎?
1.
給定一個包括 n 個整數的陣列 nums 和 一個目標值 target。找出 nums 中的 任意個數之和 等于 target,并列印出所有結果的索引,同一個結果索引不能重復。
例如: nums = [1,5,8,17,29,33,39,11,16] ,target = 33
結果1:5 ,結果2:3,8,...
!!!! 使用題目的測驗資料,把輸出結果保存到代碼后面 !!!!
2. 給定一組開始、結束時間的陣列,找出陣列中的交叉時間段及空余時間段
[
[2020-03-20,2020-03-22],
[2020-05-20,2020-07-22],
[2020-04-20,2020-05-22],
[2020-08-20,2020-09-22],
]
空余時間段:2020-03 ~ 23-2020-04-19,2020-07-23 ~ 2020-08-19,...
交叉時間段:2020-05-20 ~ 2020-05-22,...
!!!! 使用題目的測驗資料,把輸出結果保存到代碼后面 !!!!
//代碼寫在后面,寫完后,點擊左上角保存按鈕,提示 “保存成功” 即可
//如果沒提示保存成功,需要把 “名字” , 復制到#后面,并重繪頁面
//請確認代碼正確,不能出現運行報錯
uj5u.com熱心網友回復:
做出來了嗎??uj5u.com熱心網友回復:
你也要做這個嗎?
uj5u.com熱心網友回復:
import java.util.Stack;
public class Test11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {1, 5, 8, 17, 29, 33, 39, 11, 16 };
int tar = 33;
find(arr, tar, 0, 0);
}
static Stack<Integer> list = new Stack<Integer>();
public static void find(int[] arr, int tar, int idx, int total){
if(idx == arr.length)
return;
list.add(idx);
if(total + arr[idx] == tar){
System.out.println(list);
}
find(arr, tar, idx + 1, total + arr[idx]);
list.pop();
find(arr, tar, idx + 1, total);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283357.html
標籤:Java SE
上一篇:本地JIRA訪問非常卡,求指教
