public class Solution {
public int TreeDepth(TreeNode root) {
if(root == null){
return 0;
}else{
return Math.max(TreeDepth(root.left) + 1,TreeDepth(root.right) + 1);
}
}
}
package com.example.demo;
public class TrreeNode {
public static void main(String[] args) {
Node tree = Node.getTree();
System.out.println(judke(tree));
}
public static int getTreeDeepth(Node node){
if(node == null){
return 0;
}else {
return Math.max(getTreeDeepth(node.left)+1,getTreeDeepth(node.right)+1);
}
}
public static boolean judke(Node node){
if(node == null){
return true;
}else {
int left = 0;
int right = 0;
if(node.left != null){
left = getTreeDeepth(node.left);
}else {
left = 0;
}
if(node.right != null){
right = getTreeDeepth(node.right);
}else {
right = 0;
}
if(Math.abs(left - right) <= 1){
return judke(node.left )&&judke(node.right);
}else {
return false;
}
}
}
}
package com.example.demo;
public class FindNum {
public static void main(String[] args) {
}
public static void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
if(array.length == 0){
return;
}
int[] arr = new int[2];
int j = 0;
for (int i = 0; i < array.length; i++) {
if(LastIndexOf(array,i)){
if(j == 2){
break;
}
arr[j++] = array[i];
}
}
num1[0] = arr[0];
num2[0] = arr[1];
}
public static boolean LastIndexOf(int[] array, int find){
int begin = -1;
int end = -1;
for (int i = array.length - 1; i >= 0; i--){
if(array[i] == array[find]){
end = i;
break;
}
}
for (int i = 0; i < array.length; i++) {
if(array[i] == array[find]){
begin = i;
break;
}
}
if(begin == end){
return true;
}else {
return false;
}
}
}
package com.example.demo;
import java.util.ArrayList;
public class AddSth {
public static void main(String[] args) {
ArrayList> array = getArray(100);
for(ArrayList list : array){
for(Integer integer : list){
System.out.print(integer + " ");
}
System.out.println();
}
}
public static ArrayList> getArray(int num){
ArrayList> arrayLists = new ArrayList<>();
for (int i = num; i > 1; i--) {
if(i % 2 == 1){
ArrayList array = getSingle(num, i);
if(array != null){
arrayLists.add(array);
}
}else {
ArrayList aDouble = getDouble(num, i);
if(aDouble != null){
arrayLists.add(aDouble);
}
}
}
return arrayLists;
}
public static ArrayList getDouble(int num,int k){
ArrayList arrayList = new ArrayList<>();
//最中间的两个数的和必须是奇数,不是奇数则意味着不可能再分下去
int flag = k / 2;
if(num / flag % 2 != 1){
return null;
}
//分成的每一组的和必须是整数(两个数一组)
if(num % flag != 0){
return null;
}
int mid_start = num / flag / 2;
if(mid_start - flag + 1 <= 0){
return null;
}else {
for (int i = 0; i < k; i++) {
arrayList.add(mid_start - flag + 1 + i);
}
}
return arrayList;
}
public static ArrayList getSingle(int num,int k){
ArrayList arrayList = new ArrayList<>();
//最中间的数必须是整数,不是整数则不行
if(num % k != 0){
return null;
}else {
//第一个数必须大于等于1,不然是错误的
int mid = num / k;
int reduce = k /2;
if(mid - reduce <= 0){
return null;
}
else {
for (int i = 0; i < k; i++) {
arrayList.add(mid - reduce + i);
}
return arrayList;
}
}
}
}
package com.example.demo;
import java.util.ArrayList;
public class AddNum {
public static void main(String[] args) {
}
public static ArrayList get(int[] array,int k) {
if(array.length == 0){
return new ArrayList();
}
int flag = Integer.MAX_VALUE;
int index = 0;
ArrayList
package com.example.demo;
public class StrGoLeft {
public static void main(String[] args) {
System.out.println(GoLeft("1234", 6));
}
public static String GoLeft(String str, int k){
if(str.length() == 0){
return str;
}
int flag = k;
if(k == 0){
return str;
}
if(k > str.length()){
flag = k % str.length();
}
String substring = str.substring(0, flag);
String remain = str.substring(flag,str.length());
return remain + substring;
}
}
字符串的内容全部是" "的情况。
public class Solution {
public String ReverseSentence(String str) {
return reverse(str);
}
public static String reverse(String str){
if(str == null || str.length() == 0){
return str;
}
boolean flag = false;
for(int i = 0;i < str.length();i++){
if(str.charAt(i) != ' '){
flag = true;
break;
}
}
if(!flag){
return str;
}
String[] s = str.split(" ");
String string = new String();
for (int i = s.length - 1; i > 0; i--) {
string += s[i] + " ";
}
return string + s[0];
}
}
思路:除0之外的最大值和最小值的差不超过4则就可以。
package com.example.demo;
import java.util.ArrayList;
import java.util.Arrays;
public class PukePai {
public static void main(String[] args) {
System.out.println(judje(new int[]{1, 3, 0, 5, 0}));
}
public static int[] getPai(){
ArrayList objects = new ArrayList<>();
for (int i = 1; i <= 13; i++) {
objects.add(i);
objects.add(i);
objects.add(i);
objects.add(i);
}
objects.add(0);
objects.add(0);
int[] ints = new int[5];
for (int i = 0; i < ints.length; i++) {
Integer remove = objects.remove((int)(Math.random() * (54 - i)));
ints[i] = remove;
}
for (int i = 0; i < ints.length; i++) {
System.out.println(ints[i]);
}
return ints;
}
public static boolean judje(int[] array){
if(array == null || array.length< 5){
return false;
}
Arrays.sort(array);
int start = 0;
if(array[start] == 0){
start ++;
}
for(int i = start;i < array.length - 1; i++){
while (array[i + 1] == array[i]){
return false;
}
}
if(array[4] - array[start] <= 4){
return true;
}else {
return false;
}
}
}
如果没有小朋友,请返回-1
package com.example.demo;
import java.util.ArrayList;
public class ChooseChild {
public static void main(String[] args) {
System.out.println(choose(5, 3));
}
public static int choose(int n, int m){
if(n == 0){
return -1;
}
ArrayList objects = new ArrayList<>();
for (int i = 0; i < n; i++) {
objects.add(i);
}
int i = 0;
int start = 0;
int remove = 0;
while (objects.size() > 1){
//第一次定位remove需要减1,后面已经定位好了,就不需要减少1了
if(i == 0) {
remove = (start + m) % (n - i) - 1;
}else {
remove = (start + m) % (n - i);
}
if(remove < 0){
remove = objects.size() - 1;
}
System.out.println("删除的数" + objects.get(remove));
objects.remove(remove);
//指针往后移动一个,便于计算要删除的指针代表的数
start = remove - 1;
if(start < 0){
start = objects.size() - 1;
}
i ++;
}
return objects.get(0);
}
}
public class Solution {
public int Sum_Solution(int n) {
return (int)(Math.pow(n,2) + n)>>1;
}
}
public class Add {
public static void main(String[] args) {
System.out.println(Add(3, 5));
}
public static int Add(int a,int b) {
if(b == 0){
return a;
}
else{
return Add(a^b, (a&b&0xffffffff)<<1);
}
}
}
public class Add {
public static void main(String[] args) {
System.out.println(Add("123"));
}
public static int Add(String str) {
if(str == null || str.length() == 0){
return 0;
}
long n = 0;
long j = 0;
for (int i = str.length()-1; i >= 1; i--) {
if('0' <= str.charAt(i) && str.charAt(i)<= '9'){
n = n + (str.charAt(i) - '0')* (long)Math.pow(10,j++);
}else {
return 0;
}
}
if(str.charAt(0) == '+'){
if(n > Integer.MAX_VALUE){
return 0;
}else {
return (int)n;
}
}else if(str.charAt(0) == '-'){
if(-n < -Integer.MAX_VALUE-1){
return 0;
}else {
return (int)-n;
}
}else if('0' <= str.charAt(0) && str.charAt(0)<= '9'){
n = n + (str.charAt(0) - '0')*(int)Math.pow(10,str.length()-1);
if(n > Integer.MAX_VALUE){
return 0;
}else {
return (int)n;
}
}else {
return 0;
}
}
}
package com.example.demo;
public class Match2 {
public static void main(String[] args) {
}
public static boolean isNumeric(char[] str) {
String s = String.valueOf(str);
return s.matches("[\\+\\-]?\\d*(\\.\\d+)?([eE][\\+\\-]?\\d+)?");
}
}
package com.example.demo;
public class StrStream {
static String str = "";
static int[] array = new int[256];
public static void main(String[] args) {
Insert('h');
FirstAppearingOnce();
Insert('e');
FirstAppearingOnce();
Insert('l');
FirstAppearingOnce();
Insert('l');
FirstAppearingOnce();
Insert('o');
FirstAppearingOnce();
Insert('w');
FirstAppearingOnce();
}
public static void Insert(char ch)
{
str += ch;
array[ch]++;
}
public static char FirstAppearingOnce()
{
if(str == null || str.length() == 0){
return '#';
}
for(int i=0; i < str.length(); i++){
if(array[str.charAt(i)] == 1){
System.out.println(str.charAt(i));
return str.charAt(i);
}
}
System.out.println('#');
return '#';
}
}
结果:
h
h
h
h
h
h
思路:
设置快慢指针,都从链表头出发,快指针每次走两步,慢指针一次走一步,假如有环,一定相遇于环中某点(结论1)。接着让两个指针分别从相遇点和链表头出发,两者都改为每次走一步,最终相遇于环入口(结论2)。以下是两个结论证明:
两个结论:
1、设置快慢指针,假如有环,他们最后一定相遇。
2、两个指针分别从链表头和相遇点继续出发,每次走一步,最后一定相遇与环入口。
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead)
{
ListNode quick = pHead;
ListNode slow = pHead;
while(true){
if(quick.next != null){
quick = quick.next;
}else{
return null;
}
if(quick.next != null){
quick = quick.next;
}else{
return null;
}
if(slow.next != null){
slow = slow.next;
}else{
return null;
}
if(quick == slow){
break;
}
}
quick = pHead;
while(quick != slow){
quick = quick.next;
slow = slow.next;
}
return quick;
}
}