Exemple #1
0
int main()
{
    node *listHead = create();
    if (!listHead)
        return 0;
    for (int i = 0; i < 10; i++)
        add(listHead, i);
    deleteNode(listHead, 5);
    insertNode(listHead, 5, 50);

    printf("Length = %d\n", length(listHead));
    printList(listHead);
    reverseList(listHead);
    printList(listHead);

    node *pMid;
    pMid = getMid(listHead);
    printf("Mid data is %d\n", pMid->data);
    add(listHead, 99);
    printList(listHead);
    pMid = getMid(listHead);
    printf("Mid data is %d\n", pMid->data);

    destroy(listHead);
    return 0;
}
    int countStrobogrammaticUntil(string num, bool can_start_with_0) {
        if (can_start_with_0 && cache.find(num) != cache.end()) {
            return cache[num];
        }

        int count = 0;
        if (num.length() == 1) {
            for (const auto& c : {'0', '1', '8'}) {
                if (num.front() >= c) {
                    ++count;
                }
            }
            cache[num] = count;
            return count;
        }
        
        for (const auto& kvp : lookup) {
            if (can_start_with_0 || kvp.first != '0') { 
                if (num.front() > kvp.first) {
                    if (num.length() == 2) {  // num is like "21"
                        ++count;
                    } else {  // num is like "201"
                        count += countStrobogrammaticUntil(string(num.length() - 2, '9'), true);
                    }
                } else if (num.front() == kvp.first) {
                    if (num.length() == 2) {  // num is like 12".
                        count += num.back() >= kvp.second;
                    } else {
                        if (num.back() >= kvp.second) {  // num is like "102".
                            count += countStrobogrammaticUntil(getMid(num), true);
                        } else if (getMid(num) != string(num.length() - 2, '0')) {  // num is like "110".
                            count += countStrobogrammaticUntil(getMid(num), true) -
                                     isStrobogrammatic(getMid(num));
                        }
                    }
                }
            }
        }

        if (!can_start_with_0) { // Sum up each length.
            for (int i = num.length() - 1; i > 0; --i) {
                count += countStrobogrammaticByLength(i);
            }
        } else {
            cache[num] = count;
        }

        return count;
    }
 void reorderList(ListNode *head) {
     //step 1: count 
     //iterator headPtr = head;
     int len = lengthOfList( head );
     if( len < 3 ){
         return;
     }
     
     //step 2: reverse
     int mid = (len+1)/2;
     iterator midPtr = getMid( head, mid );  
     iterator mn = midPtr->next;
     midPtr->next = NULL;
     midPtr = reverseList( mn );
     
     //step 3: merge
     iterator hn = head->next;
     mn = midPtr->next;
     while( mn != NULL ){
         head->next = midPtr;
         midPtr->next = hn;
         head = hn;
         midPtr = mn;
         hn = head->next;
         mn = midPtr->next;
     }
     head->next = midPtr;
     midPtr->next = hn;
         
     return ;
 }
Exemple #4
0
    void reorderList(ListNode* head) {
        if (head==NULL || head->next==NULL || head->next->next==NULL)
        {
            return ;
        }
        
        ListNode* l1=head;
        ListNode* l2=getMid(head);
        ListNode* reverseL2=reverseList(l2);
        
		ListNode* newhead=new ListNode(-1);
        ListNode* tail=newhead;
        while (l1!=NULL && reverseL2!=NULL)
        {
			tail->next=l1;
            l1=l1->next;
			tail=tail->next;
            tail->next=reverseL2;
			tail=tail->next;
            reverseL2=reverseL2->next;
        }
       if (l1!=NULL)
       {
		   tail->next=l1;
       }
	   if (reverseL2!=NULL)
	   {
		   tail->next=reverseL2;
	   }
	   head=newhead->next;
    }
