虾皮笔试题

142 阅读3分钟
theme: simplicity-green
highlight: a11y-dark

反转字符串中的小写字母

详细描述 反转字符串中的小写字母,并且保证单个单词的 顺序不变,其他字符的位置不变字符串只包含大写字母 空格 小写字母以及数字。 例如:"Shopee is Our family123"。输出"Seepoh si Oru ylimaf 123"

 public class main {
     public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);
         String str =sc.nextLine();
         String[] words=str.split(" ");
         for(int i=0;i<words.length;i++){
             String word=words[i];
             StringBuffer reverse=new StringBuffer();
             StringBuffer lower=new StringBuffer();
             //将每次的小写反转存进去
             for(int j=0;j<word.length();j++){
                 char c=word.charAt(j);
                 if(Character.isLowerCase(c)){
                     lower.append(c);
                 }else{
                     reverse.append(c);
                 }
             }
             lower.reverse();
             //插入
             int index=0;
             for(int j=0;j<word.length();j++){
                 char c=word.charAt(j);
                 if (Character.isLowerCase(c)) {
                     reverse.insert(j, lower.charAt(index));
                     index++;
                 }
             }
             words[i] = reverse.toString();
         }
         System.out.println(String.join(" ",words));
     }
 }

题解

很简单,首先用空格分割,保存到数组,然后遍历每个单词,大写直接保存,小写用数组存起来,然后反转。

之后将这个数组里的数据插入reverse数组中就行。

这个情况没考虑大写字母在中间的情况,但是题目也没有要求。

给你一个链表的头节点 head,旋转链表,将链表每个节点向右移动k个位置,

给你一个链表的头节点 head ,旋转链表,将链表每个节点向石移动k个位置。

示例1:

链表:1->2->3->4->5

第-次旋转:5->1->2->3->4第二次旋转:4->5->1->2->3

输入:head =[1,2,3,4,5],k=200

输出:[4,5,1,2,3]

示例2:

链表:1->2->3

第-次旋转:3->1->2

第二次旋转:2->3->1

第三次旋转:1->2->3

第四次旋转:3->1->2

输入:head=[1,2,3],k=4

输出:[3,1,2]

题解:

 class ListNode {
     int val;
     ListNode next;
     
     ListNode() {}
     
     ListNode(int val) {
         this.val = val;
     }
     
     ListNode(int val, ListNode next) {
         this.val = val;
         this.next = next;
     }
 }
 ​
 public class Solution {
     public ListNode rotateRight(ListNode head, int k) {
         if (head == null || head.next == null || k == 0) {
             return head;
         }
         
         // 计算链表长度并连接成环
         ListNode oldTail = head;
         int length;
         for (length = 1; oldTail.next != null; length++) {
             oldTail = oldTail.next;
         }
         oldTail.next = head;
         
         // 找到新的尾节点和头节点
         ListNode newTail = head;
         for (int i = 0; i < length - k % length - 1; i++) {
             newTail = newTail.next;
         }
         ListNode newHead = newTail.next;
         
         // 断开连接
         newTail.next = null;
         
         return newHead;
     }
     
     public static void main(String[] args) {
         // 构造链表的过程和测试代码...
         // 示例中的链表可以这样构造:
         ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
         int k = 2;
         
         Solution solution = new Solution();
         ListNode newHead = solution.rotateRight(head, k);
         
         // 输出结果
         while (newHead != null) {
             System.out.print(newHead.val + " ");
             newHead = newHead.next;
         }
     }

购买最多的lol英雄

英雄联盟游戏中新出n个英雄,用长度为n的教组 costs 表示每个英雄的定价,其中 costs[i]表示第i个英雄的点券价格。假如你一共有coins点券可以用于消费,且想要买尽可能多的英雄并日选择英雄按costs[i]给出顺序获取。给你价格数组 costs 和金币量 coins,请你计并返回用 coins 金币能够买到最多的英雄列表。 示例1

输入

[2,1,3,4,5],10

输出

[2,1,3,4]

我的解法

 public int[] solution(int[] costs, int coins) {
         HashMap<Integer,Integer> map  = new HashMap();
         int[] b = Arrays.copyOf(costs,costs.length);
         Arrays.sort(costs);
         int totalCost = 0;
         int count = 0;
         for (int cost : costs) {
             if (totalCost + cost <= coins) {
                 map.put(cost,map.getOrDefault(cost,0) + 1);
                 totalCost += cost;
                 count++;
             } else {
                 break;
             }
         }
         int[] res = new int[count];
         int j = 0;
         for (int k : b) {
             if (map.containsKey(k) && map.get(k) > 0){
                 res[j++] = k;
                 map.put(k,map.getOrDefault(k,0) - 1);
             }
 ​
         }
         return res;
     }

就是用hashmap存购买的英雄的价码和数目,但是只过了66.7%,之后才了解到使用背包去做

背包解法

 import java.util.Arrays;
 ​
 public class Main {
     public static void main(String[] args) {
         int[] costs = {2, 1, 3, 4, 5}; // 示例的价格数组
         int coins = 10; // 示例的金币数量
         int[] result = getMostHeroes(costs, coins);
         System.out.println(Arrays.toString(result));
     }
 ​
     public static int[] getMostHeroes(int[] costs, int coins) {
         int n = costs.length;
         int[][] dp = new int[n + 1][coins + 1];
 ​
         for (int i = 1; i <= n; i++) {
             for (int j = 1; j <= coins; j++) {
                 if (costs[i - 1] <= j) {
                     dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - costs[i - 1]] + 1);
                 } else {
                     dp[i][j] = dp[i - 1][j];
                 }
             }
         }
 ​
         int[] result = new int[dp[n][coins]];
         int i = n;
         int j = coins;
         int index = dp[n][coins] - 1;
 ​
         while (index >= 0 && i > 0 && j > 0) {
             if (dp[i][j] != dp[i - 1][j]) {
                 result[index] = costs[i - 1];
                 j -= costs[i - 1];
                 index--;
             }
             i--;
         }
 ​
         return result;
     }
 }