山羊拉丁文
大致思路:
给定字符串 → 分割字符串 → 修改每个字符串对应的字符 → 将其连接在一起
Java实现
public class Demo_1 {
public static void main(String args[]){
Solution solution = new Solution();
String string = "I am a girl";
System.out.println(solution.GoatLatin(string));
}
}
class Solution {
public String GoatLatin(String string) {
String result ="";
String[] str = string.split(" ");
for(int i=0; i<str.length; i++){
if(str[i].startsWith("a")||str[i].startsWith("e")||str[i].startsWith("i")||
str[i].startsWith("o")||str[i].startsWith("u")||str[i].startsWith("A")||
str[i].startsWith("E")||str[i].startsWith("I")||str[i].startsWith("O")||
str[i].startsWith("U")){
str[i] += "ma";
}
else{
str[i] = str[i].substring(1) + str[i].substring(0,1) + "ma";
}
for(int j=0; j<i+1; j++){
str[i] += "a";
}
result = result + str[i];
if(i!=str.length-1){
result = result + " ";
}
}
return result;
}
}
public class Demo_2 {
public static void main(String args[]){
String string_2 = "we will we will rock you";
Solution_2 solution_2 = new Solution_2();
System.out.println(solution_2.findOcurrences(string_2,"we","will"));
}
}
class Solution_2 {
public Object[] findOcurrences(String text, String first, String second) {
String[] str = text.split(" ");
List list = new ArrayList<>();
for(int i = 0; i < str.length-2; i++){
if (str[i]==first && str[i+1]==(second)){
list.add(str[i + 2]);
}
}
return list.toArray(new String[list.size()]);
}
}
三角形最小路径
思路转换:
刚开始想到将其转化为一棵二叉树,但是这样中间被转换的节点很难存储
然后想到矩阵存储
对称矩阵求对角线的距离
。。。。。
猛然发现这是一个动态规划问题
import java.util.Scanner;
public class Demo_3_2 {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
int n = sca.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int min;
for(int i = 0;i<n;i++){
for(int j = 0;j<=i;j++){
a[i][j] = sca.nextInt();
}
}
b[0][0] = a[0][0];
for(int i = 1;i<n;i++){
for(int j = 0;j<=i;j++){
if(j==0)//左侧,直接相加
b[i][j] = b[i-1][j]+a[i][j];
else if(j==i)//右侧,直接相加
b[i][j] = b[i-1][j-1]+a[i][j];
else//中间,需要用min函数求经过这条路的最短路径
b[i][j] = Math.min(b[i-1][j-1],b[i-1][j])+a[i][j];
}
}
min = b[n-1][0];
for(int i = 1;i<b[n-1].length;i++){
if(b[n-1][i]<min)
min = b[n-1][i];
}
System.out.println(min);
}
}
叶子相似的树
设定根节点,建立二叉树,从根节点处进行遍历
list的使用 在这里会很方便
/*
请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 。
举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树。
如果有两颗二叉树的叶值序列是相同,那么我们就认为它们是 叶相似 的。
如果给定的两个头结点分别为 root1 和 root2 的树是叶相似的,则返回 true;否则返回 false 。
*/
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class Demo_4{
public static void main(String args[]){
Solution_4 solution_4 = new Solution_4();
TreeNode root1 = new TreeNode(2);
TreeNode root2 = new TreeNode(2);
System.out.println(solution_4.leafSimilar(root1,root2));
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Solution_4 {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
if(root1==null&&root2==null){
return true;
}
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
list1.add(2);list1.add(2);list1.add(2);list1.add(2);
list2.add(2);list2.add(2);list2.add(2);list2.add(2);
interator(list1,root1);
System.out.println(list1);
interator(list2,root2);
System.out.println(list2);
if(list1.size()==list2.size()){
for(int i=0;i<list1.size();i++){
if(list1.get(i)!=list2.get(i)){
return false;
}
}
}else {
return false;
}
return true;
}
private void interator(List<Integer> list,TreeNode root){
if(root.left==null&&root.right==null){
list.add(root.val);
}
if(root.left!=null){
interator(list,root.left);
}
if(root.right!=null){
interator(list,root.right);
}
}
}
二叉树中所有距离为k的点
保存每个节点步长为1的相邻节点,找到已知节点的相邻节点的步长为1的相邻节点
借鉴想法:
将树转化为无向图,寻找cost为2的结点
class Solution {
private Map<TreeNode, TreeNode> pmap = new HashMap<>();
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
dfs(root, null);
return helper(root, target, K);
}
private List<Integer> helper(TreeNode root, TreeNode target, int k) {
List<Integer> rlist = new LinkedList<>();
if (root == null) return rlist;
List<TreeNode> nodeList = new LinkedList<>();
nodeList.add(target);
int level = 0;
Set<TreeNode> seenSet = new HashSet<>();
while (!nodeList.isEmpty()) {
int size = nodeList.size();
List<Integer> ilist = new LinkedList<>();
seenSet.add(target);
for (int i = 0; i < size; i++) {
TreeNode node = nodeList.remove(0);
ilist.add(node.val);
if (node.left != null && seenSet.add(node.left)) nodeList.add(node.left);
if (node.right != null && seenSet.add(node.right)) nodeList.add(node.right);
if (pmap.containsKey(node)&&seenSet.add(pmap.get(node))){
nodeList.add(pmap.get(node));
}
}
if (++level==k+1) {
rlist = new LinkedList<>(ilist);
}else {
ilist.clear();
}
}
return rlist;
}
private void dfs(TreeNode node, TreeNode parent) {
if (node == null) return;
if (parent != null) {
pmap.put(node, parent);
}
dfs(node.left, node);
dfs(node.right, node);
}
}