Exemple #5
0
int main()
{
	input();
	printf("\nYour list :\n");
	show(head);
    getMid(head);
	return 0;
}
Exemple #6
0
 TreeNode * convert (ListNode * head, ListNode * end)
 {
 	if (end == head) return NULL;
 	ListNode * m = getMid(head, end);
 	TreeNode * root = new TreeNode (m -> val);
 	root -> left = convert(head, m);
 	root -> right = convert(m -> next, end);
 	return root;
 }
    ListNode *sort(ListNode *start) {
		if (start->next == NULL) return start;
		ListNode *mid = getMid(start);
		ListNode *p = mid->next;
		mid->next = NULL;
		ListNode *tmp1 = sort(start);
		ListNode *tmp2 = sort(p);
		return merge(tmp1, tmp2);
    }
Exemple #8
0
long constructSTUtil2(long *arr,long ss,long se,long *st,long si){
    if(ss==se){
        st[si]=arr[ss];
        return arr[ss];
    }
    long mid=getMid(ss,se);
    long a=constructSTUtil2(arr,ss,mid,st,si*2+1),
            b=constructSTUtil2(arr,mid+1,se,st,si*2+2);
    st[si]=maxVal(a,b);
    return st[si];
}
int RMQUtil_m(int *st, int ss, int se, int qs, int qe, int index)
{
        if (qs <= ss && qe >= se)
                return st[index];

        if (se < qs || ss > qe)
                return INT_MAX;

        int mid = getMid(ss, se);
        return maxVal(RMQUtil_m(st, ss, mid, qs, qe, 2*index+1),
                      RMQUtil_m(st, mid+1, se, qs, qe, 2*index+2));
}
int constructSTUtil(int arr[], int ss, int se, int *st, int si)
{
        if (ss == se) {
                st[si] = arr[ss];
                return arr[ss];
        }

        int mid = getMid(ss, se);
        st[si] =  minVal(constructSTUtil(arr, ss, mid, st, si*2+1),
                         constructSTUtil(arr, mid+1, se, st, si*2+2));
        return st[si];
}
Exemple #11
0
long RMQUtil(long *st,long ss,long se,long qs,long qe,long index){
    if(qs<=ss && qe>=se){
        return st[index];
    }
    if(se<qs || ss>qe){
        return LONG_MAX;
    }
    long mid=getMid(ss,se);
    long a=
            RMQUtil(st,ss,mid,qs,qe,2*index+1),
            b=RMQUtil(st,mid+1,se,qs,qe,2*index+2);
    return minVal(a,b);
}
Exemple #12
0
    ListNode* sortList(ListNode* head) {
        if (head==NULL || head->next==NULL)
        {
            return head;
        }
        
        ListNode* head1=head;
        ListNode* head2=getMid(head);
        head1=sortList(head1);
        head2=sortList(head2);

        return merge(head1, head2);
    }
 /**
  * @param visited   Visited num in current path
  * @param cur       Current num
  * @return Number of paths starts from current num
  */
 int DFS(bool* visited, int len, int cur, int minSize, int maxSize) {
     int res = 0;
     if (len > maxSize) return res;
     if (len >= minSize) res++;
     visited[cur] = 1;
     for (int next = 1; next < 10; next++) {
         if (visited[next]) continue;
         int midNum = getMid(cur, next);
         if (midNum && !visited[midNum]) continue;
         res += DFS(visited, len + 1, next, minSize, maxSize); 
     }
     visited[cur] = 0;
     return res;
 }
Exemple #14
0
/* A recursive function to update the nodes which have the given index in
   their range. The following are parameters
    st, index, ss and se are same as getSumUtil()
    i    --> index of the element to be updated. This index is in input array.
   diff --> Value to be added to all nodes which have i in range */
void updateValueUtil(long long int *st, long long int ss, long long int se, long long int i, long long int diff, long long int index)
{
    // Base Case: If the input index lies outside the range of this segment
    if (i < ss || i > se)
        return;

    // If the input index is in range of this node, then update the value
    // of the node and its children
    st[index] = st[index] + diff;
    if (se != ss)
    {
        long long int mid = getMid(ss, se);
        updateValueUtil(st, ss, mid, i, diff, 2*index + 1);
        updateValueUtil(st, mid+1, se, i, diff, 2*index + 2);
    }
}
Exemple #15
0
/*  A recursive function to get the minimum value in a given range of array
    indexes. The following are parameters for this function.
 
    st    --> Pointer to segment tree
    index --> Index of current node in the segment tree. Initially 0 is
             passed as root is always at index 0
    ss & se  --> Starting and ending indexes of the segment represented by
                 current node, i.e., st[index]
    qs & qe  --> Starting and ending indexes of query range */
