您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Java字符串,數組及二叉搜索樹實例分析”,內容詳細,步驟清晰,細節處理妥當,希望這篇“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(); } }
讀到這里,這篇“Java字符串,數組及二叉搜索樹實例分析”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。