Notes from the past week

1. Shifting one bit to the left (<<) is equivalent to multiplying by 2, and shifting one bit to the right (>>) is equivalent to dividing by 2 (not exactly the same),

2. Green coding

/*When n == 1, return [0,1].

When n == 2, return [0,1,3,2].

When n == 3, return [0,1,3,2,6,7,5,4] .

When n == ... , returns [0,1,3,2,6,7,5,4,...] .

It can be seen that when n == k, some numbers are added in the case of n == k - 1. And according to the meaning of the question, every time n increases by 1, the range of integers will double. (You can also observe the results)*/

//Gray code generation process, G(i) = i ^ (i/2);

Derivation:

如 n = 3: 
        G(0) = 000, 
        G(1) = 1 ^ 0 = 001 ^ 000 = 001
        G(2) = 2 ^ 1 = 010 ^ 001 = 011 
        G(3) = 3 ^ 1 = 011 ^ 001 = 010
        G(4) = 4 ^ 2 = 100 ^ 010 = 110
        G(5) = 5 ^ 2 = 101 ^ 010 = 111
        G(6) = 6 ^ 3 = 110 ^ 011 = 101
        G(7) = 7 ^ 3 = 111 ^ 011 = 100

3. Two kinds of subsets

1) Title: Given an integer array nums that contains repeated elements, please return all possible subsets (power sets) of the array.

A solution set cannot contain duplicate subsets. In the returned solution set, the subsets can be in any order.

 vector<vector<int>> subsetsWithDup(vector<int>& nums) 
    {
        DFS(nums,0);
        return res;
    }
    vector<vector<int>>res;//结果
    vector<int> cur;//当前结果

    void DFS(vector<int>&num,int startindex)
    {
       res.push_back(cur);//子集中有一个空集
       for(int i=startindex;i<num.size();i++)//需要去重的话勇for循环
       {
           if(i!=startindex&&num[i]==num[i-1]) continue;
           cur.push_back(num[i]);
           DFS(num,i+1);
           cur.pop_back();
       }

    }

2) Given a set of numbers ( without repeated elements ), find all the subsets that can be formed by this set of numbers. There cannot be no repeated subsets in the result. For example: nums[]={1,2,3} The result is: [ [],[1],[1,2],[1,2,3],[1,3],[2],[2,3],[3]] Idea: Each number has optional and Do not choose two cases. If you choose 1, recursively choose 2

void fn(int i, vector<int>& num, vector<int>& temp, vector<vector<int>>& ret)
{
	if (i >= num.size())
		return;
	temp.push_back(num[i]);
	ret.push_back(temp);//存起来倒最终结果
	fn(i + 1, num, temp, ret);
	temp.pop_back();//回溯时,把最后放的数据拿出来
	fn(i + 1, num, temp, ret);//继续递归的向下走
	//不选择数还是选择数都要往后递归,比如pop出了2 还是要递归的选择2之后的数
}
void main()
{
	vector<int> num;
	num.push_back(1);
	num.push_back(2);
	num.push_back(3);
	num.push_back(4);
	vector<int> temp;
	vector<vector<int>>ret;
	fn(0, num, temp, ret);
	for (int i = 0; i < ret.size(); i++)
	{
		for (int j = 0; j < ret[i].size(); j++)
		{
			cout << ret[i][j] << " ";
		}
		cout << endl;
	}
}

4. When encountering the problem of 26 English letters, when it happens that the remainder operation is needed, you can press the letters 0-25 to correspond to English A to Z, so that the number corresponding to a certain letter can be obtained by taking the remainder of 26

Why not use 1-26?

Because 26%26=0, but the actual corresponding letter should be z

5. substr() function

It is a C++ language function. Its main function is to copy a substring , which is required to start from a specified position and have a specified length. If no length Count is specified or Count+_Off exceeds the length of the source string, the substring will continue to the end of the source string. ——From the encyclopedia entry

Syntax substr(size_type_Off = 0, size_type_Count = npos) A form of constructing string: s.substr(pos, len) return value: string, including a copy of len characters starting from pos in s (the default of pos The value is 0, and the default value of len is s.size() - pos, that is, if no parameter is added, the entire s will be copied by default) Exception: If the value of pos exceeds the size of string, the substr function will throw an out_of_range exception; if The value of pos+n exceeds the size of the string, then substr will adjust the value of n and only copy it to the end of the string

6.isalpha(): A function to determine whether a character is a letter

isplnum(): A function to determine whether a character is a letter or a number

tolower(); The function of converting uppercase letters to lowercase letters is also a single character conversion

toupper(); convert lowercase letters to uppercase, single character

See the following code for parameters and return values

bool isPalindrome(string s) 
    {
       string str;//只保留
       if(s==" ")return true;
		for (int i = 0; i < s.size(); i++)
		{
			if (isalnum(s[i]))//验证是不是字母或者数字的函数
			{
				str +=tolower(s[i]);//把大写转小写
				
			}
		}
		
		for (int i = 0, j = str.size() - 1; i < j; i++, j--)
		{
			if (str[i] != str[j])
			{
				return false;
			}
		}
		return true;
    }
void main()
	{
		string s = "A man, a plan, a canal: Panama";
		cout << isPalindrome(s) << endl;
	}

7. The return value of vector<string> type, the push_back method into it

Define a string type variable s, convert a separate part to be put into it with the to_string function, s can perform + operation, this description may be a bit difficult to understand, here is an example

Given a sorted integer array nums with no duplicate elements. Returns the smallest ordered list of interval ranges that exactly cover all the numbers in the array. That is, each element of nums is covered by exactly some interval range, and there is no number x that falls within a certain range but does not belong to nums.

 vector<string> summaryRanges(vector<int>& nums) 
    {
        vector<string> res;
		int n = nums.size();
		int left = 0, right = left + 1;
       while(left<n)
       {
           string s="";
           while(right<n&&nums[right]==nums[right-1]+1)
           {
               right++;
           }
           if(left<right-1)//这里比较的是right-1 是因为上一个while循环中是比较后对right进行了++的
           {
               s+=to_string(nums[left]);
               s+="->";
               s+=to_string(nums[right-1]);
           }
           else if(left==right-1)
           {
               s+=to_string(nums[left]);
           }
           res.push_back(s);
           left=right;
           right++;
       }
		return res;
    }

Input output example:

Input: nums = [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: The interval range is: [0,2] - -> "0->2" [4,5] --> "4->5" [7,7] --> "7"

Explanation: The question requires to divide the space. It is necessary to push_back a piece of string type data into res, so we define s, and after putting a piece of data to be inserted, just put s push_back in.

Guess you like

Origin blog.csdn.net/weixin_62456756/article/details/130486922