【leetcode】solution in java——Easy4
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6415011.html
16:Invert Binary Tree
此题:以根为对称轴,反转二叉树。
思路:看到二叉树,我们第一时间要想到处理二叉树的常用方法——BFS、DFS,更常用的是DFS。此题我们先用BFS来思考:BFS是逐层每个结点处理,即通过交换每层结点的位置来达到整体交换。我们可以用队列+栈来实现,用队列处理实现按层遍历,用栈实现当前层结点位置倒转,然后从根结点开始,逐层重新建树。无疑,这很麻烦。我们转而思考DFS怎么做:从根结点开始,交换左右子树,然后递归到下一层,交换左右子树......直到倒数第二层,交换左右子树,从而实现整体互换。
牢记DFS三步骤:递归边界返回值、当前层值由递归得出、递归的返回值是什么
public TreeNode invertTree(TreeNode root) {
//递归边界的返回值(dfs最容易出错的地方就在于边界的判断。忽略了边界为空的话很容易就报NullPointerException异常了)
if (root==null) {return null;}
//当前层处理:值由递归得到
TreeNode left=root.left;
TreeNode right=root.right;
root.left=invertTree(right);
root.right=invertTree(left);
//递归的返回值
return root;
}
17:Construct the Rectangle
1. The area of the rectangular web page you designed must equal to the given target area.
2. The width W should not be larger than the length L, which means L >= W.
3. The difference between length L and width W should be as small as possible.
Note:
- The given area won't exceed 10,000,000 and is a positive integer
此题:给出矩形面积area,求组成该面积的矩形的长和宽并且长宽尽可能接近。
思路:此题提示面积上限到了10000000,说明如果想暴力尝试求出该面积的所有长宽方式再取最接近者可能会超时。所以我们需要从最快的方法去找:观察得,一个数拆分成两数相乘形式并且因数接近的话,刚好开根号得到整数是最接近的,相差为0.比如area=4,则长=宽=2。但对于二次方根不为整数的情况,比如6,则长=3,宽=2,而 根号6=2.44。可以发现,长宽最接近并且相乘得面积的答案,长位于面积的方根的右边,宽位于方根的左边,并且是众多答案中最靠近方根的。由此,我们可得到解题思路:首先对面积开方根并向下取整,然后方根相乘,如果是面积则说明矩形是正方形,长宽为方根;如果方根相乘不等于面积,说明矩形不是正方形,由于我们向下取整求的方根,说明此时的方根在真实方根的左边,令width=方根,width逐步减小1,用area%width==0这个条件求出最接近的方根的并且能被面积整除的宽,然后area/width就是最接近方根的长了。
public int[] constructRectangle(int area) {
int[] res = null;
//向下取整求方根
int width=(int) Math.sqrt(area);
//方根相乘为面积则长宽为方根
if (width*width==area) {
return res=new int[]{width,width};
}
//否则,从方根开始减小,找到最接近方根的能被面积整除的宽
while(area%width!=0){
--width;
}
//由宽得长
int length=area/width;
return res=new int[]{length,width};
}
18:Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".All the scores of athletes are guaranteed to be unique.
此题:给出N个运动员的比赛成绩,找出这N个运动员的相对名词,并且前三的用"Gold Medal", "Silver Medal" and "Bronze Medal"表示,其余名次则直接输出名次即可。
思路:映射类题目——根据比分映射为名次。第一时间想到map。
public String[] findRelativeRanks(int[] nums) {
//在对原数组排序前要先把内容赋值到另一个数组中保存。这里不能用old=nums:Java中数组是引用类对象,相当于C++中的指针,这样其实是把nums的指针给了old,没有起到内容复制保存起来的效果
int[] old=Arrays.copyOf(nums, nums.length);
Arrays.sort(nums);
Map<Integer, String> map=new HashMap<Integer, String>();
for (int i = 0;i<=nums.length-1;++i) {
if(i==nums.length-1){
map.put(nums[i], "Gold Medal");
}else if(i==nums.length-2){
map.put(nums[nums.length-2], "Silver Medal");
}else if(i==nums.length-3){
map.put(nums[nums.length-3], "Bronze Medal");
}else{
map.put(nums[nums.length-4-i], ""+(4+i));
}
}
String[] res=new String[nums.length];
//根据前面保存起来的旧内容,依次赋予排名
for(int i=0;i<nums.length;++i){
res[i]=map.get(old[i]);
}
return res;
}
19:Move Zeroes
Given an array nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
You must do this in-place without making a copy of the array.
此题:给出一个数组,要求把所有0元素移到后面,非0的保持原来的相对位置。要求只能在原数组用元素替换不能用新数组。
思路:此题如果按冒泡排序简化版来做的话,需要不断遍历找到0然后交换到末尾,时间复杂度为O(N^2)。注意到这里只要求非0的保持原来位置,后面的全是0。我们采取“用覆盖不用交换”的策略:由找0移动到后面转为找非0写入(覆盖)到前面index-1处。遍历一次数组后所有非0的都写入到0~index-1坐标下了,然后从index~nums.length-1全部赋值0即可。
public void moveZeroes(int[] nums) {
int index=0;
//找非0元素写到前面(用覆盖不用移动)
for(int i=0;i<nums.length;++i){
if(nums[i]!=0){
nums[index]=nums[i];
++index;
}
}
//后面的全是0
for(int i=index;i<nums.length;++i){
nums[i]=0;
}
}
20:Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
此题:two sum题的升级版,经典的两指针迫近问题。
思路:用两个下标分别指向有序数组的两端,求数组最大值和最小值的和,然后与target比较,大了则右边坐标左移;小了则左边左边右移动;直到和等于target。由于题目说绝对有解,那么我们就可以一步步迫近最终解。
一开始,min+max>target,所以需要减小,而min已经是最小的,所以只能减小max,右下标左移。
...
直到,min+max<target,此时需要增大,由于上一次min+max>target,所以可知如果此时用max右移增大的话结果会大于target,所以只能用左下标右移来增大。
...
直到min+max=target,此时的左边就是所求了。
public int[] twoSum(int[] numbers, int target) {
if(numbers==null || numbers.length < 1) return null;
int[] res=null;
int min=0;
int max=numbers.length-1;
while(min<max){
if(numbers[min]+numbers[max]>target){
--max;
}else if(numbers[min]+numbers[max]<target){
++min;
}else{
return res=new int[]{min+1,max+1};
}
}
return res;
}
【leetcode】solution in java——Easy4的更多相关文章
- 【leetcode】solution in java——Easy3
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6412505.html 心得:看到一道题,优先往栈,队列,map,list这些工具的使用上面想.不要来去都是暴搜 ...
- 【leetcode】solution in java——Easy2
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...
- 【leetcode】solution in java——Easy1
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6409067.html 1:Hamming distance The Hamming distance betw ...
- 【leetcode】solution in java——Easy5
转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...
- 【Leetcode】Reorder List JAVA
一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...
- 【Leetcode】Sort List JAVA实现
Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
随机推荐
- 以绑定的方式来启动service
先说下原理,之前我们的启动service就是用startService来启动的,这是显式启动.启动后我们无法得到service中的数据,也无法知道它执行的状态,如果我们要启动它的activity和它建 ...
- 深入理解多线程(五)—— Java虚拟机的锁优化技术
本文是<深入理解多线程>的第五篇文章,前面几篇文章中我们从synchronized的实现原理开始,一直介绍到了Monitor的实现原理. 前情提要 通过前面几篇文章,我们已经知道: 1.同 ...
- Linux 批量查找替换方法(VIM和sed)
版权声明:欢迎与我交流讨论,若要转载请注明出处~ https://blog.csdn.net/sinat_36053757/article/details/70946263 1.VIM命令 当前行进行 ...
- [转]你如何面对—LNMP高并发时502
From : http://www.topthink.com/topic/5683.html 之前php-fpm配置: 单个php-fpm实例,使用socket方式,内存8G 静态方式,启动php-f ...
- window.name实现的跨域数据传输 JavaScript跨域总结与解决办法
原文地址: http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html#m4 什么是跨域 1.document.domain+ifr ...
- 基于Otsu算法的图像自适应阈值切割
在图像处理实践中,将灰度图转化为二值图是非经常见的一种预处理手段. 在Matlab中,能够使用函数BW = im2bw(I, level)来将一幅灰度图 I.转化为二值图. 当中.參数level是一个 ...
- layer-list shape drawable 层叠背景 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- CentOS 7安装Hadoop 3.0.0
最近在学习大数据,需要安装Hadoop,自己弄了好久,最后终于弄好了.网上也有很多文章关于安装Hadoop的,但总会遇到一些问题,所以把在CentOS 7安装Hadoop 3.0.0的整个过程记录下来 ...
- 引用外部jquery.js
使用 Google 的 CDN <head> <script type="text/javascript" src="http://ajax.googl ...
- 使用矩阵分解(SVD)实现推荐系统
http://ling0322.info/2013/05/07/recommander-system.html 这个学期Web智能与社会计算的大作业就是完成一个推荐系统参加百度电影推荐算法大赛,成绩按 ...