求0和1个数相同的sub

You are given an array ' containing 0s and 1s. Find O(n) time and O(1) space
algorithm to find the maximum sub sequence which has equal number of 1s and
0s.

Examples
1) 10101010
The longest sub sequence that satisfies the problem is the input itself

2)1101000
The longest sub sequence that satisfies the problem is 110100


这个没想到正确的方法。。。


2. function to reverse digits of an integer. 123->321.     I just  used a smplest approach......

int reverseInt(int n)
{
	int result = 0;
	bool sign = false;

	if(n < 0)	//determine if n is pos or neg
	{
		sign = true;
		n *= -1;
	}

	int num = log10((double)n); //number of digits
	int m = n%10;

	while(n/=10)
	{
		result += m*pow(10.0,(double)num--);
		m = n%10;
	}
	result += m;

	if(sign)
		return result * (-1);
	else
		return result;
}

 3. Reverse a linkedlist.

void reverse()
{
	ListNode *curr = head;
	ListNode *newNode = 0;
		
	while(curr)
	{
		ListNode *nextNode = curr->next;
		curr->next = newNode;
		newNode = curr;
		curr = nextNode;
	}

	head = newNode;
}

  

posted @ 2011-08-05 23:18  Sw_R  阅读(151)  评论(0编辑  收藏  举报