当前位置: 首页 > ai >正文

LintCode第484题-交换数组两个元素,第9题-Fizz Buzz 问题,第46题-主元素,第50题数组剔除元素后的乘积

第484题 · 交换数组两个元素

描述

给你一个数组和两个索引,交换下标为这两个索引的数字

样例 1:

输入:  [1, 2, 3, 4], index1 = 2, index2 = 3
输出:  交换后你的数组应该是[1, 2, 4, 3], 不需要返回任何值,只要就地对数组进行交换即可。
样例解释: 就地交换,不需要返回值。

样例 2:

输入:  [1, 2, 2, 2], index1 = 0, index2 = 3
输出: 交换后你的数组应该是[2, 2, 2, 1], 不需要返回任何值,只要就地对数组进行交换即可。	
样例解释: 就地交换,不需要返回值。

代码如下:

public class Solution {

    /**

     * @param a: An integer array

     * @param index1: the first index

     * @param index2: the second index

     * @return: nothing

     */

    public void swapIntegers(int[] a, int index1, int index2) {

        // write your code here

        int temp=a[index1];

        a[index1]=a[index2];

        a[index2]=temp;

    }

}

第9题

给定整数 n ,按照如下规则打印从 1 到 n 的每个数:

  • 如果这个数被3整除,打印fizz
  • 如果这个数被5整除,打印buzz
  • 如果这个数能同时被35整除,打印fizz buzz
  • 如果这个数既不能被 3 整除也不能被 5 整除,打印数字本身

    样例 1:

    输入:

    n = 15

    输出:

    ["1", "2", "fizz","4", "buzz", "fizz","7", "8", "fizz","buzz", "11", "fizz","13", "14", "fizz buzz"
    ]

代码如下: 

public class Solution {

    /**

     * @param n: An integer

     * @return: A list of strings.

     */

    public List<String> fizzBuzz(int n) {

        List<String> fizzList = new ArrayList<>();

        for (int i = 1; i <= n; i++) {

            if (i % 3 == 0 && i % 5 == 0) {

                fizzList.add("fizz buzz");

            } else if (i % 3 == 0) {

                fizzList.add("fizz");

            } else if (i % 5 == 0) {

                fizzList.add("buzz");

            } else {

                fizzList.add(String.valueOf(i)); // 把数字转成字符串

            }

        }

        return fizzList;

    }

}

第46题

描述

给定一个整型数组,找出主元素,它在数组中的出现次数大于数组元素个数的二分之一.

假设数组非空,且数组中总是存在主元素

样例 1:

输入:

数组 = [1, 1, 1, 1, 2, 2, 2]

输出:

1

解释:

数组中1的个数大于数组元素的二分之一。

样例 2:

输入:

数组 = [1, 1, 1, 2, 2, 2, 2]

输出:

2

解释:

数组中2的个数大于数组元素的二分之一。

public class Solution {

    /**

     * @param nums: a list of integers

     * @return: find a  majority number

     */

    public int majorityNumber(List<Integer> nums) {

        // write your code here

        int majorNumber=nums.get(0);

        int count=0;//统计当前元素出现的次数

        Map<Integer,Integer> numbermap=new HashMap<>();

        for(int i=1;i<nums.size();i++)

        {

            numbermap.put(nums.get(i),numbermap.getOrDefault(nums.get(i),0)+1);

        }

        //统计哪个map的值超过了列表的一半 哪个就是主元素

        for(Map.Entry<Integer,Integer> map:numbermap.entrySet())

        {

            if(map.getValue()>=(nums.size()/2+1))

            {

                majorNumber=map.getKey();

            }

        }

        return majorNumber;

    }

}

第50题

描述

给定一个整数数组A
定义B[i]=A[0]∗...∗A[i−1]∗A[i+1]∗...∗A[n−1], 计算B的时候请不要使用除法。请输出B

输入:

A = [1,2,3]

输出:

[6,3,2]

解释:

B[0] = A[1] * A[2] = 6; B[1] = A[0] * A[2] = 3; B[2] = A[0] * A[1] = 2

样例 2:

输入:

A = [2,4,6]

输出:

[24,12,8]

解释:

B[0] = A[1] * A[2] = 24; B[1] = A[0] * A[2] = 12; B[2] = A[0] * A[1] = 8

代码如下:

public class Solution {

    /**

     * @param nums: Given an integers array A

     * @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]

     */

    public List<Long> productExcludeItself(List<Integer> nums) {

        // write your code here

        // template   T[] array = list.toArray(new T[list.size()]);

        long[] longArray = new long[nums.size()];

        for (int i = 0; i < nums.size(); i++) {

        longArray[i] = nums.get(i);

            }

        List<Long> resultList=new ArrayList<>();

       

        for(int i=0;i<longArray.length;i++)//每轮是数组的起步位置

            {

                long currentNumber=1;

                for(int j=0;j<longArray.length;j++)//当前j跟随i动态变化 当前轮的起步位置

                {

                    if(i!=j)

                    {

                    currentNumber=currentNumber*longArray[j];

                    }

                }

            resultList.add(currentNumber);

            }

             return resultList;

    }

}

 

 

http://www.xdnf.cn/news/4176.html

相关文章:

  • 超表面加工流程
  • 从零开始了解数据采集(二十二)——塑胶制品行业趋势分析案例
  • (leetcode) 力扣100 6.三数之和 (双指针)
  • 卷积神经网络的简单实战项目
  • 大模型——GraphRAG基于知识图谱+大模型技术构建的AI知识库系统
  • 怎样用 esProc 实现多数据库表的数据合并运算
  • 深入理解 Linux 阻塞IO与Socket数据结构
  • 《脑机接口与AI:如何让瘫痪患者用“意念”实现创作?》
  • 在 PyTorch 中借助 GloVe 词嵌入完成情感分析
  • 【Vue】组件自定义事件 TodoList 自定义事件数据传输
  • Spring Boot3 实现定时任务 每10分钟执行一次,同时要解决分布式的问题 区分不同场景
  • 【coze】意图识别(售前售后问题、搜索引擎去广告)
  • 机器视觉框架源码——解读3(常用的资源和样式)
  • ShardingJdbc-水平分表
  • LaTex 模板 section 前小节符号去不掉 解决方法
  • MySQL数据库:全方位剖析与实战案例
  • Android Intent 页面跳转与数据回传示例(附完整源码)
  • Gradle -> Gradle的清除缓存指令
  • jenkins访问端口调整成80端口
  • 雅思阅读--句子结构
  • 信息论04:从信息熵到互信息——信息共享的数学度量
  • 【STM32单片机】#14 PWR电源控制
  • HarmonyOS基本的应用的配置
  • Android第六次面试总结之Java设计模式篇(一)
  • android-ndk开发(7): 从库文件反推ndk版本
  • error:0308010C:digital envelope routines::unsupported
  • MySQL 中 EXISTS (SELECT 1 FROM ...) 的用法详解
  • cookie/session的关系
  • 1、PLC控制面板 - /自动化与控制组件/plc-control-panel
  • getLocation:fail [geolocation:7]KEY错误