欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

剑指Offer之Java算法习题精讲字符串操作与数组及二叉搜索树

时间:2022-10-06 11:59:58|栏目:JAVA代码|点击:

题目一

 解法

class Solution {
    public String reverseOnlyLetters(String s) {
        char[] chars = s.toCharArray();
        int left = 0;
        int right = chars.length-1;
        while(left<=right){
            char tmp = 0;
            if(chars[left]>='a'&&chars[left]<='z'||(chars[left]>='A'&&chars[left]<='Z')){
                tmp = chars[left];
            }else {
                left++;
                continue;
            }
            if(chars[right]>='a'&&chars[right]<='z'||(chars[right]>='A'&&chars[right]<='Z')){
                chars[left] = chars[right];
                chars[right] = tmp;
            }else {
                right--;
                continue;
            }
            left++;
            right--;
        }
        return new String(chars);
    }
}

题目二

解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode increasingBST(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        method(root,list);
        TreeNode ans = new TreeNode(-1);
        TreeNode cur = ans;
        for(int i:list){
            TreeNode node = new TreeNode(i);
            cur.right = node;
            cur = cur.right;
        }
        return ans.right;
    }
    public void method(TreeNode root,List<Integer> list){
        if(root==null) return;
        method(root.left,list);
        list.add(root.val);
        method(root.right,list);
    }
}

 题目三

解法

class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int[] ans = new int[nums.length];
        int left = 0;
        int right = nums.length-1;
        for(int i : nums){
            if(i%2==0){
                ans[left] = i;
                left++;
            }else{
                ans[right] = i;
                right--;
            }
        }
        return ans;
    }
}
 
class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while(left<=right){
            if(nums[left]%2==0){
                left++;
                continue;
            }
            if(nums[right]%2!=0){
                right--;
                continue;
            }
            if(nums[left]%2!=0&&nums[right]%2==0){
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
            }
        }
        return nums;
    }
}

 题目四

 解法

class Solution {
    public boolean backspaceCompare(String s, String t) {
        if(method(s).equals(method(t))) return true;
        return false;
    }
    public static String method(String s){
        int slow = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]=='#'){
                chars[i] = 0;
                slow = i;
                while (true){
                    if(slow-1<0) break;
                    if (chars[slow-1]!=0){
                        chars[slow-1] = 0;
                        break;
                    }
                    slow--;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for(char i : chars){
            if(i!=0) sb.append(i);
        }
        return sb.toString();
    }
}

上一篇:Lombok基本注解之@SneakyThrows的作用

栏    目:JAVA代码

下一篇:java实现简单的图书借阅系统

本文标题:剑指Offer之Java算法习题精讲字符串操作与数组及二叉搜索树

本文地址:http://www.codeinn.net/misctech/215626.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有