代码随想录Day9

lc151

package 字符串;

import java.util.Scanner;

public class lc151 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s = sc.nextLine();
      System.out.println(fuction01(s));
      
    }



    private static StringBuilder fuction01(String s) {
        // System.out.println("ReverseWords.removeSpace() called with: s = [" + s + "]");
        int start = 0;
        int end = s.length() - 1;
        while (s.charAt(start) == ' ') start++;
        while (s.charAt(end) == ' ') end--;
        StringBuilder sb = new StringBuilder();
        while (start <= end) {
            char c = s.charAt(start);
            if (c != ' ' || sb.charAt(sb.length() - 1) != ' ') {
                sb.append(c);
            }
            start++;
        }
        // System.out.println("ReverseWords.removeSpace returned: sb = [" + sb + "]");
        return sb;
    }
}

kam55

package 字符串;

import java.util.Scanner;

public class kam55 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        String s = in.nextLine();

        int len = s.length();  //获取字符串长度
        char[] chars = s.toCharArray();
        reverseString(chars, 0, len - 1);  //反转整个字符串
        reverseString(chars, 0, n - 1);  //反转前一段字符串,此时的字符串首尾尾是0,n - 1
        reverseString(chars, n, len - 1);  //反转后一段字符串,此时的字符串首尾尾是n,len - 1

        System.out.println(chars);

    }

    public static void reverseString(char[] ch, int start, int end) {
        //异或法反转字符串,参照题目 344.反转字符串的解释
        while (start < end) {
            ch[start] ^= ch[end];
            ch[end] ^= ch[start];
            ch[start] ^= ch[end];
            start++;
            end--;
        }
    }
   /* public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt();
        String str = sc.nextLine();

        System.out.println(functon(n,str));

    }

    //事件超时
    private static String functon(int n, String str) {
        StringBuilder sb=new StringBuilder();
        sb.append(str.substring(str.length()-n));
        sb.append(str.substring(0,str.length()-n));
        String s = sb.toString();
        String resl = "";
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i)==32){  //空格的码表是32
                continue;
            }
            resl+=s.charAt(i);
        }
        return  resl;
    }*/
}

KMP求next()


    private static int[] getNext(String s) {
        int next[]=new int[s.length()];
        next[0]=0;
        int j=0;
        for (int i = 1; i < s.length(); i++) {
            while (j>0 && s.charAt(j)!=s.charAt(i)){
                j=next[j-1];
            }
            if (s.charAt(j)==s.charAt(i)){
                j++;
            }
            next[i]=j;
        }
        return next;
    }

lc28

class Solution {
    //前缀表(不减一)Java实现
    public int strStr(String haystack, String needle) {
        if (needle.length() == 0) return 0;
        int[] next = new int[needle.length()];
        getNext(next, needle);

        int j = 0;
        for (int i = 0; i < haystack.length(); i++) {
            while (j > 0 && needle.charAt(j) != haystack.charAt(i)) 
                j = next[j - 1];
            if (needle.charAt(j) == haystack.charAt(i)) 
                j++;
            if (j == needle.length()) 
                return i - needle.length() + 1;
        }
        return -1;

    }
    
    private void getNext(int[] next, String s) {
        int j = 0;
        next[0] = 0;
        for (int i = 1; i < s.length(); i++) {
            while (j > 0 && s.charAt(j) != s.charAt(i)) 
                j = next[j - 1];
            if (s.charAt(j) == s.charAt(i)) 
                j++;
            next[i] = j; 
        }
    }
}

全部评论

相关推荐

头像
10-22 20:13
中南大学 Java
序言大家好呀。我是希晨er,一个初入职场的程序猿小登最近上班摸鱼刷到了一篇文章:10年深漂,放弃高薪,回长沙一年有感,还有聊聊30岁大龄程序员过往的心路历程,突然就有点感慨。我如今也做出了和大明哥一样的抉择,只是更早。此外我22年的人生,好像从来没好好记录过。正好现在工作不太忙,就想把这些经历写下来,也希望能得到社区里各位前辈的指点个人背景我是03年出生的西安娃,父母都是普通打工人。刚从中南大学软件工程专业毕业半年,现在在老家的央企过着躺平摆烂的日子成长轨迹从农村到城市的童年我家并不是西安的,只是爸妈在西安上班,从小学之后就把我接到了西安。后来老家房子拆了,爷爷奶奶也搬了过来。农村的生活,我觉...
Yki_:看哭了,恋爱那一段你女朋友说你不够关心她,可你毕竟也愿意遇到矛盾写几千字来和她慢慢分析;说不愿意给她花钱,我感觉可能只是消费观不一样;如果她想留在长沙,也应该提前跟你说开。不过她也许会心疼你放弃大厂offer转向数字马力?我也因为同样的原因有过一段幸福而充满遗憾的感情,不过跟爱情相比确实前途更重要一点。至于offer的选择,换我我也会这么选。把这些旧事记录下来以后,接下来就好好向前看吧,加油兄弟
🍊晨光随笔
点赞 评论 收藏
分享
头像
昨天 20:19
已编辑
门头沟学院 人工智能
本文略长,献给身处双非、学院本科的低年级依旧陷入迷茫的同学,一个参考。夹杂强烈主观因素,若观点不同,仅当笑料。近日,工作之余的午休时间给母校的学弟学妹进行了宣讲,同时也接受了牛客的访谈,不约而同的触发了两个关键词考研,就业。现象今年和去年,认识的学弟学妹,来自知某、抖某、牛客等系列的学弟学妹,这次宣讲,约有20个学弟学妹来加了我的联系方式,向我取经,聊聊未来,聊聊想法。我这里简单概括一下。1.现在很迷茫,大方向摇摆就业还是考研,但是倾向考研。小方向摇摆竞赛和项目,不知道怎么去做,不知道怎么开始。2.考研的直接目的绝大多数都是为了(混)学历,根本目的就是提高就业竞争力。3.我把他们都拉了个群,在...
牛客85294058...:“私聊能够滔滔不绝,而拉了一个小群之后就完全一声不吭”个人观点这跟从小到大“不要浪费大家时间”的社会环境有关:个人化的提问,如果你上学时有留心、或者参加QA环节多,会注意到这种做法经常是被人骂的。要营造让大家开口的氛围和做出欢迎讨论的议题设置还是比较难的,期待方法探索。
投递大连飞创信息技术有限公司等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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