leetcode 12 30

12 30

381. The number of dictionaries sorted medium

topic

Insert picture description here

The essence of the subject

First traverse the decimal tree as follows
[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-i6jxk4y3-1609381252981)(6DF74620992E47D09EF38C165119F950)]

The first less perfect code

class Solution {
    
    
    public List<Integer> lexicalOrder(int n) {
    
    
         List<Integer> nums=new ArrayList<Integer>();
		 dfs(nums, 0, n);
		 for(int i=0;i<nums.size();i++)
		 {
    
    
			 System.out.println(nums.get(i));
		 }
		 return nums;
    }

    public static void dfs(List<Integer> nums,int num,int n)
	{
    
    
		if(num>n)
		{
    
    
			return;
		}
		for(int j=0;j<=9;j++)
		{
    
    
			num=num*10+j;
			if(num==0)
				continue;
			if(num>n)
				break;
			nums.add(num);
			dfs(nums, num, n);
			num=(num-j)/10; 
			
		}
		return;
	}

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-hK6EcZkv-1609381252985)(FEF8AEE6742A430681758638E52FEA16)]

Imperfect analysis:
1. Mainly lies in the variable write-back operation, which causes a huge time consumption
2. The method parameter transfers the list in the middle, which causes a huge time consumption
3. After testing, the two aspects are the main reasons, resulting in the time exceeding 200ms, normal 5- 7ms

The second more complete code


class Solution {
    
    
     List<Integer> nums=new ArrayList<Integer>();
	 public  List<Integer> lexicalOrder(int n) {
    
    
		 
		 dfs( 0, n,0);
		 return nums;
	 }
	public void dfs(int num,int n,int step)
	{
    
    
		if(num>n)
		{
    
    
			return;
		}
		if(num>0)
		{
    
    
			nums.add(num);
		}
		for(int i=step>0?0:1;i<=9;i++)
		{
    
    
			dfs( num*10+i, n, step+1);
		}
	}
}

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-RhS6vOM8-1609381252995) (586760053D5C441ABFB131691E63B74A)]

Improvement plan
1. For variable write-back, use the method parameter to operate the variable, so that when returning to the previous node, it will automatically recover
2. For the passed list parameter, change the dfs method to the instance method, and create the instance global variable list.

Interview questions 05.01. Easy to insert

topic

[External link image transfer failed, the source site may have an anti-leech link mechanism, it is recommended to save the image and upload it directly (img-ciEA9w2Y-1609381252996)(759479C9CC684E0C87B287421E90AD74)]

The essence of the subject

For unsigned integers, they exist as original codes in 32-bit machines. The overlapping bits of N and M are replaced by M.

Code

class Solution {
    
    
     public  int insertBits(int N, int M, int i, int j) {
    
    
		 	char[] s1=to2array(N);
		 	char[] s2=to2array(M);
		 	int u=0;
		 	for(int q=i;q<=j;q++)
		 	{
    
    
		 		s1[q]=s2[u];
		 		u++;
		 	}
		 	int n=toten(s1);
		 	return n;
		 	
	    }
	public static int toten(char[] q)
	{
    
    
		int n=0;
		int l=1;
		for(int i=0;i<32;i++)
		{
    
    
			if(i!=0)
				l=l*2;
			if(q[i]=='0')
			{
    
    
				
				continue;
			}
			int j=q[i]-'0';
		
			n=n+l*j;
			
		}
		return n;
	}
	public static char[] to2array(int m)
	{
    
    
		char[] t= new char[32];
	 	int n=m;
	 	int i=0;
	 	for(int j=0;j<32;j++)
	 	{
    
    
	 		t[j]='0';
	 	}
	 	while(n!=0)
	 	{
    
    	
	 		char l=(char) (n%2+'0');
	 		n=n/2;
	 		t[i]=l;
	 		i++;
	 	}
	 	return t;
	}
	
}

