Offer to prove safety problems brush summary

1. Find a two-dimensional array

(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

Thinking problems

From the upper right or lower left began to narrow.

2. Replace spaces

Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.

Grammar questions

3. The print head from the end of the list

Enter a list, by returning a list sequentially from the tail to the head of ArrayList.

Head interpolation

Array reverse

Stack

Recursion

4. reconstruction of binary tree

And enter the result in a preorder traversal of a binary tree in preorder traversal of the binary tree a rebuild. Suppose Results preorder traversal order and input of duplicate numbers are free. Before entering e.g. preorder traversal sequence {1,2,4,7,3,5,6,8} and {4,7,2,1,5,3,8,6} order traversal sequence, and the reconstructed binary tree return.

Recursion

API:Arrays.copyOfRange()

The two stacks queue

Two stacks to achieve a queue, the completion queue Push and Pop operations. Queue elements int.

Simple question

6. The minimum number of rotation of the array

The beginning of an array of several elements moved to the end of the array, the array we call rotation.
A non-descending order of the input array of a rotation of the output rotary smallest element array.
For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum.
NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.

Bipartite find the point of rotation

7. Fibonacci number

We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0).
n <= 39

Optimized to non-recursive

8. jump stairs

A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).

Fibonacci number

9. metamorphosis jump stairs

A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps.

Recursion

Since the n steps, the first step there are n kinds of jump method: 1 skip, skip 2 to n stages hop
skip 1, the remaining level n-1, the remaining jumps is f (n-1)
hop stage 2, the remaining n-2 stage, the remaining jumps is F (n-2)
so that f (n) = f (n -1) + f (n-2) + ... + f (1)
since f (n-1) = f (n-2) + f (n-3) + ... + f (1) so that f (n) = 2 * f (n-1)

10. A matrix cover

We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle. Will the small rectangle of n 2 * 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?

For n = 3 for example, 2 * 3 tile is covered There are three methods:

Recursion

11. The number of binary 1

An integer that indicates the number of the output of the binary number 1 in. Wherein a negative number indicates a complement.

Grammar questions

12. The value of the integer power

Given a double base type and floating-point type int integer exponent. Seeking exponent of the power base.
Ensure the base and exponent are not simultaneously 0

Grammar questions

13. The order of the array so that the adjustment in front of even-odd

Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array, is located in the second half of all the even array, and between odd and ensure a relatively even, odd and even the same position.

Insertion Sort

Time for space

Create an array

Space for time

14. A linked list node k-th countdown

Input a linked list, the linked list output reciprocal k-th node.

Double pointer

15. Reverse list

After entering a list inverted list, the new list of the output header.

Grammar questions

16. merge two sorted lists

Two monotonically increasing input list and output list after synthesis of two lists, of course, after that we need to meet synthesis list - decreasing the rules.

Grammar questions

17. substructure tree

Two binary inputs A, B, B is judged not substructure A's. (Ps: we agreed empty tree is not a tree any substructure)

Recursion

18. The binary image

Operation of a given binary tree, the binary tree is converted into a source image.

           8
           /  \
          6   10
         / \  / \
        5  7 9 11
        镜像二叉树
            8
           /  \
          10   6
         / \  / \
        11 9 7  5

First Traversal

19. Clockwise print matrix

Enter a matrix, in order from outside to inside in a clockwise order to print out sequentially for each number, for example, if you enter the following 4 X 4 matrix: 1,234,567,891,011,121,314 15 sequentially printed out 16 digital 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

Delimited by four pointers

20. The stack includes a function min

Stack data structure definition, implement this type can be a min function smallest elements contained in the stack (should the time complexity O (1)).
Note: to ensure that the test is not when the stack is empty, calls on the stack pop () or min () or top () method.

Two stacks

Here is a clever design:
the Push (), two stacks have to add a new element;
POP () when the two elements of the stack are deleted.

21. The stack push, pop sequence

Two input sequence of integers, the first sequence representing a pressed stack order, determines whether it is possible for the second sequence the order of the pop-up stack. All figures are not pushed onto the stack is assumed equal. Such as a sequence of a sequence of 1,2,3,4,5 is pressed into the stack, the push sequence is 4,5,3,2,1 sequence corresponding to a sequence of pop, but 4,3,5,1,2 it is impossible to push pop-up sequence of the sequence. (Note: the length of the two sequences are equal)

Analog stack

22. The print binary tree from the top down

Downward from the print out of each node of the binary tree, with the layer node from left to right printing.

queue

23. The subsequent binary search tree traversal sequence

