【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)法. ...
随机推荐
- maven打包时跳过单元测试
运行mvn install时跳过Test <project> [...] <build> <plugins> <plugin> <groupId& ...
- 实用ExtJS教程100例-003:进度条对话框Ext.MessageBox.progress
在上一篇内容中我们介绍了三种常用的MessageBox提示框,在这篇文章中,我们将演示如何在对话框中使用进度条. 进度条对话框 我们可以使用下面的代码来在MessageBox中显示一个进度条: Ext ...
- JAVA开发中文乱码的几个解决方案
一:html乱码或者引入的JS乱码 1:第一步,text file encoding 首先确保文件的保存格式要UTF-8,如在eclipse中,要在文件上点属性,确保这里选择UTF-8 注意,在ecl ...
- 对CAP定理的理解
CAP定理的常规解释是任何分布式系统只能在一致性(Consitency),可用性(Availability)和分区容忍性(Partition Tolerance)中三选二.这个解释很让人费解,笔者在看 ...
- maskrcnn_benchmark代码分析(2)
maskrcnn_benchmark训练过程 ->训练命令: python tools/train_net.py --config-file "configs/e2e_mask_rcn ...
- 鼠标悬浮在img上让图片变大
样式: <style type="text/css"> img:hover{ transform:scale(1.02,1.02)}</style& ...
- Openstack中为虚拟机使用CDROM光驱设备
在Libvirt里处理 在nova里处理 实际效果 怎么卸载 在Libvirt里处理 尝试了下面有几种方法,为虚拟机载入光盘文件: 1.使用ide方式挂载: virsh attach-disk {in ...
- 【Spark】Spark Streaming 动态更新filter关注的内容
Spark Streaming 动态更新filter关注的内容 spark streaming new thread on driver_百度搜索 (1 封私信)Spark Streaming 动态更 ...
- 【使用JSOUP实现网络爬虫】修改数据-设置属性的值
问题 在你解析一个Document之后可能想修改其中的某些属性值,然后再保存到磁盘或都输出到前台页面. 方法 可以使用属性设置方法 Element.attr(String key, String va ...
- HTML/CSS-返回到上一页
<a class="back_btn" href="javascript:window.history.go(-1)">< 返回</a& ...