365. Count 1 in Binary【LintCode java】】的更多相关文章

Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, return 2 Given 1023, return 9 Challenge If the integer is n bits with m 1 bits. Can you do it in O(m) time? 解题:很简单,但是要考虑范围的问题.代码如下: public…
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 Return 100 解题:二进制相加.我的思路是,先转成StringBuilder对象(reverse方法比较好用),因为相加是从最后开始,所以先用reverse方法倒转过来.和上一题类似,用carry变量表示进位,0为不进位,1为需要进一位.最后的结果再倒过来.具体细节标注在代码中,代码如下:…
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or …
Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target. A valid path is from root node to any of the leaf nodes. Example Given a binary tree, and target = 5: 1 / \ 2 4 / \ 2 3 return [ [1, 2,…
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ 2 3 / \ 4 5 return the new binary tree with same structure and same value: 1 / \ 2 3 / \ 4 5 解题:链表复制.递归解法比较简单,代码如下: /** * Definition of TreeNode: * pu…
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. A word is defined as a character sequence consists of non-spa…
Description Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node. Example Linked list is 1->2->3->4, and given node 3, delete the node in place 1->2->4 解题:删除指定的结点.由于java没有delete 或者 fr…
Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1. A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of…
Description Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. If the "compressed" string would not become smaller than the original string,…
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2->3->4, you should return the list as 2->1->4->3. Challenge Your algorithm should use only constant space. You may not modify the value…
Description Cosine similarity is a measure of similarity between two vectors of an inner product space that measures the cosine of the angle between them. The cosine of 0° is 1, and it is less than 1 for any other angle. See wiki: Cosine Similarity H…
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent. Find the number of islands. Example Given gra…
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. Example The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"…
Description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Have you consider that the string might be empty? This is a good question to ask during an interview. For the purpose of this pr…
Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). Example Given x = 123, return 321 Given x = -123, return -321 解题:注意别溢出即可,所以结果用long型变量保存,最后返回的时候强转成int型.代码如下: public class Solution { /**…
Description Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Example Given [1,2,3] which represents 123, return [1,2,4]. Give…
Description Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where empty cells are filled with the character .. A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be vali…
Description Partition an integers array into odd number first and even number second. Example Given [1, 2, 3, 4], return [1, 3, 2, 4] Challenge Do it in-place. 解题:将一个数组中的奇数和偶数分开,原地分开,不申请额外的空间.思路是,记录好最后一个奇数的后一个位置,也可以理解为偶数中的第一个·.遍历数组,如果当前数组中的元素是偶数,那么什么…
Description There is a building of n floors. If an egg drops from the k th floor or above, it will break. If it's dropped from any floor below, it will not break. You're given two eggs, Find k while minimize the number of drops for the worst case. Re…
Description In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (i.e., each disk sits on top of…
这篇文章主要讲解:要想使用Java来开发软件,需要做哪些准备工作? 配置电脑 作为一名开发人员,对文件的类型.大小等信息是比较敏感的,所以建议你的电脑做2个配置: 显示文件扩展名 文件扩展名(Filename Extension),也称为文件的后缀名,用来标记文件类型 通过文件扩展名,我们可以很容易地区分出不同类型的文件 比如.png是图片文件..mp4是视频文件..txt是普通文本文件 显示详细信息 可以一次性展示文件的名称.类型.大小等信息 可以让我们尽可能地看到更多有用的信息 下面分别演示…
源码下载:https://files.cnblogs.com/files/xiandedanteng/OracleAccessComparison20191117.rar 做这个比较工程初衷是:我在单位试验一个单线程删21张表和多线程删21张表比较方案,发现单线程从八百五十万数据需要5分钟上下,多线程(一张表一个线程,为此还把MaxActive调大了)却需要9分钟左右,尤其是删其中两张两百万级别的大表的两个线程总不结束.这个结果是和我以往的多线程比单线程快的观念是违背的,我想了想,这个有违常识的…
测试机Oracle版本: SQL> select * from v$version; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production PL/SQL Release 11.2.0.1.0 - Production COR…
昨天做了插入的单线程多线程比较,今天做个删除的. 单线程批量删除类代码: package com.hy.delete.singlethread; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; im…
三张表DDL如下: CREATE TABLE tb01 ( "ID" ,) not null primary key, "NAME" NVARCHAR2() not null, "AGE" ,) not null , "CREATEDTIME" ) not null ) CREATE TABLE tb02 ( "ID" ,) not null primary key, "SN" NVAR…
由于按一千条一插程序长期无反应,之后改为百条一插方式,运行完发现插入百万记录需要9m17s,虽然比MySQL效率差,但比单条插入已经好不少了. 对Oracle的批量插入语法不明的请参考:https://www.cnblogs.com/xiandedanteng/p/11806720.html 代码如下: package com.hy; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Result…
Spark 是离线数据处理的一种大数据技术,和Flick相比数据处理要延后,因为Flick是实时数据处理,而Spark需要先读取数据到内存. Spark的库是基于Scala写的,虽然Scala也是运行在jvm上的,但是Spark提供的Java  api的能力和原生api并不完全相同,据说执行效率也有微弱差异. 但是scala语法比较难,编码也不如Java规范,用的人和企业越来越少.为了更好推广和更好交接,我们也选择Java API. 环境搭建 官方下载 maven依赖进来 安装hadoop 小试…
学习一门开源技术一般有两种入门方法,一种是去看官网文档,比如Getting Started - Spark 3.2.0 Documentation (apache.org),另一种是去看官网的例子,也就是%SPARK_HOME%\examples下面的代码.打开IDEA,选择File-Open... 跟前面文章中方法一样导入jars目录到classpath. Spark解析json字符串 第一个例子是读取并解析Json.这个例子的结果让我有些震惊,先上代码: public static void…
package cn.xf.cp.ch02.item33; import java.util.EnumMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.junit.Test; /** * *功能:序数索引 *时间:下午4:24:48 *文件:HerbOld.java *@author Administrator * */ public class HerbOld { publ…
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14302  Solved: 5779[Submit][Status][Discuss] Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 I…