993. Array Partition I
Given an array of 2n integers, your task is to group these integers
into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which
makes sum of min(ai, bi) for all i from 1 to n as large as possible.
样例
Input: [1,4,3,2]
Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
public class Solution {
/**
* @param nums: an array
* @return: the sum of min(ai, bi) for all i from 1 to n
*/
public int arrayPairSum(int[] nums) {
// Write your code here
Arrays.sort(nums);
int result=0;
for(int i=0;i<nums.length;i+=2)
result+=nums[i];
return result;
}
}
1032. Letter Case Permutation
Given a string S, we can transform every letter individually to be
lowercase or uppercase to create another string. Return a list of all
possible strings we could create.
样例
Input: S = “a1b2”
Output: [“a1b2”, “a1B2”, “A1b2”, “A1B2”]
Input: S = “3z4”
Output: [“3z4”, “3Z4”]
Input: S = “12345”
Output: [“12345”]
public class Solution {
/**
* @param S: a string
* @return: return a list of strings
*/
public List<String> letterCasePermutation(String S) {
// write your code here
List<String>result=new LinkedList<>();
result.add("");
for(Character c:S.toCharArray()){
int len=result.size();
if(c>='0'&&c<='9')
for(int i=0;i<len;i++)
result.set(i,result.get(i)+c);
else {
for(int i=0;i<len;i++){
result.add(result.get(i));
result.set(i,result.get(i)+Character.toLowerCase(c));
result.set(i+len,result.get(i+len)+Character.toUpperCase(c));
}
}
}
return result;
}
}
1033. Minimum Distance Between BST Nodes !!!!!重点
Given a Binary Search Tree (BST) with the root node root, return the
minimum difference between the values of any two different nodes in
the tree.
样例
Input: root = {4,2,6,1,3,#,#}
Output: 1
Explanation:
Note that root is a TreeNode object, not an array.
The given tree {4,2,6,1,3,#,#} is represented by the following diagram:
4
/ \
2 6
/ \
1 3
while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: the root
* @return: the minimum difference between the values of any two different nodes in the tree
*/
public void helper(TreeNode root,List<Integer>list){
if(root==null)
return;
helper(root.left,list);
list.add(root.val);
helper(root.right,list);
}
public int minDiffInBST(TreeNode root){
int result=Integer.MAX_VALUE;
List<Integer> list=new LinkedList<>();
helper(root,list);
for(int i=1;i<list.size();i++)
result=Math.min(result,list.get(i)-list.get(i-1));
return result;
}
}
1038. Jewels and Stones
You’re given strings J representing the types of stones that are
jewels, and S representing the stones you have. Each character in S is
a type of stone you have. You want to know how many of the stones you
have are also jewels.The letters in J are guaranteed distinct, and all characters in J and
S are letters. Letters are case sensitive, so “a” is considered a
different type of stone from “A”.
样例
Example 1:
Input: J = “aA”, S = “aAAbbbb”
Output: 3
Example 2:
Input: J = “z”, S = “ZZ”
Output: 0
public class Solution {
/**
* @param J: the types of stones that are jewels
* @param S: representing the stones you have
* @return: how many of the stones you have are also jewels
*/
public int numJewelsInStones(String J, String S) {
// Write your code here
Map<Character,Integer>map=new HashMap<>();
int result=0;
for(Character c:J.toCharArray()){
map.put(c,1);
}
for(Character c:S.toCharArray()){
if(map.containsKey(c))
result++;
}
return result;
}
}
1042. Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right
has the same element.Now given an M x N matrix, return True if and only if the matrix is
Toeplitz.
样例
Example 1:
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: True
Explanation:
1234
5123
9512
In the above grid, the diagonals are “[9]”, “[5, 5]”, “[1, 1, 1]”, “[2, 2, 2]”, “[3, 3]”, “[4]”, and in each diagonal all elements are the same, so the answer is True.
Example 2:
Input: matrix = [[1,2],[2,2]]
Output: False
Explanation:
The diagonal “[1, 2]” has different elements.
public class Solution {
/**
* @param matrix: the given matrix
* @return: True if and only if the matrix is Toeplitz
*/
public boolean isToeplitzMatrix(int[][] matrix) {
// Write your code here
for(int i=0;i<matrix.length-1;i++)
for(int j=0;j<matrix[i].length-1;j++)
if(matrix[i][j]!=matrix[i+1][j+1])
return false;
return true;
}
}