• 创新工场笔试最后两道题


      前几天创新工场来学校开宣讲会招聘,至少有1100多人参加了笔试, 招的人又那么少,简直就是充当炮灰去了~ 

      记住了最后两道题目,如下

      

    第一题:字符串去重,要求不能开辟新空间用来复制原字符串,比如字符串为"abddscbd",去重复后就是"abdsc"

      粘一点自己写的代码,写的比较复杂凌乱,新手~~

     1 /**
     2      * 判断一个字符在字符串中是否重复了两次以上
     3      * @return 重复了两次或两次以上就返回true,否则返回false
     4      */
     5     private static boolean tooMany(String str, char ch) {
     6         int len = str.length();
     7         int count = 0;
     8         for (int i = 0; i < len; i++) {
     9             if (str.charAt(i) == ch) {
    10                 count++;
    11             }
    12         }
    13         return (count > 1) ? true : false;
    14     }
    15     
    16     /**
    17      * 判断字符串中是否有所含的字符
    18      */
    19     private static boolean contain(String str, char ch) {
    20         boolean flag = false;
    21         for (int i = 0; i < str.length(); i++) {
    22             if (str.charAt(i) == ch)
    23                 flag = true;
    24         }
    25         return flag;
    26     }
    27 
    28     public static String toLessStr(String str) {
    29         int i = 0;
    30         while ((str.length() - i) > 0) {
    31             char ch = str.charAt(i);
    32             if (tooMany(str, ch) && (i >= 1)) {
    33                 if (contain(str.substring(0, i), str.charAt(i))) {
    34                     str = str.substring(0, i) + str.substring(i + 1);
    35                 } else {
    36                     i++;
    37                 }
    38             } else {
    39                 i++;
    40             }
    41         }
    42         return str;
    43     }
    44 
    45     public static void main(String[] args) {
    46         // TODO Auto-generated method stub
    47         System.out.println(toLessStr("aacddsdae"));    //输出 acdse
    48     }

    第二题:求逆序对个数,在一个数组a里,如果i < j,a[i]>a[j],那么<a[i],a[j]>称为一个逆序对,如3,5,1,2 一共有<3,1><3,2><5,1><5,2>四个逆序对

      

     1 /**
     2      * 数组的反序的个数
     3      * @return 返回个数
     4      */
     5     static int ArrayTest(int[] arr){
     6         if(arr.length<=1)
     7             throw new IllegalArgumentException("数组小于零");
     8         int len=arr.length;
     9         int count=0;
    10         for(int i=0;i<len-1;i++){
    11             for(int j=i+1;j<len;j++){
    12                 if(arr[i]>arr[j]){
    13                     count++;
    14                 }
    15             }
    16         }
    17         return count;
    18     }
    19     public static void main(String[] args) {
    20         // TODO Auto-generated method stub
    21         int[] a={5,2,3,4,9,1,6};
    22         System.out.println(ArrayTest(a));    //输出 9
    23     }
  • 相关阅读:
    git 拉取开发代码
    android webview
    H5 元素定位
    Python中模块
    服务器1
    活动
    宝塔面板-阿里部署
    最新版Xshell、Xftp注册机NetSarang
    最新版Navicat Premium激活,附激活工具
    9月29更新美版T-mobile版本iPhone7代和7P有锁机卡贴解锁方法
  • 原文地址:https://www.cnblogs.com/Lowp/p/2696825.html
Copyright © 2020-2023  润新知