-
2018-03-24 11:33:01
https://leetcode.com/problems/nested-list-weight-sum-ii/description/
题目大意:与339. Nested List Weight Sum反过来,求嵌套数组的加权和,只不过权重是和深度反过来,最深一层权重为1.Example 1: Given the list [[1,1],2,[1,1]], return 8. (four 1's at depth 1, one 2 at depth 2) Example 2: Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17)
解题思路:一开始想用反过来回溯的方法,然并卵,最后只能放弃,还是339题的思路,正向递归,将每层的数记录在向量record中,第i层的数之和为record[i].最后再反向遍历record求加权和即可.
代码:
class Solution { public: int depthSumInverse(vector<NestedInteger>& nestedList) { vector<int> record; //记录表 for (auto nl : nestedList) { dfs(nl, 0, record); } int sum = 0; //反向遍历求加权 for (int i = record.size()-1, level = 1; i >= 0; i--, level++) { sum += level * record[i]; } return sum; } void dfs(NestedInteger &nestedList, int depth, vector<int>& record) { if (record.size() < depth+1) record.resize(depth+1); //record按需扩张 if (nestedList.isInteger()) { record[depth] += nestedList.getInteger(); //递归出口,记录该层数之和 } else { for (auto nl : nestedList.getList()) { dfs(nl, depth+1, record); } } } };
更多相关内容 -
weight和weightSum的区别
2017-08-22 14:23:08说起weight大家肯定都非常的清楚它的使用,可是weightSum恐怕有很多人不是很清楚,起码今天前我是不知道的,哈哈,来个笔记记录下二者的区别说起weight大家肯定都非常的清楚它的使用,可是weightSum恐怕有很多人不是很清楚,起码今天前我是不知道的,哈哈,来个笔记记录下二者的区别。
一:weight的使用
来个例子,简单明了。
布局文件:
从布局文件,可以看出两个button,设置的weight分别为1,既二者占用的比例一样,各占50%的宽度。weight的占比计算为:父布局的-其他的占用空间,剩下的按照所有weight的比例来分。<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="占50%" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="占50%" /> </LinearLayout>
布局效果图:
二:weightSum的使用
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="horizontal" android:weightSum="1"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="@string/hello_world" /> </LinearLayout>
效果图:
内部的计算原理是:
Button的宽度 = Button 的 width + Button的weight * 父布局(LinearLayout)的宽度 / weightSum
上面的例子,也就是 Button的宽度 = 0 + 0.5 * LinearLayout的宽度 / 1 = 0.5 * LinearLayout的宽度
也就是Button的宽度将占屏幕的一半。
-
你真的了解weight和weightSum吗?
2016-10-19 08:00:02带你理解Android中weight和weightSum的定义和工作原理。昨晚7:30,锤子科技在上海梅赛德斯-奔驰文化中心召开了2016年秋季新品发布会。为了给大家带来第一手的资料,我也是熬夜到11点钟,看完了整场发布会后才来写的今天的科技快讯。
在这次发布会上,锤子科技正式推出第三代手机:Smartisan M1 和 M1L,前者为低配小屏版,后者为高配大屏版。这次的锤子手机被粉丝们喻为最有诚意的一代锤子手机,硬件方面被老罗称之为当前最顶配,使用高通骁龙821处理器,配备了高达2300万像素的索尼摄像头,分辨率最高达到2K,最高6G内存以及64GB闪存,支持全网通。
另外在软件方面,锤子科技推出了Smartisan OS 3.0操作系统,其中着实包含了不少令人印象深刻的功能,如Big Bang语义识别、One Step一键分享等等,相信看了发布会直播的朋友们应该对这些功能还是感到颇为震撼的。除此之外,老罗这次竟然还在开源方面发表情怀,为了对谷歌的开源贡献表示感谢,准备将Smartisan OS 3.0操作系统中的One Step等功能也进行开源,引起了现场不小的轰动。
最后,锤子手机的价格方面还算是比较良心的,M1售价2499元,M1L最高配售价2999元。听说这次锤子为了保证货源充足,提前备好了50万台现货手机,但我写到这里时是23:53分,锤子的京东、天猫、还有官网等销售渠道均显示已售罄,看来这次锤子的新品还是相当受欢迎的。
本篇来自 胡萝卜小兔 的投稿,给大家分析了 weight 和 weightSum 定义以及使用方法,让你了解更加细节的方面。
胡萝卜小兔 的博客地址:
http://www.jianshu.com/users/17aed505615e
看到本文的标题,很多童鞋会一脸不屑的说,这有什么不了解的。不就是通过weight来给子布局按比例来分配空间嘛!好,这个答案也对也不对。 此时有人会疑惑了,为什么也对也不对? 我先来举两个最常见的例子:
layout_1
layout_2
这两个例子都能实现上图的效果。那么问题来了,这两个方式有什么区别吗? 眼尖的童鞋立马会说:当然有区别,一个 android:layout_width 是 0, 另一个是 wrap_content。 那么这两个有什么区别吗? 为什么实现的效果是一样的? 我还要问一句,真的是一样吗?
如果对这个问题有所疑惑,那么兔子哥将带领大家来深入了解一下weight和weightSum,并通过阅读本篇文章,做到知其然,并且知其所以然。
某位伟人曾经说过,要对某个知识点深入了解,最好的切入点就是仔细阅读知识点的定义。至于这位伟人是谁,什么?想不起来了?不用想了,就是这篇博文的作者,兔子哥大人也! ~~~好了,扯了这么多废话!我们还是先步入正题,先看看Google是怎么给 android:layout_weight 和 android:weightSum 来定义的:
Layout Weight
LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
大体意思就是,android:layout_weight 这个属性代表了一个“重要性”的值,这个值的大小代表了该控件能在屏幕中占据多大的空间。这个值越大,表明该控件可以在父控件中占据较多的“剩余”空间。默认的weight是0。
在这里,大家一定要注意“剩余”两个字!大家往往容易忽略这一点,导致出现了很多问题。举个例子:水平方向布局的父类有三个子类,父类总的宽度是100,子类的宽度分别是10,20,30。 那么 android:layout_weight 这个属性的目的,就是瓜分剩余的 100 - 10 - 20 - 30,也就是剩余的40的使用权。没错! 就是android:layout_weight 这个属性 仅仅决定 哪个子类能瓜分到更多的40的部分!
android:weightSum
Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children.
这个就很好理解了,weightSum 定义了 weight 总和的最大值。如果 android:weightSum 没有定义,那么默认值就是通过各个子类的 layout_weight 累加得到。
讲了这么多,举个例子来说明一下。
效果如下图:
内部的计算原理是:
Button的宽度 = Button 的 width + Button的weight * 父布局(LinearLayout)的宽度 / weightSum。
上面的例子,也就是 Button的宽度 = 0 + 0.5 * LinearLayout的宽度 / 1 = 0.5 * LinearLayout的宽度。也就是Button的宽度将占屏幕的一半。
结合上面的知识再看一下 Google的例子:
https://developer.android.com/guide/topics/ui/layout/linear.html#Weight
可能体会的更加深刻。
首先看效果图:
如果让大家自己来实现这个效果的话,相信很多童鞋会采用相对布局,将send按钮位于屏幕最下方,然后上面的 三个EditText 从上往下依次排列。但是 message 的 EditText 要铺满剩余的空间,这个怎么实现? 有的童鞋会不屑的说了,这有什么难的,把 message 的高度设置 match_parent 并且位于 subject和send之间 即可。代码如下:
但是如果采用本文讲解的 weight 可能会更加的简单。这个布局关键的一点是 message 要铺满 subject与send之间 的区域。问题是如何实现铺满呢? 对了! 本文讲解的 weight 的核心就是瓜分父布局剩余的空间。全部瓜分不就是铺满嘛! 那么代码也就出来了:
着重看一下 message 的布局:
没错,采用了极为巧妙的方法。其他的子布局都不设置 layout_weight, 也就是默认是0 。 那么 message 将获取全部的剩余空间,实现了铺满的效果。
相信阅读到这里的童鞋们,对文章开头的问题应该已经知道答案了。如果还有童鞋不知道答案,可以试着将文章开头的两个Button的内容改一下,一个叫AAAAAAAAA,一个叫B。您再看一下效果,可能就明白了。至于那个也对也不对的问题,“不对”的原因是他们要瓜分的是 剩余 空间。本文止。
如果你有好的技术文章想和大家分享,欢迎向我的公众号投稿,投稿具体细节请在公众号主页点击“投稿”菜单查看。
欢迎长按下图 -> 识别图中二维码或者扫一扫关注我的公众号:
-
weightSum使用的几个效果
2015-05-09 22:40:10android:weightSum = "1" > < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_weight = "0.3" android:background = "#ff00ff00" >...这几种效果只是做一个记录,忘记了就不用再去一个个试了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.3" android:background="#ff00ff00" > </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.7" android:background="#ff0000ff" > </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.3" android:background="#ff00ff00" > </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" //把match_parent改成0dp android:layout_weight="0.7" android:background="#ff0000ff" > </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.3" android:background="#ff00ff00" > </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.7" android:background="#ff0000ff" > </LinearLayout> </LinearLayout>
-
什么是weight和weightSum
2016-10-24 15:48:331.android:layout_weight 大体意思就是,android:layout_weight 这个属性代表了一个“重要性”的值,这个值的大小代表了该控件能在屏幕中占据多大的空间。这个值越大,表明该控件可以在父控件中占据较多的“剩余”... -
Android布局中的layout_weight和weightSum属性的详解及使用
2021-10-11 18:00:55由于Android设备的尺寸大小不一,种类繁多,当我们在开发应用的时候就要考虑屏幕的适配型了,...在布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置它所占据屏幕的... -
weightSum与weight
2019-02-14 11:22:19如果 android:weightSum 没有定义,那么默认值就是通过各个子类的 layout_weight 累加得到。 &lt;?xml version="1.0" encoding="utf-8"?& -
安卓线性布局的weightSum属性用法
2016-10-09 15:42:03android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2"> -
android:weightSum="2
2016-09-05 21:18:53在xml文件中设置属性android:weightSum="2",然后在Button中设置属性android:layout_wetght=“1”,此时,Button将会占到整个布局的1/2,也就是布局宽度的一半了。我们想要的效果就达到了,而且不会因为手机设备不同... -
LeetCode 364. Nested List Weight Sum II
2016-06-24 00:16:31原题网址:https://leetcode.com/problems/nested-list-weight-sum-ii/ Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either ... -
Android小知识:使用android:weightSum
2015-02-02 10:26:44android:weightSum="1" android:gravity="center_horizontal" android:orientation="horizontal" > android:layout_width="0dp" android:layout_weight="0.5" android:layout_height="wrap_content" ... -
通过Android源码分析LinearLayout的layout_weight与weightsum对布局大小的影响
2016-12-05 00:54:27特别是当我们要实现几个控件平均分割一定的区域的时候,一般都会通过LinearLayout的layout_weight和weightsum组合实现。要理解layout_weight和weightsum这2个属性对LinearLayout布局大小的影响,最好还是结合源码... -
Android系列之一:android:layout_weight和android:weightSum总结
2017-05-06 13:38:12android:layout_weight是LinearLayout布局的一个属性,它定义了每个控件占据屏幕剩余空间的权重。以划分屏幕横向空间为例:每个控件利用android:layout_width来定义自身的宽度,Android首先将所有控件定义的宽度相加... -
Android_按比例布局layout_weight和weightSum
2015-07-13 10:46:58一个Button占据整个屏幕的一半宽度,开发文档中对layout_weight属性的...一个典型的案例是:通过指定子视图的layout_weight属性为0.5,并设置LinearLayout的weightSum属性为1.0,实现子视图占据可用宽度的50。” XML -
Layout中weightSum和layout_weight的详解
2016-05-27 17:20:53首先声明:1).weightSum用于父容器 2).layout_weight用于子容器 那么就可能出现一下几种情况: 1).当父容器weightSum=各子容器weight的总和时,或者父容器没有声明weightSum,则效果为:各子容器按照weight比例... -
50个Android开发技巧(1,使用android:layout_weight和android:weightSum属性的使用)
2014-11-04 22:25:26android:weightSum 属 性 和 LinearLayout 的 子 视 图 的 android:layout_weight 属 性来解决这个问题。 这听起来似乎很简单, 不过 我经常在面试中问到这个问题, 很少有面试者知道最佳答案。 ... -
LinearLayout使用weightSum按比例布局
2016-09-08 15:26:23父View需要设置三项,orientation,weightsum,gravity子view需要设置一项 layout_weight注意:如果父View的orientation是h,子view的width需要设置为0,另一项设置为matchparent如果父View的orientation是v,子view的... -
LinearLayout中借助:weightSum和layout_weight布局
2015-06-02 19:12:00如图说是,为一个Activity的自定义标题,现在需要借助Linearlayout中的weightSum和layout_weight布局来实现: 在开发中使用LinearLayout 进行View的布局时,要实现这样的布局有很多种布局方式,程序源码是使用硬... -
日积月累:weightSum和layout_weight属性合用
2014-07-13 15:15:36讲解一:weightSum和layout_weight属性合用 android:weightSum属性:定义weight总和的最大值。如果为指定该值,所有子视图的layout_weight属性的累加值作为总和的最大值。例如,通过指定子视图的layout_weight属性... -
在LinearLayout中,巧妙使用weightSum和layout_weight
2014-09-26 12:18:05在LinearLayout中,巧妙使用weightSum和layout_weight -
LeetCode 339. Nested List Weight Sum
2016-05-17 12:04:00Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lis -
[LeetCode] 339 Nested List Weight Sum 嵌套链表权重和
2017-01-04 08:59:54[LeetCode] Nested List Weight Sum 嵌套链表权重和Given a nested list of integers, return the sum of all integers in the list weighted by their depth.Each element is either an integer, or a list – ... -
android:layout_weight android:weightSum
2013-06-05 11:49:50引用:... android中layout_weight的理解 SDK中的解释 Indicates how much of the extra space in the LinearLayout will be allocated to -
weightSum属性和layout_weight属性的用法和详解
2016-04-12 10:36:30在xml中有个属性我们很少用到,但它很有用,就是android:weightSum属性,下面我们讲解他的属性和用法 将按钮居中显示,并且占据其父视图宽度的一半,应该怎么做呢,肯定读者会有很多种不同的做法 如何结合...