✅ 191. 位1的个数

描述

编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。

 

示例 1:

输入:00000000000000000000000000001011
输出:3
解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。 来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-1-bits
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解答

cpp

class Solution {
public:
int hammingWeight(uint32_t n) {
// 10 based to 2 based
int ans = 0;
while(n) {
ans += n&1;
n>>=1;
}
return ans;
}
};
/*执行用时 :
4 ms
, 在所有 C++ 提交中击败了
76.64%
的用户
内存消耗 :
8.3 MB
, 在所有 C++ 提交中击败了
5.95%
的用户*/ or: class Solution {
public:
int hammingWeight(uint32_t n) { return bitset<32>(n).count();
}
};
/*执行用时 :
8 ms
, 在所有 C++ 提交中击败了
30.26%
的用户
内存消耗 :
8.2 MB
, 在所有 C++ 提交中击败了
52.87%
的用户*/

py

class Solution:
def hammingWeight(self, n: int) -> int:
return str(bin(n)).count('1');
'''
执行用时 :
32 ms
, 在所有 Python3 提交中击败了
75.73%
的用户
内存消耗 :
13.4 MB
, 在所有 Python3 提交中击败了
22.90%
的用户
'''

✅ 107. 二叉树的层次遍历 II

描述


给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:
给定二叉树 [3,9,20,null,null,15,7], 3
/ \
9 20
/ \
15 7
返回其自底向上的层次遍历为: [
[15,7],
[9,20],
[3]
]

解答

传统解法: 使用层序遍历,中间加上 stack

问题是,我不知道如何让 截止 一个 层。这个问题就描述不清楚。那你去看看py的实现

while(!q.empty()){
node = q.poll();
stack.push(node);
if(node.left): q.enque(node.left);
if(node.right): q.enque(node.right);
}
int ret[stack.size()];
while(!stack.empty()){
ret.append(stack.pop());
}

c todo 观赏 0222

typedef struct queue
{
struct TreeNode* data;
int front;
int rear;
}Myqueue; void initqueue(Myqueue* queue, int maxsize)
{
queue->data = (struct TreeNode*)malloc(sizeof(struct TreeNode) * maxsize);
queue->front = 0;
queue->rear = 0;
} void enqueue(Myqueue* obj, struct TreeNode* root)
{
obj->data[obj->rear++] = root[0];
} struct TreeNode* dequeue(Myqueue* obj, struct TreeNode* root)
{
return &obj->data[obj->front++]; } int isqueueempty(Myqueue* obj)
{
return obj->front == obj->rear ? 1 : 0;
} int height(struct TreeNode* root) //求深度
{
int HL, HR, MaxH;
if(root)
{
HL = height(root->left);
HR = height(root->right);
MaxH = (HL > HR) ? HL : HR;
return (MaxH + 1);
}
else return 0;
} int** levelOrderBottom(struct TreeNode* root, int** columnSizes, int* returnSize)
{
int **ret = (int**)malloc(sizeof(int*) * 1000);
int count = 0, i = 0, depth;
depth = height(root);
*returnSize = depth;
Myqueue* queue = (Myqueue*)malloc(sizeof(Myqueue));
columnSizes[0] = (int*)malloc(sizeof(int) * 1000);
initqueue(queue, 10000);
if(!root) return NULL;
enqueue(queue, root);
while(!isqueueempty(queue))
{
depth--;
count = queue->rear - queue->front;
ret[depth] = (int*)malloc(sizeof(int) * count);
columnSizes[0][depth] = count;
while(i != count)
{
root = dequeue(queue, root);
ret[depth][i++] = root->val;
if(root->left) enqueue(queue, root->left);
if(root->right) enqueue(queue, root->right);
}
i = 0;
}
return ret;
}
最早
最新
miaoxingren喵星人
1 年前
@福 膜拜大佬,,看了那么多其他语言写的,,我只佩服大佬写的c语言 程序 大佬你的c语言功底也太好了吧 fu-Z2aABL1fkT福
1 年前
@喵星人 惭愧,因为我只会C语言【捂脸】, 来日方长,加油! get_money走过的路、沉淀了回忆
10 个月前
@福 大佬,有空做个注释咯! 感激不尽 xiu-nya修~Nya
10 个月前
@福 果然这种题目,用C写起来,代码量和思维都,令人咂舌,佩服佩服,太惊人了 poison-9poison
6 个月前
@福 用C写是真的牛皮 lao-ju-wei-lai-ke-ji-gong-si老菊未来科技公司
6 个月前
@福 用C语言做最大的问题就是不知道这些函数参数都是干嘛的ORZ

py

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
if root == None: return [] # must add this line
q = []
res = []
q.append(root)
while len(q):
tmp_q = []
small_list = []
for i in q:
if i:
small_list.append(i.val)
if i.left: tmp_q.append(i.left)
if i.right: tmp_q.append(i.right)
q = tmp_q
res.insert(0, small_list)
return res
'''
执行用时 :
64 ms
, 在所有 Python3 提交中击败了
6.18%
的用户
内存消耗 :
13.7 MB
, 在所有 Python3 提交中击败了
35.13%
的用户
'''

✅ 806. 写字符串需要的行数

描述