Analysis:
1. to2array() converts a decimal number into a binary array of characters with a length of 32, using zero extension
2 toten() is a character array with a length of 32 to convert to a decimal number
tips:
'0' means The character 0, when it participates in the operation, is automatically converted to the int type, which is the asscci code. If the number involved in the operation is still 0, -'0' is required. In the same way, if you want to convert 0 to a character, you need to add +'0', and int will be automatically converted to char

1220 Difficulty in counting the number of vowel sequences

topic:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-tP1S2FnJ-1609381252998)(3EAA08A15B234D6590203256731ED593)]

The first less perfect code:

Analysis:
Using dynamic programming, map[n][i] represents the number of character strings ending with the letter corresponding to the nth position (n...1) and i.
State transition equation:
map[i][1]+=map[i +1][0];//a can only be followed by e
map[i][0]+=map[i+1][1];//e can only be followed by a or i
map[i][2 ]+=map[i+1][1];
map[i][0]+=map[i+1][2];//i cannot be followed by another i
map[i][1]+= map[i+1][2];
map[i][3]+=map[i+1][2];
map[i][4]+=map[i+1][2];
map[ i][2]+=map[i+1][3];//o can only be followed by "i" or "u"
map[i][4]+=map[i+1][3];
map[i][0]+=map[i+1][4];//u can only be followed by a

class Solution {
    
    
   public  int countVowelPermutation(int n) {
    
    
       long[][] map=new long[n][5];
        //'a', 'e', 'i', 'o', 'u'对应的字母
       //  0    1    2    3    4
       int mod = 1000000007;
       for(int j=0;j<5;j++)
   		{
    
    
  
   			map[n-1][j]=1;
   		
   		}
        for(int i=n-2;i>=0;i--)
        {
    
    
    		map[i][1]+=map[i+1][0];//a后面只能跟e
    		map[i][0]+=map[i+1][1];//e后面只能跟a或者i
    		map[i][2]+=map[i+1][1];
    		map[i][0]+=map[i+1][2];//i后面不能跟着另一个i
    		map[i][1]+=map[i+1][2];
    		map[i][3]+=map[i+1][2];
    		map[i][4]+=map[i+1][2];
    		map[i][2]+=map[i+1][3];//o后面只能跟着“i”或者“u”
    		map[i][4]+=map[i+1][3];
    		map[i][0]+=map[i+1][4];//u后面只能跟着a
    		for(int j=0;j<5;j++)
            {
    
    
            	map[i][j]=map[i][j]%mod;
            }
        }
        long y=0;
        for(int i=0;i<5;i++)
        {
    
    
        	y+=map[0][i];
        }
        int result=(int)(y%mod);
        return result;
   }
	
}

Imperfections:
1. The map[][] accesses too many states. In fact, only the upper-level state is needed, not all storage.
2. The intermediate result modulo 10 to the 9th power +7 uses long to save the precision, but all intermediate results need to be modulo division

The second more complete code:

Analysis:
1. Use a layer of variables and a layer of local variables for updating

class Solution {
    
    
   public  int countVowelPermutation(int n) {
    
    
       long a=1,e=1,i=1,o=1,u=1;
        //特定字母之后能排的字母
		//a  e
		//e  ai
		//i  aeou
		//o  iu
		//u  a
		
		//反推出特定字母之前能排的字母
		//eiu     a
		//ai      e
		//eo      i
		//i       o
		//io      u
		int mod=1000000007;
		for(int q=0;q<n-1;q++)
		{
    
    
			long a_tmp=(e+i+u)%mod;
			long e_tmp=(a+i)%mod;
			long i_tmp=(e+o)%mod;
			long o_tmp=i;
			long u_tmp=(i+o)%mod;
			
			a=a_tmp;
			e=e_tmp;
			i=i_tmp;
			o=o_tmp;
			u=u_tmp;
		}
		return  (int)((a+e+i+o+u)%mod);
   }
	
}

Insert picture description here

Guess you like

Origin blog.csdn.net/hch977/article/details/112003784