代码随想录Day08

lc344

class Solution {
    public void reverseString(char[] s) {
int left=0;
           int right= s.length-1;
           while (left<right){
               char temp=s[right];
               s[right]=s[left];
               s[left]=temp;
               left++;
               right--;
           }
    }
}

lc541

class Solution {
  public static String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();
          for (int i=0;i< chars.length;i+=2*k){
              if (i+k<=chars.length){
                  reverse(chars,i,i+k-1);
              }else{
                 reverse(chars,i,chars.length-1);
              }
             
          }

          return new String(chars);
    }

    private static void reverse(char[]chars, int i, int end) {
        while (i<end){
            char temp=chars[i];
            chars[i]=chars[end];
            chars[end]=temp;
            end--;
            i++;
        }
           }

}

kama54


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

    private static String reverse(String s) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.charAt(i))) {
                result.append("number");
            } else {
                result.append(s.charAt(i));
            }
        }
        return result.toString();
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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