Offer-- algorithm to prove safety issues and problem-solving ideas

Two-dimensional array lookup

Description Title
(same as the length of each one-dimensional array) in a two-dimensional array, each row from left to right in order of ascending sort, to sort each column from top to bottom in increasing order. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

Problem-solving ideas: Matrix is ​​ordered, the lower left corner from the point of view, up numbers are decreasing, the right number is increasing, so start looking from the lower left corner, when you're looking for big numbers than the lower-left corner, right step ; when you are looking for digital hours, moving up one step.

/*
	@param: arr二位数组,target要查找的数字
	@return: 当二位数组中存在要找查找的数字,则返回 1,否则返回 0 
*/
int find(int arr[][N], int target)
{
	int i = N - 1;	//从左下角开始
	int j = 0;
	while(i >= 0 && j < N)
	{
		if(target == arr[i][j])	//找到要查找的数字
			return 1;
		if(target > arr[i][j])	//比左下角的数字大,右移一步
			j++;
		else	//比左下角的数字小,向上移动一步
			i--;
	}
	return 0;
}

Do not do addition and subtraction, multiplication and division

Title Description
write a function, and the sum of two integers, the function may not be used in vivo requires +, -, *, / four arithmetic symbols.

Outline of Solution: calculated binary values ​​using a three-step manner by adding:

  1. Members of adding value, not a carry. Each binary equivalent of the sum you do XOR (^) operation.
  2. Carry value is calculated. I do the equivalent with (&) operation, then the left one.
  3. Repeat the above two steps until the carry is 0, then out of the loop.

exp:5(101)7(0111)

  1. 101^111 == 010
  2. (101&111)<<1 == 1010
  3. Repeat the above two steps, 0010 ^ == 1010 1000, (0010 & 1010) << 1 == 0100.
    repeated, 1000 ^ == 0100 1100, (1000 & 0100) == 0 << 1, out of the loop, the return of 1100 (12) .
int Add(int num1, int num2)
{
	int sum, carry;
	while(0 != num2)
	{
		//对应位相加但不进位,用异或实现
		sum = num1^num2;
		//记下进位,用与 和 移位实现
		carry = (num1&num2)<<1;
		num1 = sum;
		num2 = carry;
	}
	return num1;
}

Replace spaces

Title Description
Please implement a function, a string to replace each space to "20%." For example, when the string I Am A Handsome Boy. After the string after replacement of I% 20Am% 20A% 20Handsome% 20Boy.

Problem-solving ideas: Replace front to back, the back of the characters to be constantly moving backward, to move several times, so inefficient. Moving from back to front, first calculate how much space is needed, and then move from the back, each character can only move once, a little more efficient.

/*
	@param: str字符串
*/
void replaceSpace(char *str)
{
	int Oldlen = 0;	//记录字符串原来的长度
	int cnt = 0;	//记录空格的数量
	int Newlen;	//记录插入后的长度
	while(str[Oldlen] != '\0')	//遍历一遍字符串找出空格的数量
	{	
		if(str[Oldlen] == ' ')
			cnt++;
		Oldlen++;
	}
	Newlen = Oldlen + 2*cnt;	//插入后的长度,不用减一因为隐藏个'\0'也要算上
	if(Newlen > max_size)	//如果计算后的长度大于总长度就无法插入
		returnwhile(Oldlen >= 0 && Newlen > Oldlen)	//放字符
	{
		if(str[Oldlen] == ' ')	//碰到空格就替换
		{
			str[Newlen--] = '0';
			str[Newlen--] = '2';
			str[Newlen--] = '%';
		}
		else	//不是空格就将Oldlen指向的字符放入Newlen指向的位置
			str[Newlen--] = str[Oldlen];
		Oldlen--;	//不管是if还是else,Oldlen都要前移
	}
}

Reverse list

Description Title
input a linked list, the linked list after the inversion, the output of the new header.

Problem-solving ideas:
Here Insert Picture Description

