Data structure and algorithm---leetcode part

Source: Internet
Author: User

               
----------------------personal Work, if you have the work of the younger generation of the same, you can refer to study, Exchange, do not copy directly
1. Remove Nth Node from End of List (#19)

Given A linked list, remove the nth node from the end of the list and return its head.

For example,

Given Linked list: 1->2->3->4->5, and n = 2.

1->2->3->5.

Note: Given n would always be valid. Try to do the in one pass.

• Special case judgment à lookup node to find the chain table length à delete the node (whether to do at the end of the decision to delete)-------the list of nodes to delete is: from the "list and return its head."

2. Merge Sorted Lists (#21)

Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.

• Special case judgment à general merge sort combined with linked list operation

3. Search Insert Position (#35)

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would is if it were inserted in order. Assume no duplicates in the array. Here is few examples.

[1,3,5,6], 5→2

[1,3,5,6], 2→1

[1,3,5,6], 7→4

[1,3,5,6], 0→0

• Finding and judging arrays

4. Climbing Stairs (#70)

You is climbing a stair case. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In what many distinct ways can you climb to the top?

• Dynamic planning is used. The nth step is only related to step n-1 and step n-2, i.e. steps[n] = Steps[n-1] + steps[n-2], with an array of two yuan to store the first two steps related to nth step.

5. Same Tree (#100)

Given binary trees, write a function to check if they is equal or not. The binary trees is considered equal if they is structurally identical and the nodes has the same value.

• Divide and conquer recursion, special case judgment (whether non-empty/number of elements in the same) à judge the current node and the left and right sub-tree, traverse the node

6. Best time to Buy and Sell Stock (#121)

Say you has an array for which the i-th element is the price of a given-stock on day I. If you were-permitted-to-complete at most one transaction (ie, buy one and sell one share of the stock), design an AL Gorithm to find the maximum profit.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output:5
Max. difference = 6-1 = 5 (Not 7-1 = 6, as selling-price needs-be-larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]
output:0
In this case, no transaction are done, i.e. Max Profit = 0.

• Greedy problem, the first to determine whether it is empty, empty error exits. If the inputs are correct, compare them one by one and replace the better solution if the current result is better. Where I is used to point to the smallest value in the traversed element, J is used to iterate over the array, the two as the subscript is the value of the profit, if the profit is greater than Max, then replace Max (that is greedy), when the value of J point is less than I point to the element, I = j, continue to loop.

Data structure and algorithm---leetcode part

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.