java几个经典的算法题目----------查询子串和等于已知数字
给出一个排序好的数组和一个数,求数组中连续元素的和等于所给数的子数组
public class testClockwiseOutput { public static void main(String[] args) { int[] arr = {1,2,2,3,4,5,6,7,8,9}; int sum = 7; findSum(arr, sum); } /** * 找出连续子串的和等于已知数字 */ public static void findSum(int[] arr,int number){ int left = 0; int right = 0; for(int i = 0; i < arr.length;i ++){ left = i; right = i; int curCount =0; while(curCount < number){ curCount+=arr[right++]; } if(curCount == number){ for(int j = left; j<right;j++){ System.out.print(arr[j]); } System.out.println(""); } } } }