我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行。我们给定了一个数组 widths ,这个数组 widths[0] 代表 'a' 需要的单位, widths[1] 代表 'b' 需要的单位,..., widths[25] 代表 'z' 需要的单位。 现在回答两个问题:至少多少行能放下S,以及最后一行使用的宽度是多少个单位?将你的答案作为长度为2的整数列表返回。 示例 1:
输入:
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "abcdefghijklmnopqrstuvwxyz"
输出: [3, 60]
解释:
所有的字符拥有相同的占用单位10。所以书写所有的26个字母,
我们需要2个整行和占用60个单位的一行。
示例 2:
输入:
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "bbbcccdddaaa"
输出: [2, 4]
解释:
除去字母'a'所有的字符都是相同的单位10,并且字符串 "bbbcccdddaa" 将会覆盖 9 * 10 + 2 * 4 = 98 个单位.
最后一个字母 'a' 将会被写到第二行,因为第一行只剩下2个单位了。
所以,这个答案是2行,第二行有4个单位宽度。 注: 字符串 S 的长度在 [1, 1000] 的范围。
S 只包含小写字母。
widths 是长度为 26的数组。
widths[i] 值的范围在 [2, 10]。

解答

cpp


js

/**
* @param {number[]} widths
* @param {string} S
* @return {number[]}
*/
var numberOfLines = function(widths, S) {
//妙啊
//不用求余求商,那样扣边界烦死,逐项相加,累加一旦大于100,行数加1,那么要到下一行的一定是当前这个使得累加和大于100 的那个元素,累加和更新为当前元素的单位,开始新的一行,直到最后。。。 72ms, 34.7MB.
let count = 1;
let sum = 0;
for (let i = 0; i < S.length; i++) {
let code = S.charCodeAt(i);
sum += widths[code - 97];
if (sum > 100) {
count += 1;
sum = widths[code - 97];
}
}
return [count, sum];
};
/*执行用时 :
64 ms
, 在所有 JavaScript 提交中击败了
81.54%
的用户
内存消耗 :
34.8 MB
, 在所有 JavaScript 提交中击败了
75.56%
的用户 */

lc 0222的更多相关文章

  1. 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。

    laviewpbt  2014.8.4 编辑 Email:laviewpbt@sina.com   QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...

  2. “LC.exe”错误

    错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...

  3. 解决VS下“LC.exe已退出,代码为-1”问题

    今天使用VS2015开发一个Winform程序,手一抖拖错了一个第三方控件,然后将其去掉并删除相关的引用,结果导致了LC.exe错误:"Lc.exe已退出,代码为-1 ". 经过上 ...

  4. 解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析

    许可证编译器 (Lc.exe) 的作用是读取包含授权信息的文本文件,并产生一个可作为资源嵌入到公用语言运行库可执行文件中的 .licenses 文件. 在使用第三方类库时,经常会看到它自带的演示程序中 ...

  5. Lc.exe已退出,代码为-1

    编译项目,出现提示"Lc.exe已退出,代码为-1" .   解决办法: 意思就是把licenses.licx这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...

  6. "LC.exe" exited with code -1 错误

    当打开一个VS程序时出现"LC.exe" exited with code -1错误,解决方法是: 删除licenses.licx文件即可

  7. LC.exe exited with code -1

    昨天从win8.1升级到win10之后, 一切还算顺利, 就是升级时间比较长. 但是快下班的时候 遇到一个问题, 是之前在win8.1上没遇到的, 首先代码win8.1 vs2013 上跑的时候一切正 ...

  8. vs2012编译出错“LC.exe”已退出解决方法

    “LC.exe”已退出,代码为 -1. 解决方法: 将项目Properties下的licenses.licx文件删除,重新编译即可.

  9. TT付款方式、前TT和后TT、LC信用证+TT付款方式

    TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款. .T/T属于商业信用,也就是说付款的最终决定权在于客户.T/T分预付,即期和远期.现在 ...

随机推荐

  1. Light Up Your Business Promotions With LED Keychain

    Imagine you want to insert the car key into the keyhole in the dark. What would you do? You will def ...

  2. 【转载】SpringMVC框架介绍

    转自:http://com-xpp.iteye.com/blog/1604183 SpringMVC框架图   SpringMVC接口解释   DispatcherServlet接口: Spring提 ...

  3. C# 泛型说明

    详细说明转 https://www.cnblogs.com/dotnet261010/p/9034594.html 最近项目用到了多个参数的泛型方法,这里说明下泛型的组成: /// <summa ...

  4. opencv:边缘提取

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  5. sql注入基础知识

    信息安全概论课堂作业 SQL注入之万能密码漏洞 第一道题是牵扯到了万能密码漏洞 用户名先输入个’ 返回了sql报错语句,猜测存在sql注入漏洞 使用万能密码测试 登陆成功 原理 假设登录框处的判断代码 ...

  6. AcWing 853. 有边数限制的最短路 bellman-ford 结构体

    //存在负权值 处理负环 //如果能求出来 一般是不存在负权回路 //如果有负回路 那最小距离可能是负无穷 #include <cstring> #include <iostream ...

  7. 【Webex】加入会议是无法正常加入!提示“下载会议组件时无法获取正确的参数。 请联系技术支持以获取帮助。

    来自:Cisco Webex帮助中心 https://help.webex.com/zh-cn/WBX9000023909/Error-Failed-to-get-correct-parameters ...

  8. 【C语言】scanf()输入浮点型数据

    #include<stdio.h> int main() { double x1, x2, x3, x4; printf("输入2个浮点数x1,x2:\n"); sca ...

  9. java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/com.alibaba.druid.filter.Filter].

    九月 11, 2019 2:56:36 下午 org.apache.catalina.loader.WebappClassLoaderBase checkStateForResourceLoading ...

  10. label 阻止冒泡 防止点击label 触发2次事件

    // 必须要把 jnput的外面的label加上事件阻止冒泡,否则点击label的时候,会冒泡到input上 再次触发input的点击事件 $('.xt_order_cleft_modb_rl_dx' ...