Enter an integer array, the array is not the result of the determination after traversing a binary search tree. If the output Yes, otherwise the output No. Suppose the input array of any two numbers are different from each other.

Recursion

Legitimate sequence sequence sequence is BST, for a sequence S, is the last element of x (i.e. the root), if the sequence is to remove the last element T, then T satisfies: T can be divided into two sections, the front section (left subtree) is less than x, after a period (right subtree) is greater than x, and these two (subtree) of the subsequent sequences are legitimate. Perfect recursive definition:).

24. A binary tree is a path of a certain value and

An input binary tree root node and an integer, the binary print values ​​of nodes in the input path and all integers. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)

Preorder traversal

25. The list of replication complex

A complex input list (each node has a node value, and two pointers, one pointing to the next node, a special pointer to any other node), returns a value after the head of the list replication complex. (Note that the output results do not return parameter node reference, otherwise the program will return empty sentenced questions directly)

hashmap

  1. Traversing the original list, copy the mapping list node by node hashMap keep the original list
  2. Traversing the new list, find the corresponding random

26. The binary search trees and doubly-linked list

Enter a binary search tree, the binary search tree converted into a doubly linked list sorted. Requirements can not create any new node, point to only adjust the tree node pointer.

Preorder

27. sort strings

There are a number of array number that appears more than half the length of the array, find this number. For example, a length of the input array 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears five times, more than half the length of the array in the array, the output 2. If there is 0 output.

Recursion

for (int i = begin; i < arr.length; i++) {
    swap(arr, begin, i);
    permutation(arr, begin + 1);
    swap(arr, begin, i);
}

More than half of the number 28. The digital array appears

There are a number of array number that appears more than half the length of the array, find this number. For example, a length of the input array 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears five times, more than half the length of the array in the array, the output 2. If there is 0 output.

hashmap

29. The minimum number k

Input n integers, find the smallest number K. 4,5,1,6,2,7,3,8 e.g. eight digital inputs, the minimum number is four 1,2,3,4 ,.

The maximum heap

30. The maximum and continuous subarray

HZ occasionally get some professional issues to flicker those non-computer science students. After the test group will finish today, he spoke up: in the old one-dimensional pattern recognition, it is often necessary to calculate the maximum and continuous sub-vector when the vector is a positive whole number, the problem solved. However, if the vector contains a negative number, it should contain a negative number, and expect a positive number next to it would make up for it? For example: {6, -3, -2,7, -15,1,2,2}, and the maximum successive sub-vectors of 8 (beginning from 0, up to the third). To an array and returns its maximum continuous subsequence and you will not be fooled him live? (Sub-vector length is at least 1)

Dynamic Programming

31. The integer number 1 appears

Calculated the number of integer of 1 to 13 1 arise, and calculates the number of times an integer from 100 to 1300 in 1 occur? To this end he especially counted about 1 to 13 contains the digits 1 has 1,10,11,12,13 therefore appear a total of six times, but for the problem behind him Meizhe. ACMer hope you will help him, and the problem is more generalized, the number of non-negative integer in the range 1 appears (the number of occurrences from 1 to n. 1) can quickly find any.

Bitwise analysis

The main idea : there are for each digit number included 1 point for analysis.

The integer position set is divided to n, is divided into two parts, high n / i, n% i lower

Let us consider the case of n by the corresponding number of one hundred, to analyze:

  1. When i represents one hundred and one hundred corresponding to the number> = 2, as n = 31456, i = 100, then a = 314, b = 56, this time to one hundred times with a 1 a / 10 + 1 = 32 (up to two 0 to 31), each time with 100 consecutive points, i.e., a total of (a / 10 + 1) * 100 points to a one hundred.

  2. When i represents one hundred and one hundred corresponding to the number 1, such as n = 31156, i = 100, then a = 311, b = 56, corresponding to one hundred at this time is 1, the total of a / 10 = 31 ( two highest 0-30) comprising 100 contiguous time points, when the top two bits is 31 (i.e., a = 311), this corresponds to only 00 to 56 local points, a total of b + 1 times, the combined total of all the points ( a / 10 * 100) + (b + 1), which corresponds to a one hundred points.
  3. When i represents one hundred and one hundred corresponding to the number is 0, such as n = 31056, i = 100, then a = 310, b = 56, this time one hundred times as there are 1 a / 10 = 31 (up to two bits 0 to 30).

Based on the above three cases, when one hundred corresponding to 0 or> = 2, (a + 8) / 10 times 100 contains all the points, when there is one hundred 1 (a% 10 == 1), needs to be increased local point b + 1.

