构建乘积数组

  给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。

思路:就是每一行的乘积中没有A[i]。

import java.util.ArrayList;
public class Solution {
public int[] multiply(int[] A) {
int len = A.length;
int[] B = new int[len];
for(int i=0;i<len;i++)
{
B[i]=1;
for(int j=i+1;j<len;j++)
B[i]*=A[j];
}
for(int i=0;i<len;i++)
{
for(int j=i-1;j>=0;j--)
B[i]*=A[j];
}
return B;
}
}

数组中重复数字

  在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

思路:有点迷,暴力求解,从前到后,相同的保存并返回True,检索后没有则false;

public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication)
{
for(int i=0;i<length-1;i++)
{
for(int j=i+1;j<length;j++)
{
if(numbers[i]==numbers[j])
{
duplication[0]=numbers[i];
return true;
}
}
}
return false;
}
}

顺时针打印矩阵

  输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] array) {
ArrayList<Integer> result = new ArrayList<Integer> ();
if(array.length==0) return result;
int n = array.length,m = array[0].length;
if(m==0) return result;
int layers = (Math.min(n,m)-1)/2+1;//这个是层数
for(int i=0;i<layers;i++){
for(int k = i;k<m-i;k++) result.add(array[i][k]);//左至右
for(int j=i+1;j<n-i;j++) result.add(array[j][m-i-1]);//右上至右下
for(int k=m-i-2;(k>=i)&&(n-i-1!=i);k--) result.add(array[n-i-1][k]);//右至左
for(int j=n-i-2;(j>i)&&(m-i-1!=i);j--) result.add(array[j][i]);//左下至左上
}
return result;
}
}

最新文章

  1. 规约模式Specification的学习
  2. iOS---TextView显示HTML文本
  3. bzoj 1503 splay
  4. js创建对象的6种方式
  5. mysql安装(Mac平台)
  6. LeetCode 笔记系列 19 Scramble String [合理使用递归]
  7. jquery技巧总结
  8. Django 1.6 最佳实践: 如何设置和使用 Log(转)
  9. ORACLE-树状数据结构获取各层级节点信息
  10. 修复ecshop商品重量BUG小数位增至五位
  11. Android中的事件分发机制总结
  12. C++程序设计教程学习(0)-引子
  13. Linux下的命令行上网
  14. CRM客户关系管理系统(十一)
  15. Nginx 图片服务器
  16. HashMap的resize方法中尾部遍历出现死循环问题 Tail Traversing (多线程)
  17. [BJOI2018]双人猜数游戏
  18. ReactiveX 学习笔记(15)使用 Rx.NET + Json.NET 调用 REST API
  19. 【Qt】QLabel实现的圆形图像
  20. Python Kivy 中文教程:安装(Windows)

热门文章

  1. Jquery选择器大全汇总
  2. when_did_you_born-瞟来的wp
  3. HihoCoder第三周与POJ2406:KMP算法总结
  4. 免杀PHP一句话一枚
  5. 99乘法表(for循环嵌套)
  6. 用ftp命令实现主机文件批量更新
  7. maven package和install
  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-file
  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-stop
  10. DevOps - 为什么