ListNode* ReverseList(ListNode* pHead)
{
	if(NULL == pHead)	//判断链表是否为空
		return NULL;
	ListNode* pre = NULL;
	ListNode* next = NULL;
	while(pHead)
	{
		next = pHead->next;
		pHead->next = pre;
		pre = pHead;
		pHead = next;
	}
	return pre;	//pre成为新的头结点
}

Frog jump stairs

Problem Description
A frog can jump once a level 1, level 2 can also jump on. The frog jumped seeking a total of n grade level how many jumps. (Count different priorities to different results)

Problem-solving ideas: the number of columns with the same Fibonacci solution, nothing to say.

When n = 1, one species jumps;
when n = 2, there are two jumps;
when n = 3, there are three kinds of jumps;
when n = 4, the five kinds of jumps;
......
when the n-; have f (n-1) + f (n-2) kinds of jumps;

int jumpFloor(int number)
{
	if(number <= 2)
		return number;
	int num1 = 1;
	int num2 = 2;
	int tmp;
	int i;
	for(i=2; i<number; i++)
	{
		tmp = num1 +num2;
		num1 = num2;
		num2 = tmp;
	}
	return num2;
}

Abnormal level

Description Title
frog steps one can jump stage 1, stage 2 may jump jump ...... it may be n stages. The frog jumped seeking a total of n grade level how many jumps.

Outline of Solution:
F (. 1). 1 =
F (2) = F (2-1) + F (2-2) // F (2-2) represents a second-order frequency hop stage 2
f (3) = F (3-1) + F (3-2) + F (3-3)
......
F (n-) = F (n--. 1) + F (n--2) + F (. 3-n-) + ...... + f (n- (n-1 )) + f (nn)

Description:

  1. Where f (n) represents 1,2, ...... n has a number of stages n th step jump method.
  2. n = 1, only one kind of jump method, f (1) = 1
  3. When n = 2, there will be two jumps, a grade 1 or 2. f (2) = f (0) + f (1)
  4. When n = 3, there will be three kinds of jumps, a level 1, 2 or 3. The first stage is then bounce back rest 1: f (3-1); first jump stage 2, the remaining f (3-2); the first three out of the remaining f (3-3) . Thus concluded that: f (3) = f (3-1) + f (3-2) + f (3-3)
  5. When n = n, n kinds of jump method will, a level 1, level 2 ... n stages. Concluded that: f (n) = f ( n-1) + f (n-2) + ...... + f (n- (n-1)) + f (nn) ----> f (0) + f (1) + f ( 2) + f (3) + ...... + f (n-2) + f (n-1).
    Can be reduced to: f (n) = 2 * f (n-1)
  6. It concluded that:
    F (n-): n-= 0 when f (n) = 0; f (n) = 1 when f (n) = 1; n > = 2 when f (n) = 2 * f (n- 1).
int jumpFloorII(int number)
{
	if(0 == number || 1 == number)
		return number;
	int i;
	int power = 2;
	for(i=2; i<number; i++)
	{
		power *= 2;
	}
	return power;
}

Adjust the order of the array so that in front of the even-odd

Description Title
input an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array, the array is located in the second half of all even and ensure between odd and even, odd and even the relative positions unchanged.

Example:

Input: [1234567]
Output: [1357246]

Problem-solving ideas:
similar to bubble sort, before and after the even and odd exchanged.

void reOrderArray(int *array, int len)
{
	int i, j;
	for(i=0; i<len-1; i++)
	{
		for(j=len-1; j>i; j--)
		{
			//前偶后奇,则交换
			if(!(array[j-1]&1) && array[j]&1)
			{
				array[j] = array[j]^array[j-1];
				array[j-1] = array[j]^array[j-1];
				array[j] = array[j]^array[j-1];
			}
		}
	}
}
Published 50 original articles · won praise 5 · Views 1512

Guess you like

Origin blog.csdn.net/qq_42483691/article/details/104940912