LeetCode 17th Biweekly

51443 Decompression Encoding List
Give you an integer list nums compressed in run length encoding.

Consider every two adjacent elements [a, b] = [nums[2i], nums[2i+1] (where I >= 0), and each pair represents an element with a value of B after decompression.

Please return to the unzipped list.

Example:

Input: nums = [1,2,3,4]
Output: [2,4,4,4]

Tips:

2 <= nums.length <= 100
nums.length % 2 == 0
1 <= nums[i] <= 100

public static int [] ans(int []nums){
		int sum=0;
		for(int i=0;i<nums.length;i+=2){
			sum+=nums[i];
		}
		int ans[]=new int[sum];
		int p=0;
		for(int i=0;i<nums.length;i+=2){
			for(int j=0;j<nums[i];j++)
				ans[p+j]=nums[i+1];
			p+=nums[i];
		}
		return ans;
	}

5144. Matrix regions and
Give you a matrix mat of m * n and an integer K, and return a matrix answer, where each answer[i][j] is the sum of all the elements mat[r][c] that meet the following conditions:

i - K <= r <= i + K, j - K <= c <= j + K
(r, c) in the matrix.

Example 1:

Input: mat = [[1,2,3], [4,5,6], [7,8,9], K = 1
Output: [[12,21,16], [27,45,33], [24,39,28]]
Example 2:

Input: mat = [[1,2,3], [4,5,6], [7,8,9], K = 2
Output: [[45,45,45], [45,45,45], [45,45,45]]

Tips:

m == mat.length
n == mat[i].length
1 <= m, n, K <= 100
1 <= mat[i][j] <= 100

public static int [][] ans(int[][] mat, int K){
		int answer[][]=new int[mat.length][mat[0].length];
		for(int i=0;i<answer.length;i++){
			for(int j=0;j<answer[0].length;j++){
				
				for(int x=i-K;x<=i+K;x++){
					for(int y=j-K;y<=j+K;y++){
						if(x>=0&&y>=0&&x<mat.length&&y<mat[0].length)
						answer[i][j]+=mat[x][y];
					}
				}
				
			}
		}
		return answer;
	}

5145. Nodes with even grandparent node values and
Give you a binary tree, and return the sum of the values of all nodes that meet the following criteria:

The value of this node's grandparent node is even.(The grandparent node of a node is the parent node of its parent.)
Returns 0 if there is no node whose grandparent node value is even.

Example:

Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output: 18
Explanation: The grandparent nodes of the red nodes in the diagram have even values, and the blue nodes are the grandparents of the red nodes.

Tips:

The number of nodes in the tree is between 1 and 10^4.
Each node has a value between 1 and 100.

	public static int ans(TreeNode root){
		int sum=0;
		HashSet<TreeNode> set=new HashSet<TreeNode>();
		HashSet<TreeNode> old,cur;
		set.add(root);
		if(root.val%2==0)
		sum+=find(root);
		do{
			cur=new HashSet<TreeNode>();
			Iterator it = set.iterator();		
			while(it.hasNext()){
				TreeNode key=(TreeNode) it.next();
				if(key.left!=null){
					cur.add(key.left);
				}
				if(key.right!=null){
					cur.add(key.right);
				}
			}
			old=set;
			set=cur;
			Iterator itt = set.iterator();		
			while(itt.hasNext()){
				TreeNode key=(TreeNode) itt.next();
				if(key.val%2==0){
					sum+=find(key);
				}
			}
		}while(set.size()>0);
		return sum;
	}
	public static int find(TreeNode root){
		int ans=0;
		if(root.left!=null){
			if(root.left.left!=null)ans+=root.left.left.val;
			if(root.left.right!=null)ans+=root.left.right.val;
		}
		if(root.right!=null){
			if(root.right.left!=null)ans+=root.right.left.val;
			if(root.right.right!=null)ans+=root.right.right.val;
		}
		return ans;
	}

5146.Different cyclic substrings
Give you a string text and return the number of different non-empty substrings that can be written as a string concatenated with itself.

Example 1:

Input: text = "abcabcabc"
Output: 3
Interpretation: The three substrings are "abcabc", "bcabca" and "cabcab cab".
Example 2:

Input: text = "leetcodeleetcode"
Output: 2
Interpretation: The two substrings are "ee" and "leetcodeleetcode".

Tips:

1 <= text.length <= 2000
text contains only lowercase English letters.

public static int ans2(String text){
		int n = text.length();
		HashSet<String> set = new HashSet<>();
		for (int i = 0; i < n; i++) {
			for (int j = i+1; j < n; j++) {
				int left = i,right = j;
				if (j+j-i>n)break;
				if (text.substring(i,j).equals(text.substring(j,j+j-i))){
					set.add(text.substring(i,j));
				}
			}
		}
		return set.size();
	}
39 original articles published, 14 praised, 4075 visits
Private letter follow

Tags: encoding

Posted on Sat, 11 Jan 2020 22:21:10 -0500 by Tremor