剑指offer java实现合集(6)第26~30题

26.二叉搜索树与双向链表

模仿中序遍历的思路,但是这次是从右向左进行。

public class Solution {
    TreeNode temp = null;
    public TreeNode Convert(TreeNode pRootOfTree) {
      
       if(pRootOfTree==null){
           return null;
       }
       Convert(pRootOfTree.right);
       if(temp==null){
           temp = pRootOfTree;
       }else{
           temp.left = pRootOfTree;
           pRootOfTree.right = temp;
           temp = pRootOfTree;
       }
        Convert(pRootOfTree.left);
        return temp;
    }
}

27.字符串的排列

 

import java.util.ArrayList;
import java.util.*;
public class Solution {
    ArrayList<String> res = new ArrayList();
    public ArrayList<String> Permutation(String str) {
        ArrayList<String> res = new ArrayList();
        int len = str.length();
        if(len==0){
            return res;
        }
        char []cha = str.toCharArray();
        getres(cha,0,len,res);
        Collections.sort(res);
        return res;
    }
    public ArrayList<String> getres(char[] cha,int i ,int len,ArrayList list){
        if(i==len-1){
            String temp = String.valueOf(cha);
            if(!list.contains(temp)){
                list.add(temp);
            }
        }else{
            for(int j = i;j<len;j++){
                swap(cha,i,j);
                getres(cha,i+1,len,list);
                swap(cha,i,j);
            }
        }
        return list;
    }
    public void swap(char cha[],int i ,int j){
        char x = cha[i];
        cha[i] = cha[j];
        cha[j] = x;
    }
}

28.数组中超过出现次数超过一半的数字

排序之后,如存在这样的数,取中位数就是答案,然后验证出现的次数是否符合超过半数的设定。

import java.util.*;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        Arrays.sort(array);
        int len = array.length;
        int mid = len/2;
        int res = array[mid];
        int count = 0;
        for(int x :array){
            if(x==res){
                count++;
            }
        }
        if(count>mid){
            return res;
        }else{
            return 0;
        }
    }
}

29.最小的K个数

考察的应该是堆排序,后续会添加上。

import java.util.*;
public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> arr = new ArrayList();
        int len = input.length;
        if(len==0||k>len){
            return arr;
        }
        Arrays.sort(input);
        int i =0;
        while(k>0){
            arr.add(input[i]);
            i++;
            k--;
        }
        return arr;
    }
}

30.连续子数组的最大和

能让整体的和更大的方式,就是之前数的和为整数,如果为负数,就抛弃之前所有的数,以当前数作为起点。

public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        int res = array[0];
        int temp = array[0];
        for(int i =1;i<array.length;i++){
            if(temp>=0){
                temp = temp+array[i];
            }else{
                temp = array[i];
            }
            if(temp>res){
                res = temp;
            }
        }
        return res;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值