num

力扣 OJ 338. 比特位计数

家住魔仙堡 提交于 2020-02-08 19:18:08
题目: 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 示例 1: 输入: 2 输出: [0,1,1] 示例 2: 输入: 5 输出: [0,1,1,2,1,2] 进阶: 给出时间复杂度为O(n*sizeof(integer))的解答非常容易。但你可以在线性时间O(n)内用一趟扫描做到吗? 要求算法的空间复杂度为O(n)。 你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 __builtin_popcount)来执行此操作。 代码: class Solution { public: vector<int> countBits(int num) { vector<int>ans; ans.insert(ans.end(), 0); for (int i = 1; i <= num; i++) { ans.insert(ans.end(), i % 2 + ans[i / 2]); } return ans; } }; 来源: CSDN 作者: csuzhucong 链接: https://blog.csdn.net/nameofcsdn/article/details/104220855

Java 基础数据与运算

こ雲淡風輕ζ 提交于 2020-02-08 18:32:57
基础数据与运算 关键字和标识符 关键字:被赋予了特殊含义的字符,如class,public,void,if等等; 首先我们注意到这些关键字一般来说都是小写的,而且它们也是相当于一些特定的称谓,不能运用在其他的地方,或者不能去改变它们的含义。 标识符:这是我们自定义的一些名称,目的是为了增强我们代码的可读性,当然定义标识符的时候也存在组成规则的: (1)由字母、数字、下换线、美元符组成;ps:数字不能作为开头使用; (2)还有就是要严格的区分大小写,大小写不同表示的含义就会有所不同; (3)不能使用关键字和Java自带的类名称作为标识符来使用; 注意:为了增强代码的可读性,应起有意义的单词作为其标识符。 标识符的组成规范(t规范与规则是有所不同的): (1)小驼峰式命名法:如果一个标识符是由多个单词组成的一个词,那么除了第一个单词首字母小写外,其他单词首字母大写 ,如:myName等,主要应用于变量名、函数名; (2)大驼峰式命名法:如果一个标识符是由多个单词组成的,那么所有单词的首字母大写,如:MyName,主要应用于类、接口; (3)全大写命名法:如果一个标识符是由多个单词组成的,那么每一个单词都要大写,单词与单词之间用_连接;如MAX_VALUE DEFAULT_SIZE,主要应用于定义常量。 注释 注释:注释的存在也是为了更好的读懂代码,这其中有单行注释、多行注释、文档注释

try catch finally 中包含return的几种情况,及返回结果

若如初见. 提交于 2020-02-08 18:18:51
当当当,兴致勃勃的第二篇博客,散花~ 下面是正题(敲黑板) 第一种情况: 在try和catch中有return,finally中没有return ,且finally中没有对try或catch中要 return数据进行操作的代码,这种情况也是最好理解的。 public class Test { public static int num=1; public static void main(String[] args) throws ParseException { int result; result = num(); System.out.println(result); } private static int num() { try{ int b=4/0; return num = num+2; }catch(Exception e){ return num = num+3; }finally { System.out.println("不管你怎么样,我都是要执行"); } } } 输出内容为: 不管你怎么样,我都是要执行 4 原因 ,int b=4/0;发生了异常,直接进 入catch 的代码块中执行了return num = num+3;此时把返回的结果4。如果没有异常的话会执行try中的return,catch中的代码不会执行,但是无论怎样

Python3.7实现自动刷博客访问量(只需要输入用户id)(转)

与世无争的帅哥 提交于 2020-02-08 17:48:06
新增了代理功能,代码很浅显易懂不想多余讲解 import re import requests from requests import RequestException import time import random from bs4 import BeautifulSoup # 获取网页的response文件 def get_response(url): try: headers = { 'Referer': 'https://blog.csdn.net', # 伪装成从CSDN博客搜索到的文章 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36' # 伪装成浏览器 } # 设置代理ip porxy_list = [ {"http": "http://218.60.8.99:3129"}, {"http": "http://114.226.244.78:9999"}, {"http": "http://39.137.95.71:80"}, {"http": "http://115.159.31.195:8080"}, {"http": "http://39.137.69

HDU 2181 哈密顿绕行世界问题

南笙酒味 提交于 2020-02-08 17:35:12
题目描述: 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市。 Input 前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行以后每行有1个数m,m<=20,m>=1.m=0退出. Output 输出从第m个城市出发经过每个城市1次又回到m的所有路线,如有多条路线,按字典序输出,每行1条路线.每行首先输出是第几条路线.然后个一个: 后列出经过的城市.参看Sample output Sample Input 2 5 20 1 3 12 2 4 10 3 5 8 1 4 6 5 7 19 6 8 17 4 7 9 8 10 16 3 9 11 10 12 15 2 11 13 12 14 20 13 15 18 11 14 16 9 15 17 7 16 18 14 17 19 6 18 20 1 13 19 5 0 Sample Output 1: 5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 5 2: 5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 5 3: 5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 5 4: 5 1 2 3 10

