Programmer's Advanced Algorithm Practice (i)

Source: Internet
Author: User
Tags add numbers

Objective

Maybe a lot of mobile programming students hear the algorithm to feel fear, think I will not algorithm can also develop AH. It is true that no algorithm can handle the general work. But the gap with Daniel is that maybe someone else 3 lines of code to achieve something, you have to write more than 10 lines, and performance than others worse. So, let's learn some algorithms.

Algorithmic Learning

The simplest way to learn the algorithm is to practice more, find a website that provides algorithmic exercises, think, encode, validate, and finally look at other people's ideas.
The topics in this series come from Leetcode. The IDE uses Xcode , the author uses swift .

(PS: The Code Implementation section of the following exercises is not the only solution, for reference only)

The Sum of

Topic links
Given an array of integers, find the index of two numbers that meet the sum of two numbers equals the number of targets , and return.
For example:

Nums = [2, 7, one, all], target = 9,
Because Nums[0] + nums[1] = target,
So return [0, 1]

Code implementation:

func twoSum(_ nums:[Int], _ target:Int) -> [Int]? {    var d = [Int: Int]()        for (i, num) in nums.enumerated(){        if let sum = d[target - num] {            return [sum, i]        }else{            d[num] = i        }    }    return nil}
ADD Numbers

Topic links
The main idea: using a linked list to achieve two numbers added

For example:

12 + 13 = 25

Code implementation:

 public class ListNode {publicly var val:int public var next:listnode? Public init (_ Val:int) {Self.val = val Self.next = nil}}public class Solution {func addtwonumbers ( _ L1:listnode?, _ L2:listnode?) ListNode? {return helper (L1, L2, 0)} Func helper (_ L1:listnode?, _ L2:listnode?, _ Carry:int), ListNode? {var p = l1 var q = L2 if p = = Nil && Q = = Nil {return carry = = 0? nil: ListNode (Carry)} if p = = Nil && Q! = nil {p = listnode (0)} If P! = Nil && Q = = Nil {q = ListNode (0)} Let sum = (p?. val)! + (q?. val)! + Carry Let Curr = ListNode (sum%) Curr.next = Helper (p?. Next, Q?. Next, SUM/10) return curr}}var L1:listnode?var l2:listnode?l1 = ListNode (all) L2 = ListNode (all) let S = solution () Let result1 = s.addtwonumbers (L1, L2) 

Ideas:

In accordance with the principle of elementary school addition, from the end of the sum, full 10. The trick is how to handle two numbers of different lengths, as well as the judgments of the rounding and the highest bits. Here, for different lengths of numbers, we can ensure that each bit is added by adding zero in front of the shorter number. Mainly divided into the following 3 points:

    • Returns the Carry value when all is nil;
    • When there is a nil, returns the result of the addition of the listnode and carry values that are not nil;
    • is not nil, returns the result of the addition of two listnode and a carry value.
Longest Substring without repeating characters

Topic links

Topic: Given a string, find the longest length of consecutive substrings with no repeating characters.
For example:

"ABCABCBB" the longest non-repeating character string is "abc", with a length of 3;
"BBBBB" the longest non-repeating character string is "B", with a length of 1;
"Pwwkew" the longest non-repeating character string is "Wke", the length is 3;

Code implementation:

func lengthOfLongestSubstring(_ s:String) -> Int {    if s.isEmpty {        return 0    }        var map = [Character: Int]()    var result = 0    var j = 0        for (i, charactor) in s.characters.enumerated() {        if map.keys.contains(charactor) {            j = max(j, map[charactor]! + 1)        }                map[charactor] = i        result = max(result, i-j+1)    }        return result}

Ideas:

There are 3 main points of attention in this topic:

  1. the longest;
  2. continuous;
  3. no duplicate characters;
Thanks

If you find the wrong place, you are welcome to point out, thank you!

Programmer's Advanced Algorithm Practice (i)

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.