The reason why 8 up, because when one hundred to 0, a / 10 == (a + 8) / 10, when one hundred> = 2, 8 complement carry bit is generated, the same effect as (a / 10 + 1 ).

// 统计次数
int count = 0;
for(int i = 1; i <= n; i *= 10){
    // 计算高位和低位
    int a = n / i, b = n % i;
    count += (a + 8) / 10 * i + (a % 10 == 1 ? 1 : 0) * (b + 1);
}
return count;

Some people may have doubts, such as 11100, in consideration of the number of one hundred counted as a time of 1, in consideration of one thousand, it also forgets time, considering ten thousand to 1 when they forget time, a total of three times, this is not obvious repeat it?

其实并不重复,题目中要我们统计出现的1的个数,那么我们可以看到11100一共是3个1,如果剔除了重复的情况只考虑一次才会是问题。换言之,在计算从1到n整数中1的出现次数时,我们把10位出现1的情况个数加上百位出现1的情况个数一直加到最高位是1的情况的个数,这里面一个数可能被统计过多次;11100百位出现1,千位和万位都为1,那么被重复统计了3次。

32. 把数组排成最小的数

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

字典序

改写compare()

Arrays.sort(str,new Comparator<String>(){
    @Override
    public int compare(String s1, String s2) {
        String c1 = s1 + s2;
        String c2 = s2 + s1;
        return c1.compareTo(c2);
    }
});

33. 丑数

把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

思路题

一个丑数的因子只有2,3,5,那么丑数p = 2 ^ x * 3 ^ y * 5 ^ z

那么我们从1开始乘以2,3,5,就得到2,3,5三个丑数,在从这三个丑数出发乘以2,3,5就得到4,6,10,6,9,15,10,15,25九个丑数,这里我们发现会得到重复的丑数,而且得到的丑数也是无序的。

那么我们可以维护三个队列:

  • 乘以2的队列
  • 乘以3的队列
  • 乘以5的队列
// 选出三个队列头最小的数
res[i] = Math.min(res[p2] * 2, Math.min(res[p3] * 3, res[p5] * 5));
// 这三个if有可能进入一个或者多个,进入多个是三个队列头最小的数有多个的情况
if (res[i] == res[p2] * 2) p2++;
if (res[i] == res[p3] * 3) p3++;
if (res[i] == res[p5] * 5) p5++;

34. 第一个只出现一次的字符

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写)。

hashmap

35. 数组中的逆序对

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007

归并排序

36. 两个链表的第一个公共结点

输入两个链表,找出它们的第一个公共结点。

堆栈

  1. 把两个链表放入两个栈,弹出,直到结果不相等为止
  2. 最后一个相等的就是第一个公共结点

双指针

假设list1长度为a + n,list2 长度为b + n,a < b

  1. p1会先走到list1尾部,将p1置为list2头部
  2. p2接着走到list2尾部,将p2置为list1头部
  3. 在p1和p2都走了a+b+n步后,p1,p2到达 null 或者公共结点

37. 数字在排序数组中出现的次数

统计一个数字在排序数组中出现的次数。

二分

二分找到数组中第一个出现该数字下标和最后一个出现该数字的下标

38. 二叉树的深度

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

递归

递归比较左右子树

39. 平衡二叉树

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

递归

递归左右子树差的绝对值小于等于1

40. 数组中只出现一次的数字

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。

位运算

只有一个数出现一次时,我们把数组中所有的数,依次异或运算,最后剩下的就是落单的数,因为成对出现的都抵消了。

对于两个数(我们假设是A、B)出现一次的数组:

  1. 还是先异或,剩下的数字为A、B异或的结果(这个结果的二进制中的1,表现的是A和B的不同的位)
  2. 从右取第一个1所在的位数
  3. 这样可以将原数组分成两组,A、B肯定不在一组
  4. 问题就回到了“数组只有一个数出现一次”的问题了,再次异或,剩余的两个结果就是这两个只出现一次的数字。

41. 和为S的连续正数序列

小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!

双指针

  1. 两个起点,相当于动态窗口的两边,根据其窗口内的值的和来确定窗口的位置和大小
  2. 当前窗口内的值之和是(a0+an)*n/2
  3. 如果和sum相等,那么就将窗口范围的所有数添加进结果集
  4. 如果小于sum,那么右边窗口右移一下
  5. 如果当前窗口内的值之和大于sum,那么左边窗口右移一下

42. 和为S的两个数字

输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。