建立简单的静态链表

你离开我真会死。 提交于 2020-02-08 16:55:00
1 #include <stdio.h> 2 struct student 3 { 4 int num; 5 float score; 6 struct student*next; 7 }; 8 int main() 9 { 10 struct student a,b,c,*head,*p; 11 a.num=10101;a.score=89.5; 12 b.num=10103;b.score=90; 13 c.num=10103;c.score=90; 14 head=&a; 15 a.next=&b; 16 b.next=&c; 17 c.next=NULL; 18 p=head; 19 do{ 20 printf("%ld%5.1f\n",p->num,p->score); 21 p=p->next; 22 }while(p!=NULL); 23 return 0; 24 } head指向a的结点,a.next指向b的结点·······c.next=null是为了不让其指向任何存储单元 需要定义一个指针变量来接受head的地址 本程序所有节点都是在程序中定义的所以用完后不能释放,为静态链表 来源: https://www.cnblogs.com/ZJK132/p/12283764.html

Python字符串格式化

放肆的年华 提交于 2020-02-08 16:40:08
Python字符串格式化: 字符串中符号:   %c :单个字符   %s :字符串   %d :整数   %u :无符号整数   %o :无符号八进制数   %x :无符号十六进制数   %X :无符号十六进制数(大写)   %f :浮点数,可指定小数点后的精度   %e :对浮点数使用科学计数法,可指定小数点后的精度。%E 与 %e 作用相同   %g :%f 和 %e 的简写,%G 与 %g 作用相同 注:%o 为八进制(oct)、%x 为十六进制(hex)。 # %c 只能输出单个字符 a = 'a' print("%c"%(a)) # a # %s 字符串 strs = 'hello' print("%s"%(strs)) # hello # %d 整数 num = 123 print("%d"%(num)) # 123 num = -123 print("%d"%(num)) # -123 # %u 无符号整数 num = 123 print("%u"%(num)) # 123 num = -123 print("%u"%(num)) # -123 # %o 八进制数 num = 11 print("%o"%(num)) # 13 1*8**1 + 3*8**0 = 11 print(oct(11)) # 0o13 # %x 十六进制数 num = 18 print("

338. 比特位计数

风格不统一 提交于 2020-02-08 15:47:36
给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 示例 1: 输入: 2 输出: [0,1,1] 示例 2: 输入: 5 输出: [0,1,1,2,1,2] 1.正常做法,没一个元素做popcount(n=n&(n-1)) 2.dp p(x)=p(x/2)+x%2 class Solution(object): def countBits(self, num): """ :type num: int :rtype: List[int] p(x) = p(x/2)+ x mod 2 """ res = [0 for _ in range(num+1)] for i in range(1,num+1): res[i]=res[i>>1] + (i&1) return res 来源: CSDN 作者: p0ther 链接: https://blog.csdn.net/qq_36328915/article/details/104222777

Java 进制转换 输出17进制

依然范特西╮ 提交于 2020-02-08 12:57:58
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while(input.hasNextInt()){ int num = input.nextInt(); String str4 = Integer.toString(num,17); System.out.println(str4 ); } input.close(); } } 来源: CSDN 作者: 马心宇 链接: https://blog.csdn.net/weixin_44438770/article/details/103877930

第二天练习题

戏子无情 提交于 2020-02-08 12:32:53
华氏温度转化为摄氏温度 这道题最主要的是 Scanner 这个控制台输入语句 用学过的知识来看的话 Scanner 是一个类 题中的 input 是类 Scanner 的对象 紧接着,input 这个对象调用了 nextDouble() 这个方法 这个输入语句的格式便是 Scanner 对象 =new Scanner(System.in); int(double) 变量 =对象.nextInt(Double)(); 比较重要的是当一个程序调用了Scanner这个方法后要把该类的所有方法即该类的”包“导进来 即: import java.util.Scanner;(如果该程序使用了util目录下的另一个类,导包的源代码可以写为 import java.util.*) import java.util.Scanner; class Excercise1{ public static void main (String[] args){ Scanner input =new Scanner(System.in); //输入摄氏温度 System.out.println("输入摄氏温度:"); double c=input.nextDouble(); //摄氏温度转为华氏温度 double f=0.0; f=(9 / 5.0) * c + 32; System.out.println(