Java十大经典排序算法的实现图解
322
2022-08-19
剑指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() sb.append(s); } if(sb.toString().equals(a)){ return true; } } return false; } } 题目二 解法 class Solution { public boolean detectCapitalUse(String word) { if(word.toLowerCase().equals(word)||word.toUpperCase().equals(word)||word.substring(1, word.length()).toLowerCase().equals(word.substring(1, word.length()))) return true; return false; } } 题目三 解法 二叉搜索树有个性质为二叉搜索树中序遍历得到的值序列是递增有序的 /** * 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; * } * } */ claswtYdotDFs Solution { int ans; int pre; public int getMinimumDifference(TreeNode root) { ans = Integer.MAX_VALUE; pre = -1; method(root); return ans; } public void method(TreeNode root){ if(root==null){ return; } method(root.left); if(pre==-1){ pre = root.val; }else{ ans = Math.min(ans, root.val - pre); pre = root.val; } method(root.right); } } 题目四 解法 class Solution { public String reverseStr(String a, int k) { int con = 1; StringBuffer sb = new StringBuffer(); while (con*k<=a.length()){ String substring = a.substring((con - 1) * k, con * k); if(con%2==0){ sb.append(substring); con++; }else { for (int i1 = substring.length()-1; i1 >=0 ; i1--) { sb.append(substring.charAt(i1)); } con++; } } if((con-1)*k String s = a.substring((con-1) * k, a.length()); if(con%2!=0){ for (int i1 = s.length()-1; i1>=0; i1--) { sb.append(s.charAt(i1)); } }else { sb.append(s); } } http:// return sb.toString(); } }
sb.append(s);
}
if(sb.toString().equals(a)){
return true;
}
}
return false;
}
}
题目二
解法
class Solution {
public boolean detectCapitalUse(String word) {
if(word.toLowerCase().equals(word)||word.toUpperCase().equals(word)||word.substring(1, word.length()).toLowerCase().equals(word.substring(1, word.length()))) return true;
return false;
}
}
题目三
解法
二叉搜索树有个性质为二叉搜索树中序遍历得到的值序列是递增有序的
/**
* 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;
* }
* }
*/
claswtYdotDFs Solution {
int ans;
int pre;
public int getMinimumDifference(TreeNode root) {
ans = Integer.MAX_VALUE;
pre = -1;
method(root);
return ans;
}
public void method(TreeNode root){
if(root==null){
return;
}
method(root.left);
if(pre==-1){
pre = root.val;
}else{
ans = Math.min(ans, root.val - pre);
pre = root.val;
}
method(root.right);
}
}
题目四
解法
class Solution {
public String reverseStr(String a, int k) {
int con = 1;
StringBuffer sb = new StringBuffer();
while (con*k<=a.length()){
String substring = a.substring((con - 1) * k, con * k);
if(con%2==0){
sb.append(substring);
con++;
}else {
for (int i1 = substring.length()-1; i1 >=0 ; i1--) {
sb.append(substring.charAt(i1));
}
con++;
}
}
if((con-1)*k String s = a.substring((con-1) * k, a.length()); if(con%2!=0){ for (int i1 = s.length()-1; i1>=0; i1--) { sb.append(s.charAt(i1)); } }else { sb.append(s); } } http:// return sb.toString(); } }
String s = a.substring((con-1) * k, a.length());
if(con%2!=0){
for (int i1 = s.length()-1; i1>=0; i1--) {
sb.append(s.charAt(i1));
}
}else {
sb.append(s);
}
}
http:// return sb.toString();
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~