int RMQUtil(int *st, int ss, int se, int qs, int qe, int index)
{
    // If segment of this node is a part of given range, then return the
    // min of the segment
    if (qs <= ss && qe >= se)
        return st[index];
 
    // If segment of this node is outside the given range
    if (se < qs || ss > qe)
        return INT_MAX;
 
    // If a part of this segment overlaps with the given range
    int mid = getMid(ss, se);
    return minVal(RMQUtil(st, ss, mid, qs, qe, 2*index+1),
                  RMQUtil(st, mid+1, se, qs, qe, 2*index+2));
}
Exemple #16
0
/* A recursive function to get the sum of values in given range of the array.
	The following are parameters for this function.

	st --> Pointer to segment tree
	si --> Index of current node in the segment tree. Initially 0 is
			passed as root is always at index 0
	ss & se --> Starting and ending indexes of the segment represented by
				current node, i.e., st[si]
	qs & qe --> Starting and ending indexes of query range */
int getSumUtil(int *st, int ss, int se, int qs, int qe, int si)
{
	// If segment of this node is a part of given range, then return the
	// sum of the segment
	if (qs <= ss && qe >= se)
		return st[si];

	// If segment of this node is outside the given range
	if (se < qs || ss > qe)
		return 0;

	// If a part of this segment overlaps with the given range
	int mid = getMid(ss, se);
	return getSumUtil(st, ss, mid, qs, qe, 2*si+1) +
		getSumUtil(st, mid+1, se, qs, qe, 2*si+2);
}
Exemple #17
0
/*  A recursive function to get the sum of values in given range of the array.
    The following are parameters for this function.

    st    --> Polong long inter to segment tree
    index --> Index of current node in the segment tree. Initially 0 is
             passed as root is always at index 0
    ss & se  --> Starting and ending indexes of the segment represented by
                 current node, i.e., st[index]
    qs & qe  --> Starting and ending indexes of query range */
