当前位置:   article > 正文

华为笔试21年4月10日_华为 4-21笔试

华为 4-21笔试

华为笔试21年4月17日

1.反转括号里的字符串

String str = “((jhkv)(love)i)”; // 变为ilovejhkv
String str = “(u(s)(love)i)”; // 变为ilovesu
从里面括号到外面括号依次反转,然后拼接
思路用 递归
定义:inverse(str,l,r) 反转str从l到r不包括括号的字符串

package com.company;

public class fanzhuan {
    public static void main(String[] args) {
//        String str = "((jhkv)(love)i)";
        String str = "(u(s)(love)i)";
        System.out.println(inverse(str, 0, str.length()-1));
    }

    private static String inverse(String str, int l, int r) {
        if(r-l<=1) return "";
        StringBuilder sb = new StringBuilder();
        for(int i=l+1; i<r;i++)
        {
            char c = str.charAt(i);
            if(c != '(') sb.append(c);
            else {
                int j;
                for(j=i+1;i<r;j++)
                {
                    if(str.charAt(j)==')') break;
                }
                sb.append(inverse(str,i,j));
                i=j;
            }
        }
        return sb.reverse().toString();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

减速



#include <iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

int main()
{
    int N;
    cin >> N;
    vector<int> ve(N);
    int temp;
    for (int i = 0; i < N; i++)
    {
        cin >> temp;
        ve[i] = temp;
    }
    vector<int> ans;
    ans.push_back(ve[0]);
    int i = 1, lastspeed = ve[0],count=1,ABEfalg=0,ABEedge=0;
    while (i<N)
    {
        if (lastspeed - ve[i] >= 9)
        {
            ABEfalg++;
        }
        else
        {
            if (ABEfalg >= 4)
            {
                int l = max(ABEedge, i - ABEfalg-4);
                int r = min(N - 1, i + 3);
                for (int jj = l; jj <= r; jj++)
                    ans.push_back(ve[jj]);
                ABEedge = i + 4;
                count = -3;
            }
            ABEfalg = 0;
            
        }
        if (count == 60&&ABEfalg<4)
        {
            ans.push_back(ve[i]);
            count = 0;
        }
        lastspeed = ve[i];i++; count++; 

    }
    if (ABEfalg >= 4)
    {
        int l = max(ABEedge, i - ABEfalg - 4);
        int r = min(N - 1, i + 3);
        for (int jj = l; jj <= r; jj++)
            ans.push_back(ve[jj]);
    }
    int numcc = ans.size() - 1;
    for (int luo=0;luo<numcc;luo++)
        cout << ans[luo] << ',';
    cout << ans[numcc] << endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

3. 最少的基站个数

4
1 3 1 1
第1个基站最多传到1+1=2
第2个基站最多传到2+3=5
最短1-2-4 最少2基站 1 2
1-2-3-4 要3个基站
用两个指针,i指针指向区间范围的最左边,r指针指向区间范围的最右边
当r指向最后一个基站n-1时结束

package com.company;

public class jizhan {
    public static void main(String[] args) {
//        int[] a={2,5,1,1,1,1};
        int[] a={1,3,1,1,2,1,3,1,1};
        System.out.println(leastCount(a));
    }

    private static int leastCount(int[] a) {
        int ans=1, r=a[0];
        int lenp=0;
        for (int i = 0; i < a.length; ) {
            if(r>=a.length-1)break;
            for (int j = i+1; j <= r; j++) {
                if(j+a[j]>lenp+a[lenp]) lenp=j;
            }
            i=r;
            r=lenp+a[lenp];
            ans++;
        }
        return ans;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/572338
推荐阅读
相关标签
  

闽ICP备14008679号