目录
学习内容:
LintCode刷题:
·编写对象属性的赋值方法和打印方法
对象赋值,重写下printInfo()方法即可
public class Employee {
String name;
String occupation;
String telephone;
int age;
int salary;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getOccupation() { return occupation; }
public void setOccupation(String occupation) { this.occupation = occupation; }
public String getTelephone() { return telephone; }
public void setTelephone(String telephone) { this.telephone = telephone; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public int getSalary() { return salary; }
public void setSalary(int salary) { this.salary = salary; }
public void printInfo(){
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("Telephone: "+telephone);
System.out.println("Occupation: "+occupation);
System.out.println("Salary: "+salary);
}
}
·打印九九乘法表
两层for循环遍历即可,记得区分i、j的位置还有末尾不要有空格
public class Main {
public static void main(String[] args) {
for (int i=1;i<10;i++){
for (int j=1;j<=i;j++){
if (i==j)
System.out.println(j+"*"+i+"="+i*j);
else
System.out.print(j+"*"+i+"="+i*j+" ");
}
}
}
}
·两个字符串是否相等
用string的 equalsIgnoreCase方法即可
import java.util.*;
public class Solution {
public boolean judgmentString(String str1,String str2) {
return str1.equalsIgnoreCase(str2);
}
}
·Integer的取值范围
对于Integer 的赋值,当取值范围在 -128~127之间时,会去从字符串常量池中获取数据,否则会 new
一个新的对象进行赋值。
public class Solution {
public boolean Interval(Integer a, Integer b) {
return a.equals(b);
}
}
· 交换两个整数
定义一个变量来交换即可
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c =a;
a=b;
b=c;
System.out.println("a = " + a + ", b = " + b);
}
}
·二叉树的倾斜程度
第一种建立数组用来存入自己这颗树的总值(所有子节点相加,再加上root值),和当前root中左节点和右节点的绝对值。不停使用方法的递归,返回数组中第二个数值即可。
public class Solution {
public int findTilt(TreeNode root) {
return countTree(root)[1];
}
public int[] countTree(TreeNode root) {
if (root == null) return new int[]{0, 0};
int[] left = countTree(root.left);
int[] right = countTree(root.right);
return new int[]{root.val + left[0] + right[0], left[1] + right[1] + Math.abs(left[0] - right[0])};
}
public static void main(String[] args) {
Solution solution = new Solution();
TreeNode treeNode = new TreeNode(1);
TreeNode treeNodeleftroot = new TreeNode(3);
TreeNode treeNoderightroot = new TreeNode(4);
treeNode.left = treeNodeleftroot;
treeNode.right = treeNoderightroot;
TreeNode treeNodeleft = new TreeNode(1);
TreeNode treeNoderight = new TreeNode(9);
treeNodeleftroot.right = treeNoderight;
treeNodeleftroot.left = treeNodeleft;
System.out.println(Arrays.toString((solution.countTree(treeNode))));
}
}
第二种就是不停的递归左节点后右节点,每次递归将res+=左节点右节点相差之数即可
public class Solution {
int res = 0;
public int findTilt(TreeNode root) {
// Write your code here
postOrder(root);
return res;
}
public int postOrder(TreeNode root)
{
if(root == null)
return 0;
int leftSum = postOrder(root.left);
int rightSum = postOrder(root.right);
res+=Math.abs(leftSum-rightSum);
return leftSum+rightSum+root.val;
}
}
学习时间:
2021-8-25 9:00-11:45、13:30-17:00、18:30-21:00
学习产出:
项目调试
刷题*6
学习笔记*1