有点晕,其实题目不难,但是自己模拟类的题目做的好少!! leetcode上刷了270多题,模拟题太少了,每次碰到都有点懵,最后一个dfs的题一个低级错误(倒
第一题:
public class Code_0905_sougou1 {
public int numberofprize (int a, int b, int c) {
int l = 0;
int r = Math.max(a,Math.max(b,c));
while (l < r){
int mid = l + r + 1 >> 1;
if (check(a,b,c,mid)){
l = mid;
}else {
r = mid - 1;
}
}
return l;
}
public boolean check(int a,int b ,int c ,int mid){
int plus = 0;
if (a >= mid){
plus += (a - mid);
}
if (b >= mid){
plus += (b - mid);
}
if (c >= mid){
plus += (c - mid);
}
int minus = 0;
if (a <= mid){
minus += (mid- a);
}
if (b <= mid){
minus += (mid- b);
}
if (c <= mid){
minus += (mid- c);
}
return plus / 2 >= minus;
}
}
第二题:
public class Code_0905_sougou2 {
public int getHouses (int t, int[] xa) {
// write code here
int res = 2;
int n = xa.length;
int[] x = new int[n / 2];
int[] a = new int[n / 2];
int index = 0;
for (int i = 0;i < xa.length;i+=2){
x[index] = xa[i];
a[index] = xa[i + 1];
}
if (n == 1){
return res;
}
for (int i = 0;i < n - 1;i++){
double lo = (double)x[i] + a[i] / 2.0;
double hi = (double)x[i + 1] - a[i + 1] / 2.0;
if (hi - lo < t)continue;
if (hi - lo == t){
res++;
}else {
res += 2;
}
}
return res;
}
}
第三题
public class Code_0905_sougou3 {
long res = 0;
Set<String> set = new HashSet<>();
public long getPasswordCount (String password) {
if (password.equals("")){
return 0;
}
// write code here
for (int i = 0;i <= 9;i++){
helper(password,1,new StringBuilder("i"));
}
return res;
}
public void helper(String password,int index,StringBuilder temp){
if (index == password.length() ){
if (!(temp.toString()).equals(password) || !set.contains(temp.toString())){
set.add(temp.toString());
res++;
}
return;
}
helper(password,index + 1 ,
temp.append((char)((password.charAt(index) + temp.charAt(temp.length() - 1) / 2) )));
temp.deleteCharAt(temp.length() - 1);
helper(password,index + 1 ,
temp.append((char) ((password.charAt(index) + temp.charAt(temp.length() - 1) + 1) / 2)));
temp.deleteCharAt(temp.length() - 1);
}
}