Leetcode's Medium collection (C + + implementation) 17

Source: Internet
Author: User

1 Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to X.you should preserve the original relative order of the nodes in each of the of the partitions. For example:
Given 1->4->3->2->5->2 and x = 3,
Return 1->2->2->4->3->5.
In order to facilitate the operation of the first node of the list, here to use the head pointer to the first node, starting from the beginning of the pointer, we want to use a variable to record the first occurrence of the element value in the node is greater than the target value of the position, continue to traverse backward, will be less than the target node moved the variable record position.

ListNode*Partition (ListNode*Head, int x) {if(Head==NULL)return NULL; ListNode*Result=NewListNode (0); Result -Next=Head ListNode*Pre=Result ListNode*Premid; ListNode*Cur=Head ListNode*Mid BOOL Flag=true;//Used to mark the first node that is greater than the target value         while(Pre -Next) {if(Pre -Next -Val<x) {if(flag) {Pre=Cur Cur=Cur -Next }Else{Pre -Next=Cur -Next ListNode*Reg=Cur Cur=Cur -Next Premid -Next=Reg Reg -Next=Mid Premid=Premid -Next }            }Else{if(flag) {Premid=Pre Mid=Pre -Next Flag=false; Pre=Cur Cur=Cur -Next }Else{Pre=Cur Cur=Cur -Next }            }        }returnResult -Next }

Gray Code
The gray code is a binary numeral system where the successive values are differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A Gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00-0
01-1
11-3
10-2.
You can get the N-bit gray code word directly from the corresponding n-bit binary code word, the steps are as follows:
(1) code word for n-bit binary, from right to left, numbered 0 to N-1
(2) If the binary code word of the first and i+1 bit is the same, then the corresponding gray code of the first bit is 0, otherwise 1 (when i+1=n, the nth bit of the binary code word is considered to be 0, that is, the n-1 bit unchanged). Expressed in a formula: G i = B i ?B i + 1 ,(N?1≥I≥0) 。

vector<int> grayCode(int n) {        vector<int> result;        int len=1<<n;        for(int i=0;i<len;i++)        {            result.push_back((i>>1)^i);        }        return result;    }

Leetcode's Medium collection (C + + implementation) 17

Related Article

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.