Sword refers to offer1

1.Insert picture description here

package com.example.test;

public class offer01 {
    
    
	public boolean Find(int target, int[][] array) {
    
    
		int lines = array.length;
		int cols = array[0].length;

		int x = 0;
		int y = cols - 1;

		while (x < lines && y >= 0) {
    
    
			if (array[x][y] == target)
				return true;
			if (array[x][y] > target)
				y--;
			else
				x++;

		}
		return false;
	}
}

2.Insert picture description here

package com.example.test;

public class offer02 {
    
    
	// 直接替换

	public String replaceSpace(StringBuffer str) {
    
    
		// return str.toString().replace("", "%20");

		// 手动模拟;
		StringBuffer result = new StringBuffer();
		for (char ch : str.toString().toCharArray()) {
    
    
			if (ch == ' ')
				result.append("%20");
			else
				result.append(ch);
		}
		return result.toString();
	}

	public static void main(String[] args) {
    
    
		String result = new offer02().replaceSpace(new StringBuffer("we are happy"));
		System.out.println(result);
	}
}

String:不可变,final声明的字符数组
StringBuilder:可变,不加锁,速度快
StringBuffer:可变,加锁,线程安全,速度慢一点

3.Insert picture description here

package com.example.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class offer03 {
    
    

	public static void main(String[] args) {
    
    
		//直接使用Collections的逆序操作函数
		ArrayList a = new ArrayList();
		a.add(1);
		a.add(2);
		a.add(3);
		a.add(4);
		a.add(5);
		Collections.reverse(a);
		System.out.println(a);
		
		//ArrayList插入,新插入的元素会把之前的往后顶
		a=new ArrayList();
		a.add(0, 1);
		a.add(0, 2);
		a.add(0, 3);
		a.add(0, 4);
		a.add(0, 5);
		System.out.println(a);		
	}
}
ArrayList是list接口的可变数组的实现。查询快,增删慢。
LinkedList是List接口的双向链表的实现。查询慢,增删快

Insert, push the previous one to the back Insert picture description here
Recursive Insert picture description here
4.Insert picture description here

public class BuildTree {
    
    
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
    
    
        TreeNode root=ConstructCore(pre,0,pre.length-1,in,0,in.length-1);
        return root;
    }
 
    public TreeNode ConstructCore(int[] pre,int startPre,int endPre,int[] in,int startIn,int endIn)
    {
    
    
        if(startPre>endPre||startIn>endIn)
            return null;
        TreeNode node = new TreeNode(pre[startPre]);
        for(int i=startIn;i<=endIn;i++)
        {
    
    
            if(in[i]==pre[startPre])
            {
    
    
                node.left = ConstructCore(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
                node.right =ConstructCore(pre,startPre+i-startIn+1,endPre,in,i+1,endIn);
                break;
            }
        }
        return node;
 
    }
}
 
class TreeNode {
    
    
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode(int x) {
    
     val = x; }
 }

5. Insert picture description here
Insert picture description here
6.
Insert picture description here
Insert picture description here
7. Fibonacci sequence
Insert picture description here
recursion
Insert picture description here
conventional Insert picture description here
optimized storage
Insert picture description here
8/9. Fibonacci sequence Insert picture description here
Insert picture description here
10. Fibonacci sequence
Insert picture description here
Insert picture description here
11.Insert picture description here

public class offer05 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println(new offer05().number(10));
	}

	public int number(int n) {
    
    
		int ans = 0;
		for (int i = 0; i < 32; i++) {
    
    
			if ((n & 1) == 1)
				ans++;
			n = n >> 1;
		}
		return ans;
	}
}

12. Insert picture description here
Even bubble sort, odd bubble sort

public class offer06 {
    
    
	public static void main(String[] args) {
    
    
		int[] arr = new int[] {
    
    4,3,1,2,2,5};
		new offer06().reoder(arr);
		System.out.println(Arrays.toString(arr));
	}

	public void reoder(int[] array) {
    
    
		for (int i = array.length - 1; i >= 0; i--) {
    
    
			for (int j = 0; j + 1 <= i; j++) {
    
    
				if (array[j] % 2 == 0 && array[j + 1] % 2 == 1) {
    
    
					swap(array, j, j+1);
				}
			}
		}
	}

	public void swap(int[] array, int i, int j) {
    
    
		int t = array[j];
		array[j] = array[i];
		array[i] = t;
	}
}

13.Insert picture description here
Insert picture description here
14.
Insert picture description here
Insert picture description here
15.
v
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45530931/article/details/109264474