双指针(相差越远乘积越小)

  1. 数列满足递增,设两个头尾两个指针i和j
  2. 若ai + aj == sum,就是答案(相差越远乘积越小)
  3. 若ai + aj > sum,aj肯定不是答案之一(前面已得出 i 前面的数已是不可能),j--
  4. 若ai + aj < sum,ai肯定不是答案之一(前面已得出 j 后面的数已是不可能),i++

43. 左旋转字符串

汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!

substring

两次翻转字符串

主要思想是:

YX = (X^{T}Y^{T})^{T}
  1. 先反转前面部分字符串,再反转后面部分字符串
  2. 反转整个字符串

44. 翻转单词顺序列

牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?

堆栈

先翻转整个句子,再依次翻转每个单词

public class Solution {
    public String ReverseSentence(String str) {
        char[] chars = str.toCharArray();
        reverse(chars, 0, chars.length - 1);
        int blank = -1;
        for(int i = 0; i < chars.length; i++){
            if(chars[i] == ' '){ 
                int nextBlank = i;
                reverse(chars, blank + 1, nextBlank - 1);
                blank = nextBlank;
            }
        }
        reverse(chars, blank + 1, chars.length - 1);//最后一个单词单独进行反转
        return new String(chars);
         
    }
    public void reverse(char[] chars, int low, int high){
        while(low < high){
            char temp = chars[low];
            chars[low] = chars[high];
            chars[high] = temp;
            low++;
            high--;
        }
    }
}

45. 扑克牌顺子

LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。

思路题

  1. max - min < 5
  2. 除0外没有重复的数字

46. 孩子们的游戏(圆圈中最后剩下的数)

每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)

如果没有小朋友,请返回-1

利用数组来模拟环

  1. 用一个数组做标志
  2. if(i>=n) i=0; 模拟环 

47. 求1+2+3+...+n

求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

递归

可以利用&&判断递归出口

boolean flag = (n > 0) && ((sum += Sum_Solution(n - 1)) > 0);

48. 不用加减乘除做加法

写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

与或运算

  1. 相加各位的值,不算进位
  2. 计算进位值
  3. 重复上述两步,直至进位值为0

49. 把字符串转换成整数

将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0

溢出判断

https://blog.csdn.net/qq_33330687/article/details/81626157

50. 数组中重复的数字

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

利用现有数组

使用hashmap有空间消耗,利用现有数组设置标志,当一个数字被访问过后,可以设置对应位上的数 + n,之后再遇到相同的数时,会发现对应位上的数已经大于等于n了,那么直接返回这个数即可。

51. 构建乘积数组

给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。

矩阵连乘

下三角用连乘可以很容求得,上三角,从下向上也是连乘。

52. 正则表达式匹配

请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

思路题

分为以下的情况:

  1. 模式第二个为*
    • 字符串第一个和模式第一个匹配
      • 匹配0个字符
      • 匹配1个字符
      • 先匹配str中1个字符,再匹配str中的下一个
    • 字符串第一个和模式第一个不匹配
  2. 模式第二个不为*
    • 字符串第1个跟模式第1个匹配
    • 字符串第1个跟模式第1个不匹配

53. 表示数值的字符串

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。

思路题

通过三个变量index(此处是否允许有+/-),small(此处是否允许有.),next(此处是否需要后面不为空)控制。

54. 字符流中第一个不重复的字符

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

简单题

一个字符占8位,因此不会超过256个,可以申请一个256大小的数组来实现一个简易的哈希表。

55. 链表中环的入口结点

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

快慢指针

主要的思想是:用两个指针 first,second 分别从起点开始走,first 每次走一步,second 每次走两步。
如果过程中 second 走到null,则说明不存在环。否则当 first 和 second 相遇后,让 first 返回起点,second 待在原地不动,然后两个指针每次分别走一步,当相遇时,相遇点就是环的入口。

如上图所示,a 是起点,b 是环的入口,c 是两个指针的第一次相遇点,ab 之间的距离是 x,bc 之间的距离是 y。
则当 first 走到 b 时,由于 second 比 first 多走一倍的路,所以 second 已经从 b 开始在环上走了 x 步,可能多余1圈,距离 b 还差 y 步(这是因为第一次相遇点在 b 之后 y 步,我们让 first 退回 b 点,则 second 会退 2y 步,也就是距离 b 点还差 y 步);所以 second 从 b 点走 x+y 步即可回到 b 点,所以 second 从 c 点开始走,走 x 步即可恰好走到 b 点,同时让 first 从头开始走,走 x 步也恰好可以走到 b 点。所以第二次相遇点就是 b 点。

