最大子数组
arr没有正数:就是数组中的最大元素
arr中有正数:从左到右遍历arr,变量cur记录每一步的累加和,遇到正数增加,遇到负数,将cur减少;
当cur<0,说明此部分不能作为子数组的左边部分,令cur = 0,从下一个重新开始;
当cur>0,用额外变量记录值max,即用max全程跟踪cur出现的最大值即可
public static int maxSum(int[] arr){
if(arr == null || arr.length == 0){
return 0;
}
int max = Integer.MIN_VALUE;
int cur = 0;
for(int i = 0;i<arr.length;i++){
cur+=arr[i];
max = Math.max(max, cur);
cur = cur < 0 ? 0 : cur;
}
return max;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] strA = str.split(" ");
int[] a = new int[strA.length];
for(int i=0; i<strA.length; i++){
a[i] = Integer.parseInt(strA[i]);
//a[i] = Integer.parseInt(strA[i]);
}
int b = maxSum(a);
System.out.println(b);
}
堆排序算法
1、将初始无序元素构成大根堆,此堆为初始的无序区;
2、将对顶元素与最后一个元素交换,得到新的无序区(1......n-1)和有序区(n)
3、将新的无序区调整为大根堆结构,再次将顶元素与此区中最后一个元素交换,
形成新的无序区(1......n-2)和有序区(n-1,n)
4、不断重复此过程直到有序区元素为n-1个,整个排序过程就完成了
堆排序:数据看成是完全二叉树、根据完全二叉树的特性来进行排序的一种算法
完全二叉树的特性; 左边子节点位置 = 当前父节点的两倍 + 1,右边子节点位置 = 当前父节点的两倍 + 2
大根堆:父节点值一定大于子节点的值;小根堆同理
- 调整堆,使之成为最大堆(调整index位置使成为大根堆),其它位置已经满足父节点大于子节点的情况了
-
递归方法和非递归方法
-
由数组建立整个堆
///-========-调整堆某个元素,使之为大根堆:递归方法和非递归方法-=========
//调整堆,使之成为最大堆(调整index位置使成为大根堆),其它位置已经满足父节点大于子节点的情况了
public void heapify(int[] arr, int index, int heapSize) {
//arr完全二叉树,index当前节点,heapSize根的大小
int left = index * 2 + 1;
int right = index * 2 + 2;
int largest = index;//最大的索引
while (left < heapSize) {
if (arr[left] > arr[index]) {
largest = left;
}
if (right < heapSize && arr[right] > arr[largest]) {
largest = right;
}
if (largest != index) {
swap(arr, largest, index);
} else {
break;
}
index = largest;
left = index * 2 + 1;
right = index * 2 + 2;
}
}
//调整堆,使之成为最大堆(调整index位置使成为大根堆),其它位置已经满足父节点大于子节点的情况了
public void heapify1(int[] arr, int index, int heapSize) {
//arr完全二叉树,index当前节点,heapSize根的大小
int left = index * 2 + 1;
int right = index * 2 + 2;
int largest = index;//最大的索引
if (left < heapSize && arr[left] > arr[largest]) {
largest = left;
}
if (right < heapSize && arr[right] > arr[largest]) {
largest = right;
}
if (largest != index) {
swap(arr, largest, index);
heapify1(arr, largest, heapSize);
}
}
//由数组建立最大堆,直到第一个元素
public void heapBuild(int[] arr){
for(int i = (arr.length/2 - 1); i >= 0; i--){
//从非叶子节点开始开始调整使之成为大根堆(叶子节点没有子节点,不需要要调整)
heapify1(arr, i, arr.length);//传入的是数组、开始调整的位置、堆的大小
}
}
小根堆的建立同理:
//==================================小根堆=========================================================================
//调整堆,使之成为最小堆(调整index位置使成为大根堆),其它位置已经满足父节点大于子节点的情况了
public void heapifyMin(int[] arr, int index, int heapSize) {
//arr完全二叉树,index当前节点,heapSize根的大小
int left = index * 2 + 1;
int right = index * 2 + 2;
int smallest = index;//最大的索引
if (left < heapSize && arr[left] < arr[smallest]) {
smallest = left;
}
if (right < heapSize && arr[right] < arr[smallest]) {
smallest = right;
}
if (smallest != index) {
swap(arr, smallest, index);
heapifyMin(arr, smallest, heapSize);
}
}
//由数组建立最小堆,直到第一个元素
public void heapBuildMin(int[] arr){
for(int i = (arr.length/2 - 1); i >= 0; i--){
//从非叶子节点开始开始调整使之成为小根堆(叶子节点没有子节点,不需要要调整)
heapifyMin(arr, i, arr.length);//传入的是数组、开始调整的位置、堆的大小
}
}
//=========================================================================================
堆排序算法实现:
public void sortHeap(int[] arr){
if(arr == null || arr.length == 0){
return;
}
heapBuild(arr);//先构造一个大根堆
for(int i = arr.length-1; i>0;i--){
swap(arr, 0, i);//循环调整第一个元素与无序区域最后一个元素的位置
heapify1(arr, 0, i);//将无序的堆(第一个元素导致的)调整为有序的堆
}
}
求第k大的数:
建立一个含k个元素的小根堆(全局最大的k个数,根元素就是第k大的数):
如果新来的数大于堆顶元素,替换根节点,调整根结构;如果新来的数小于堆顶元素,不做改变
//第k小的数,对应大根堆
public int MumberKeyMin(int[] arr, int k){
int temp[] = new int[k];
for(int i =0 ;i< k;i++){
temp[i] = arr[i];
}
heapBuild(temp);//建立一个k大小的大根堆
for(int i =k; i < arr.length; i++){
if(arr[i] < temp[0]){
temp[0] = arr[i];
heapify1(temp, 0,k);
}
}
return temp[0];
}
//第k大的数,对应小根堆
public int MumberKeyMax(int[] arr, int k){
int temp[] = new int[k];
for(int i =0 ;i< k;i++){
temp[i] = arr[i];
}
heapBuildMin(temp);//建立一个k大小的小根堆
for(int i =k; i < arr.length; i++){
if(arr[i] > temp[0]){
temp[0] = arr[i];
heapifyMin(temp, 0,k);
}
}
return temp[0];
}
字典序算法 (字典序的全排列)
1、首先对字符串本身按照大小进行排序得到最小的字符串
2、从字符串最右边开始向左找直到i位置:i-1位置比i位置要小的第一个位置
3、从i位置向右找比i-1处元素大的最后一个数,此数的位置为m;
4、交换i-1和m位置的元素;将i位置及后面的元素顺序逆序
5、将此字符串数组添加到集合中
6、直到i位置到了第一个元素位置,退出循环
public ArrayList<String> Permutation1(String str){
ArrayList<String > list = new ArrayList<>();
if(str.length() == 0 || str == null){
return list;
}
char[] chars = str.toCharArray();
Arrays.sort(chars);
list.add(String.valueOf(chars));
int len = str.length();//字符串的长度
while (true){
int i = len-1;
int m = 0;
while (i >= 1 && chars[i] <= chars[i-1]){
i--;
}
if(i == 0){
break;
}
m = i;
while (m < len && chars[m] > chars[i-1]){
m++;
}
swap(chars, i-1, m-1);
revise(chars, i);
list.add(String.valueOf(chars));
}
return list;
}
private void swap(char[] chars , int i, int j){
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
//====字符串数组以k及k位置后的元素逆序排列
private void revise(char[] chars, int k){
if(chars == null || chars.length <= k){
return;
}
int len = chars.length;
for(int i = 0; i < (len-k)/2; i++){
int m = i+ k;
int n = len-1-i;
if(m <= n){
swap(chars, m, n);
}
}
}
//=====输出
@Test
public void Test1() {
ArrayList<String> list = Permutation1("abacacbbd");
System.out.println(list);
System.out.println(list.size());
}
全排列算法(无序)、使用集合工具类排序
public ArrayList<String> Permutation(String str) {
ArrayList<String> arrayList = new ArrayList<>();
if (str != null && str.length() > 0) {
PermutationHelper1(str.toCharArray(), 0, arrayList);
Collections.sort(arrayList);
}
// arrayList.stream().forEach(System.out::println);
System.out.println(arrayList);
System.out.println(arrayList.size());
return arrayList;
}
//比PermutationHelper2高效很多!!!!!!
private void PermutationHelper1(char[] chars, int i, ArrayList<String> list) {
if (i == chars.length - 1) {//比PermutationHelper2高效很多!!!!!
list.add(String.valueOf(chars));
} else {
Set<Character> chaSet = new HashSet<>();
for (int j = i; j < chars.length; ++j) {
if (j == i || !chaSet.contains(chars[j])) {
chaSet.add(chars[j]);
swap(chars, i, j);
PermutationHelper1(chars, i + 1, list);
swap(chars, j, i);
}
}
}
}
private void PermutationHelper2(char[] chars, int i, ArrayList<String> list) {
if (i == chars.length - 1) {
if(!list.contains(String.valueOf(chars))){//此处判断是否包含低效
list.add(String.valueOf(chars));
}
} else {
for (int j = i; j < chars.length; ++j) {
swap(chars, i, j);
PermutationHelper2(chars, i + 1, list);
swap(chars, j, i);
}
}
}
输入总结
1、nextInt()只读取数值,剩下"\n"还没有读取,所以光标还在本行中;
2、next() 方法遇见第一个有效字符(非空格,非换行符)时,开始扫描, 直到遇见第一个分隔符或结束符(空格或换行符)时,结束扫描,光标还是在本行。
3、NextLine()等待用户输入一个文本行并且回车,该方法得到一个String类型的数据 ;
4、next(),nextInt() 不推荐和 nextLine()混合使用
//输入一行字符串,保存在字符串str中
public static void TString1(){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//test
System.out.println(str);
}
//大小为n的字符串数组:第一行n,后面n行或用空格隔开的字符串
public static void TStringN(){
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
String ss = sc.next();
list.add(ss);
}
//test
System.out.println(list);
}
//第一行输入一个整数n,第二行输入n个整数,第三行也输入n个整数;分别存放到两个数组中
public static void TNumberFix() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr1 = new int[n];
int[] arr2 = new int[n];
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
arr1[i] = a;
}
for (int i = 0; i < n; i++) {
int b = sc.nextInt();
arr2[i] = b;
}
//test
for (int i = 0; i < n; i++) {
System.out.println(arr1[i]);
}
for (int i = 0; i < n; i++) {
System.out.println(arr2[i]);
}
}
public static void TNumberN() {
//输入大小为n的数组:第一行n,后面n行或用空格隔开的数字
//数字保存在数组中了
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
arr[i] = a;
}
//test
for (int i = 0; i < n; i++) {
System.out.println(arr[i]);
}
}
//输入一行数组,用空格分开,数组长度不定;
//结果保存在数组a中
public static void TNumber1() {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] strA = str.split(" ");
int[] a = new int[strA.length];
for (int i = 0; i < strA.length; i++) {
a[i] = Integer.parseInt(strA[i]);
//a[i] = Integer.parseInt(strA[i]);
}
//test
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
//输入共两行,每一行都是一个数组,数组元素用空格分开,数组长度不确定;
// 结果存放到a数组和b数组中
public static void TNumber2() {
Scanner in = new Scanner(System.in);
int n = 2;
for (int k = 0; k < n; k++) {
}
String str = in.nextLine();
String[] strA = str.split(" ");
int[] a = new int[strA.length];
for (int i = 0; i < strA.length; i++) {
a[i] = Integer.parseInt(strA[i]);
//a[i] = Integer.valueOf(strA[i]);
}
String str2 = in.nextLine();
String[] strB = str2.split(" ");
int[] b = new int[strB.length];
for (int i = 0; i < strB.length; i++) {
b[i] = Integer.parseInt(strB[i]);
}
//=====test
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}