LeetCode 1418. 点菜展示表 / NC103 反转字符串 / NC33 合并有序链表 / NC61两数之和

1418. 点菜展示表

2021.7.6 每日一题

题目描述
给你一个数组 orders,表示客户在餐厅中完成的订单,确切地说, orders[i]=[customerNamei,tableNumberi,foodItemi] ,其中 customerNamei 是客户的姓名,tableNumberi 是客户所在餐桌的桌号,而 foodItemi 是客户点的餐品名称。

请你返回该餐厅的 点菜展示表 。在这张表中,表中第一行为标题,其第一列为餐桌桌号 “Table” ,后面每一列都是按字母顺序排列的餐品名称。接下来每一行中的项则表示每张餐桌订购的相应餐品数量,第一列应当填对应的桌号,后面依次填写下单的餐品数量。

注意:客户姓名不是点菜展示表的一部分。此外,表中的数据行应该按餐桌桌号升序排列。

 

示例 1:

输入:orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
输出:[["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]] 
解释:
点菜展示表如下所示:
Table,Beef Burrito,Ceviche,Fried Chicken,Water
3    ,0           ,2      ,1            ,0
5    ,0           ,1      ,0            ,1
10   ,1           ,0      ,0            ,0
对于餐桌 3:David 点了 "Ceviche" 和 "Fried Chicken",而 Rous 点了 "Ceviche"
而餐桌 5:Carla 点了 "Water" 和 "Ceviche"
餐桌 10:Corina 点了 "Beef Burrito" 
示例 2:

输入:orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
输出:[["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]] 
解释:
对于餐桌 1:Adam 和 Brianna 都点了 "Canadian Waffles"
而餐桌 12:James, Ratesh 和 Amadeus 都点了 "Fried Chicken"
示例 3:

输入:orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
输出:[["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/display-table-of-food-orders-in-a-restaurant
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

这两天竟出这种题,就是用哈希表存储,然后排序输出的问题,这个有点烦,因为考虑的东西比较多,所以写起来比较慢,不过还是一遍过了
其实关键点就是,两个需要排序的集合,一个是食物名称,一个是桌号,需要先存储在哈希表中去重,然后再转成list排序
最后,用一个嵌套的哈希表取出每个餐品每桌对应的数量。

class Solution {
    public List<List<String>> displayTable(List<List<String>> orders) {
        //不算难,就是烦
        //就相当于在数据库中建一个表
        //map中存map,外层map键值是 餐品名称,值是map。里层map键是桌号,值是份数
        //然后对键排序
        //用一个list存放桌号,并排序
        Map<String, Map<Integer, Integer>> map = new HashMap<>();
        //存放桌号
        Set<Integer> set = new HashSet<>();

        for(List<String> list : orders){
            int table = Integer.valueOf(list.get(1));
            String food = list.get(2);
            set.add(table);
            Map<Integer, Integer> temp = map.getOrDefault(food, new HashMap<>());
            temp.put(table, temp.getOrDefault(table, 0) + 1);
            map.put(food, temp);
        }
        //对食物和桌号排序
        List<String> foods = new ArrayList<>(map.keySet());
        List<Integer> tables = new ArrayList<>(set);
        Collections.sort(foods);
        Collections.sort(tables);
        //System.out.println(foods);
        //System.out.println(tables);
        List<List<String>> res = new ArrayList<>();
        //创建第一行
        List<String> title = new ArrayList<>(foods);
        title.add(0, "Table");
        res.add(title);
        int l = tables.size();
        int n = foods.size();
        //创建其余行
        for(int i = 0; i < l; i++){
            //获得当前桌号
            int table = tables.get(i);
            //加入集合
            List<String> content = new ArrayList<>();
            content.add(String.valueOf(table));
            for(int j = 0; j < n; j++){
                //当前食物
                String food = foods.get(j);
                //当前食物对应的购买情况
                Map<Integer, Integer> temp = map.get(food);
                //如果当前桌购买了
                if(temp.containsKey(table))
                    content.add(String.valueOf(temp.get(table)));
                else
                    content.add("0");
            }
            res.add(content);
        }
        return res;
    }
}

NC103 反转字符串

题目描述
写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)
思路

调用方法

public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        return new StringBuffer(str).reverse().toString();
    }
}
import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        char[] cc = str.toCharArray();
        int l = cc.length;
        for(int i = 0; i < l / 2; i++){
            char temp = cc[i];
            cc[i] = cc[l - 1 - i];
            cc[l - 1 - i] = temp;
        }
        return new String(cc);
    }
}

NC33 合并有序链表

题目描述
将两个有序的链表合并为一个新链表,要求新的链表是通过拼接两个链表的节点来生成的,且合并后新链表依然有序。

示例1
输入:
{1},{2}

返回值:
{1,2}

示例2
输入:
{2},{1}

返回值:
{1,2}
思路
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param l1 ListNode类 
     * @param l2 ListNode类 
     * @return ListNode类
     */
    public ListNode mergeTwoLists (ListNode l1, ListNode l2) {
        // write code here
        ListNode id1 = l1;
        ListNode id2 = l2;
        ListNode dummy = new ListNode(-1);
        ListNode id = dummy;
        while(id1 != null && id2 != null){
            if(id1.val < id2.val){
                id.next = id1;
                id1 = id1.next;
            }else{
                id.next = id2;
                id2 = id2.next;
            }
            id = id.next;
        }
        id.next = id1 == null ? id2 : id1;
        return dummy.next;
    }
}

NC75数组中只出现一次的数字

题目描述
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
思路

哈希表方法

//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
import java.util.*;
public class Solution {
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
        Map<Integer, Integer> map = new HashMap<>();
        int n = array.length;
        for(int i = 0; i < n; i++){
            map.put(array[i], map.getOrDefault(array[i], 0) + 1);
        }
        int[] res = new int[2];
        int id = 0;
        for(int num : array){
            if(map.get(num) == 1){
                res[id++] = num;
            }
        }
        num1[0] = res[0];
        num2[0] = res[1];
    }
}

异或方法

//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
import java.util.*;
public class Solution {
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
        int xor = 0;
        int l = array.length;
        
        for(int i = 0; i < l; i++){
            xor ^= array[i];
        }
        //取最低位不同的
        int lowbit = xor & (-xor);
        num1[0] = 0;
        num2[0] = 0;
        for(int i = 0; i < l; i++){
        	//注意这里不能写成等于1,只能写等于0或者不等于0
            if((array[i] & lowbit) == 0)
                num1[0] ^= array[i];
            else
                num2[0] ^= array[i];
        }
    }
}

NC61两数之和

题目描述
给出一个整数数组,请在数组中找出两个加起来等于目标值的数,
你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index2.。注意:下标是从1开始的
假设给出的数组中只存在唯一解
例如:
给出的数组为 {20, 70, 110, 150},目标值为90
输出 index1=1, index2=2
思路

力扣中的hello world,遥想做这个题的时候已经差不多一年了,但是这个题所用的方法很多题都能用到

import java.util.*;


public class Solution {
    /**
     * 
     * @param numbers int整型一维数组 
     * @param target int整型 
     * @return int整型一维数组
     */
    public int[] twoSum (int[] numbers, int target) {
        // write code here
        int l = numbers.length;
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < l; i++){
            int num = numbers[i];
            if(map.containsKey(target - num))
                return new int[]{map.get(target - num) + 1, i + 1};
            map.put(num, i);
        }
        return new int[]{};
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值