56. 删除链表中重复的结点

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

双指针

利用零结点,减少首结点的处理。

57. 二叉树的下一个结点

给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。

简单题

  1. 判断是否有右子节点
  2. 没有的话找到该结点的父节点

58. 对称的二叉树

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

简单题

左右等于右左,左左等于右右

59. 按之字形顺序打印二叉树

请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

两个栈

  1. s1存奇数层结点
  2. s2存偶数层结点

60. 把二叉树打印成多行

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

队列

61. 序列化二叉树

请实现两个函数,分别用来序列化和反序列化二叉树

二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于先序、中序、后序、层序的二叉树遍历方式来进行修改,序列化的结果是一个字符串,序列化时通过 某种符号表示空节点(#),以 ! 表示一个结点值的结束(value!)。
二叉树的反序列化是指:根据某种遍历顺序得到的序列化字符串结果str,重构二叉树。

递归

通过先根遍历将二叉树转换成字符串,通过全局变量index递归完成而二叉树的重建。

62. 二叉搜索树的第k个结点

给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8)    中,按结点数值大小顺序第三小结点的值为4。

中序遍历

63. 数据流中的中位数

如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。

大、小顶堆

主要的思想是:因为要求的是中位数,那么这两个堆,大顶堆用来存较小的数,从大到小排列;小顶堆存较大的数,从小到大的顺序排序,显然中位数就是大顶堆的根节点与小顶堆的根节点和的平均数。

  1. 通过PriorityQueue创建大顶堆和小顶堆:
//小顶堆
private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
//大顶堆
private PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(15, new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
});
  1. 当数目为偶数的时候,将这个值插入大顶堆中,再将大顶堆中根节点(即最大值)插入到小顶堆中
  2. 当数目为奇数的时候,将这个值插入小顶堆中,再讲小顶堆中根节点(即最小值)插入到大顶堆中
  3. 取中位数的时候,如果当前个数为偶数,显然是取小顶堆和大顶堆根结点的平均值;如果当前个数为奇数,显然是取小顶堆的根节点

64. 滑动窗口的最大值

给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。

双端队列

使用双端队列,记录每个窗口的最大值下标

  1. 先删去队列中num[index]比num[i]小的
  2. 将i压入队列尾部
  3. 判断队首元素是否过期(即是否在给定窗口中)
LinkedList<Integer> qmax = new LinkedList<>();

65. 矩阵中的路径

请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。

回溯法

  1. 找到矩阵中起点等于给定字符串第一个元素的值
  2. 回溯

66. 机器人的运动范围

地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

回溯法

67. 剪绳子

给你一根长度为n的绳子,请把绳子剪成整数长的m段(m、n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],...,k[m]。请问k[0]xk[1]x...xk[m]可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。

输入:target(为绳子长度)
输出:最大乘积

动态规划

在这个题目中,由于必须要剪一刀,因此会导致当所给的绳子长度是小于4的时候,剪完之后的乘积小于剪之前的长度。

if (target < 2) return 0;
if (target == 2) return 1;
if (target == 3) return 2;

大于等于4可以分成很多段,比如4可分成1和3,这里的3不再分,因为3分段最大才2,不继续分就是3,当然4也可以分成2和2,乘积为4,记录最大的。

int[] dp = new int[target + 1];
dp[1] = 1;
dp[2] = 2;
dp[3] = 3;
int res = 0;
for (int i = 4; i < target + 1; i++) {
for (int j = 1; j <= i / 2; j++) {
    res = Math.max(res, dp[j] * dp[i - j]);
}
dp[i] = res;
}
return dp[target];

贪心算法

当n>=5的时候,2(n-2) > n and 3(n-3) > n and 3(n-3) >= 2(n-2)
也就是说当长度大于5的时候,剪出长度为3的段数越多最后的乘积越大

但是对于长度为4的时候,如果剪出一段长度为3,另一段长度为1,则乘积为3,小于剪出两段长度为2,因此,当最后剩下4的时候,不是剪成1,3而是剪成2,2

if (target < 2) return 0;
if (target == 2) return 1;
if (target == 3) return 2;
int x = target % 3;
int y = target / 3;
if (x == 0) {
    return Math.pow(3, y);
} else if (x == 1) {    // 最后剩下4的时候,不是剪成1,3而是剪成2,2
    return 2 * 2 * Math. pow(3, y - 1);
} else {    // 最后剩下2
    return 2 *  Math.pow(3, y);
}

Guess you like

Origin www.cnblogs.com/kyrie17/p/12380448.html