快手工程B试卷第三题咋做呀

前两个AC第三个时间不够没写完过了17.6%
有啥通俗易懂的思路嘛

编辑一下
不会用动态规划,第一题代码。。。

#include<iostream>
using namespace std;
#include<string>

int Get(string& s1, string& s2)
{
    int ret = 0;
    if (s1.size() == s2.size())
    {
        if (s1.c_str() == s2.c_str())
        {
            return ret;
        }
        else
        {
            size_t i = 0;
            size_t j = 0;
            int grep = 0;
            for (i; i < s1.size(); ++i)
            {
                for (j; j < s2.size(); ++j)
                {
                    if (s1[i] == s2[j])
                    {
                        grep++;
                        break;
                    }
                }
            }
            return s1.size() - grep;
        }
    }
    else
    {
        size_t i = 0;
        size_t j = 0;
        size_t grep = 0;
        for (i; i < s1.size(); ++i)
        {
            j = grep;
            for (j; j < s2.size(); ++j)
            {
                if (s1[i] == s2[j])
                {
                    ret++;
                    grep = j;
                    break;
                }
            }
        }
        return s1.size() + s2.size() - 2 * ret;
    }
}
第二题,每次保留两位数,然后0返回的是1.

#快手##笔试题目##春招#
全部评论
层级遍历 import java.util.LinkedList; import java.util.Scanner; public class Main {    public static void main(String[] args) {       Scanner in = new Scanner(System.in);       String s = in.next();       String[] split = s.split(",");       int x = Integer.valueOf(split[0]);       int y = Integer.valueOf(split[1]);       LinkedList<Integer> list = new LinkedList<>();       int times = 0;       int count = 0;       int temp = x;       while(temp != y) {           list.add(temp+1);           list.add(temp-1);           list.add(temp*2);           if(count == 0) {               count = list.size();               times++;           }           temp = list.removeFirst();           count--;       }       System.out.println(times);    } }
点赞 回复 分享
发布于 2019-04-13 17:21
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); String[]ss = s.split(","); dfs(Integer.parseInt(ss[0]),Integer.parseInt(ss[1]),0 );System.out.println(ans);} } static int ans = Integer.MAX_VALUE; static void dfs(int a,int b,int curstep) { if(ans <= curstep)return; if(a == b) { ans = Math.min(ans,curstep); return; } if(a > b) dfs(a-1,b,curstep+1); else if(a < b) { if(a > 0) { dfs(2*a,b,curstep+1); } dfs(a+1,b,curstep+1); } }}
点赞 回复 分享
发布于 2019-04-13 17:31
感觉思路挺清晰的,但是最后83.3%,哭了
点赞 回复 分享
发布于 2019-04-13 17:20
https://www.nowcoder.com/discuss/177739 ac但是其实逻辑不全~
点赞 回复 分享
发布于 2019-04-13 17:08
求发前两题代码
点赞 回复 分享
发布于 2019-04-13 17:07

相关推荐

07-07 12:47
门头沟学院 Java
码农索隆:竟然还真有卡体检报告的
点赞 评论 收藏
分享
07-15 11:41
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务