720. 重排带整数字符串
给一包含大写字母和整数(从 0 到 9)的字符串, 试写一函数返回有序的字母以及数字和.
样例
给出 str = AC2BEW3, 返回 ABCEW5
字母按字母表的顺序排列, 接着是整数的和(2 和 3).
public class Solution {
/**
* @param str: a string containing uppercase alphabets and integer digits
* @return: the alphabets in the order followed by the sum of digits
*/
public String rearrange(String str) {
// Write your code here
if(str==null||str.equals(""))
return "";
int s[]=new int[26];
int num[]=new int[10];
String result="";
for(char c:str.toCharArray()){
if(Character.isDigit(c))
num[c-'0']++;
else{
s[c-'A']++;
}
}
for(int i=0;i<26;i++)
while (s[i]!=0){
result+=(char)(i+'A');
s[i]--;
}
int sum=0;
for (int i=0;i<10;i++)
while (num[i]!=0){
sum+=i;
num[i]--;
}
return result+sum;
}
}
730. 所有子集的和
给一整数 n, 我们需要求前n个自然数形成的集合的所有可能子集中所有元素的和。
样例
给出 n = 2, 返回 6
可能的子集为 {{1}, {2}, {1, 2}}.
子集的元素和为 1 + 2 + 1 + 2 = 6
给出 n = 3, 返回 24
可能的子集为 {{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
子集的和为:
1 + 2 + 3 + (1 + 2) + (1 + 3) + (2 + 3) + (1 + 2 + 3) = 24
思路:通过计算每个元素在求和过程中出现的次数,尝试获取一种规律。
[1]==> 0+1=1 // 1出现一次[1,2]==>0+1+2+(1+2)=6 // 1出现2次,2出现2次[1,2,3]===>0+1+2+3+(1+2)+(1+3)+(2+3)+(1+2+3)=24 // 1,2,3出现4次
好像有点规律了,每个元素在求和过程中出现的次数是一样的。假设出现的次数是N。
sum = (1+2+3+4+…+n) * N
N的值又和数组的长度有关系,N=2^(n-1)
sum = (1+2+3+4+…+n) * 2^(n-1)
public class Solution {
/**
* @param n: the given number
* @return: Sum of elements in subsets
*/
public int subSum(int n) {
// write your code here
int sum=0;
for(int i=0;i<=n;i++)
sum+=i;
return sum*(int)Math.pow(2,n-1);
}
}
765. Valid Triangle
Given three integers a, b, c, return true if they can form a triangle.
样例
Given a = 2, b = 3, c = 4
return true
Given a = 1, b = 2, c = 3
return false
public class Solution {
/**
* @param a: a integer represent the length of one edge
* @param b: a integer represent the length of one edge
* @param c: a integer represent the length of one edge
* @return: whether three edges can form a triangle
*/
public boolean isValidTriangle(int a, int b, int c) {
// write your code here
int res[]={a,b,c};
Arrays.sort(res);
if((res[0]+res[1]>res[2])&&(res[2]-res[0]<res[1]))
return true;
else return false;
}
}
749. 约翰的后花园
约翰想在他家后面的空地上建一个后花园,现在有两种砖,一种3 dm的高度,7 dm的高度。约翰想围成x
dm的墙。如果约翰能做到,输出YES,否则输出NO。
样例
给出 x = 10,返回YES。
解释:
x = 3 + 7 : 即需要1匹3 dm高度的砖和1匹7 dm 高度的砖。
给出 x = 5,返回 NO。
解释:
不能用高度为3 dm的砖和高度为7 dm的砖砌成 5 dm的墙。
给出 x = 13,返回YES。
解释:
x = 2 * 3 + 7 : 即需要2匹3 dm高度的砖和1匹7 dm 高度的砖。
public class Solution {
/**
* @param x: the wall's height
* @return: YES or NO
*/
public String isBuild(int x) {
// write you code here
if(x==0)
return "YES";
return Judge(x)?"YES":"NO";
}
boolean Judge(int x){
if(x==0)
return true;
if(x<0)
return false;
return Judge(x-3)||Judge(x-7);
}
}
737. 查找矩阵
给一矩阵, 找到矩阵中每一行都出现的元素. 你可以假设矩阵中只有一个满足条件的元素.
样例
给一矩阵:
[
[2,5,3],
[3,2,1],
[1,3,5]
]
返回 3
public class Solution {
/**
* @param Matrix: the input
* @return: the element which appears every row
*/
public int FindElements(int[][] Matrix) {
// write your code here
if(Matrix.length==0||Matrix[0].length==0)
return -1;
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<Matrix[0].length;i++)
map.put(Matrix[0][i],1);
for(int j=1;j<Matrix.length;j++)
for(int i=0;i<Matrix[0].length;i++){
int key=Matrix[j][i];
if(map.containsKey(key)&&map.get(key)<j+1)
map.put(key,map.get(key)+1);
}
for(Integer key:map.keySet())
if(map.get(key)==Matrix.length)
return key;
return -1;
}
}