unsigned long long int getSumUtil(long long int *st, long long int ss, long long int se, long long int qs, long long int qe, long long int index)
{
    // If segment of this node is a part of given range, then return the
    // sum of the segment
    if (qs <= ss && qe >= se)
        return st[index];

    // If segment of this node is outside the given range
    if (se < qs || ss > qe)
        return 0;

    // If a part of this segment overlaps with the given range
    unsigned long long int mid = getMid(ss, se);
    return getSumUtil(st, ss, mid, qs, qe, 2*index+1) +
           getSumUtil(st, mid+1, se, qs, qe, 2*index+2);
}
Exemple #18
0
// A recursive function that constructs Segment Tree for array[ss..se].
// si is index of current node in segment tree st
long long int constructSTUtil(long long int arr[], long long int ss, long long int se, long long int *st, long long int si)
{
    // If there is one element in array, store it in current node of
    // segment tree and return
    if (ss == se)
    {
        st[si] = arr[ss];
        return arr[ss];
    }

    // If there are more than one elements, then recur for left and
    // right subtrees and store the sum of values in this node
    long long int mid = getMid(ss, se);
    st[si] =  constructSTUtil(arr, ss, mid, st, si*2+1) +
              constructSTUtil(arr, mid+1, se, st, si*2+2);
    return st[si];
}
Exemple #19
0
// A recursive function that constructs Segment Tree for array[ss..se].
// si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int *st, int si)
{
    // If there is one element in array, store it in current node of
    // segment tree and return
    if (ss == se)
    {
        st[si] = arr[ss];
        return arr[ss];
    }
 
    // If there are more than one elements, then recur for left and
    // right subtrees and store the minimum of two values in this node
    int mid = getMid(ss, se);
    st[si] =  minVal(constructSTUtil(arr, ss, mid, st, si*2+1),
                     constructSTUtil(arr, mid+1, se, st, si*2+2));
    return st[si];
}
void updateValueOfArrByAdjustSegTree(int *segTree,int segStart,int segEnd,int i,int diffValue,int index){
	//updating segtree only if the node to be updated falls within the range of segstart, segend
	
	if(i<segStart||i>segEnd){//only if the i value is within range.
		return ;
	}
	
	segTree[index]=segTree[index]+diffValue;
	if(segStart!=segEnd){
		//if both are not equal
		int mid = getMid(segStart,segEnd);
		updateValueOfArrByAdjustSegTree(segTree,segStart,mid,i,diffValue,2*index+1);
		updateValueOfArrByAdjustSegTree(segTree,mid+1,segEnd,i,diffValue,2*index+2);
	}
	
	
}
int constructSegTreeUtil(int arr[],int segStart,int segEnd,int *segTree,int segIndex){
	//assume that you have 6 nodes
	//3rd node will be the root, 0-3 will be left subtree always, 4-6 will be right subtree.
	//This subdivision will be used everywhere.
	if(segStart==segEnd)//if the segment start and segment ends are equal
	{//construct the leaf node, just the array element.
		segTree[segIndex]=arr[segStart];
		return segTree[segIndex];
	}
	int mid = getMid(segStart,segEnd);
	//construct the sum of leaves to the internal node, ie merging takes place here
	segTree[segIndex]=constructSegTreeUtil(arr,segStart,mid,segTree,segIndex*2+1)+ // Left subtree, 2*i+1
						constructSegTreeUtil(arr,mid+1,segEnd,segTree,segIndex*2+2); //2*i+2 right subtree
						
	return segTree[segIndex];
	
	
} 
int getSuMOfValuesFromSegTree(int *segTree,int segStart,int segEnd,int queryStart,int queryEnd,int index){
	
	//if the query start and query end is within segStart and segEnd of node, then return node's data
	// If segment of this node is a part of given range, then return the 
    // sum of the segment
	if(queryStart<=segStart&&queryEnd>=segEnd)
	{//segStart, segEnd [2,2] queryStart,queryEnd [1,3]
	//segStart ,segEnd [3,3] queryStart,queryEnd [1,3]
		return segTree[index];
	}
	if(segEnd<queryStart||segStart>queryEnd){//seg end greater than query
		//segStart, segEnd [0,0] queryStart, queryEnd [1,3] outside the range
		//segStart, segEnd [4,5], [4,4] queryStart, queryEnd [1,3] outside the range
		return 0;//outside the range.
	}
	int mid = getMid(segStart,segEnd);
	return getSuMOfValuesFromSegTree(segTree,segStart,mid,queryStart,queryEnd,2*index+1)+
			getSuMOfValuesFromSegTree(segTree,mid+1,segEnd,queryStart,queryEnd,2*index+2);
	
}
 void reorderList(ListNode* head) {
     if (!head || !head->next) return;
     
     // q stocks the first half of the list
     queue<ListNode*> q;
     // s stocks the second half of the list
     stack<ListNode*> s;
     
     // stop points to the fist node in the second half
     ListNode* stop = getMid(head);
     ListNode* curr = head;
     while (curr != stop) {
         q.push(curr);
         curr = curr->next;
     }
     while (curr) {
         s.push(curr);
         curr = curr->next;
     }
     
     // create the reorder list
     curr = head;
     q.pop();    // pop the first node, which is head already
     // Because s's length is at least q's length,
     // only care about the q
     while (!q.empty()) {
         curr->next = s.top(); s.pop();
         curr = curr->next;
         curr->next = q.front(); q.pop();
         curr = curr->next;
     }
     // s may left nodes
     while (!s.empty()) {
         curr->next = s.top();
         s.pop();
         curr = curr->next;
     }
     curr->next = NULL;
 }
 ListNode* sortList(ListNode* head) {
     if (!head || !head->next) return head;
     ListNode* mid = getMid(head);
     return mergeList(sortList(head), sortList(mid));
 }