Rumah > Java > javaTutorial > Analisis Contoh Pokok Carian Java String, Array dan Binari

Analisis Contoh Pokok Carian Java String, Array dan Binari

WBOY
Lepaskan: 2023-04-21 09:19:08
ke hadapan
1453 orang telah melayarinya

Soalan 1

Analisis Contoh Pokok Carian Java String, Array dan Binari

Penyelesaian

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]>=&#39;a&#39;&&chars[left]<=&#39;z&#39;||(chars[left]>=&#39;A&#39;&&chars[left]<=&#39;Z&#39;)){
                tmp = chars[left];
            }else {
                left++;
                continue;
            }
            if(chars[right]>=&#39;a&#39;&&chars[right]<=&#39;z&#39;||(chars[right]>=&#39;A&#39;&&chars[right]<=&#39;Z&#39;)){
                chars[left] = chars[right];
                chars[right] = tmp;
            }else {
                right--;
                continue;
            }
            left++;
            right--;
        }
        return new String(chars);
    }
}
Salin selepas log masuk

Soalan 2

Analisis Contoh Pokok Carian Java String, Array dan Binari

Penyelesaian

/**
 * 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);
    }
}
Salin selepas log masuk

Soalan 3

Analisis Contoh Pokok Carian Java String, Array dan Binari

Penyelesaian

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;
    }
}
Salin selepas log masuk

Soalan 4

Analisis Contoh Pokok Carian Java String, Array dan Binari

Penyelesaian

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]==&#39;#&#39;){
                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();
    }
}
Salin selepas log masuk

Atas ialah kandungan terperinci Analisis Contoh Pokok Carian Java String, Array dan Binari. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:yisu.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan