剑指Offer之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();
    }
}

到此这篇关于剑指Offer之Java算法习题精讲字符串操作与数组及二叉搜索树的文章就介绍到这了,更多相关Java 字符串操作内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 剑指Offer之Java算法习题精讲二叉搜索树与数组查找

    题目一  解法 /** * 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; *

  • 剑指Offer之Java算法习题精讲二叉树与链表

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isBalanced(TreeNode root) { if(root==null){ return true; }e

  • 剑指Offer之Java算法习题精讲排列与N叉树

    题目一  解法 class Solution { LinkedList<List<Integer>> ans = new LinkedList<List<Integer>>(); public List<List<Integer>> permute(int[] nums) { LinkedList<Integer> list = new LinkedList<Integer>(); boolean[] bo =

  • 剑指Offer之Java算法习题精讲二叉树与斐波那契函数

    题目一 解法 class Solution { public int fib(int n) { int[] arr = new int[31]; arr[0] = 0; arr[1] = 1; for(int i = 2;i<=n;i++){ arr[i] = arr[i-2]+arr[i-1]; } return arr[n]; } } 题目二  解法 /** * Definition for a binary tree node. * public class TreeNode { * in

  • 剑指Offer之Java算法习题精讲链表专题篇

    题目一  解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solut

  • 剑指Offer之Java算法习题精讲数组与字符和等差数列

    题目一  解法 class Solution { public int[] relativeSortArray(int[] arr1, int[] arr2) { int[] arr = new int[1001]; int[] ans = new int[arr1.length]; int index = 0; for(int i =0;i<arr1.length;i++){ arr[arr1[i]]+=1; } for(int i = 0;i<arr2.length;i++){ while

  • 剑指Offer之Java算法习题精讲二叉树专项解析

    题目一 解法 /** * 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; * t

  • 剑指Offer之Java算法习题精讲求和篇

    题目一   解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solu

  • 剑指Offer之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'&

  • 剑指Offer之Java算法习题精讲字符串与二叉搜索树

    题目一 解法 class Solution { public boolean repeatedSubstringPattern(String a) { for (int i = 1; i <=a.length()/2 ; i++) { String s = a.substring(0, i); StringBuffer sb = new StringBuffer(); while (sb.length()<a.length()){ sb.append(s); } if(sb.toString(

  • 剑指Offer之Java算法习题精讲二叉树与N叉树

    题目一  解法 /** * 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; *

  • 剑指Offer之Java算法习题精讲二叉树专题篇下

    题目一  解法 /** * 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; *

  • 剑指Offer之Java算法习题精讲二叉树专题篇上

    来和二叉树玩耍吧~ 题目一 解法 /** * 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

随机推荐