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
。 - 如果这个数能同时被
3
和5
整除,打印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;
}
}