213. String Compression【LintCode java】】的更多相关文章

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,…
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, your method…
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 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 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 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 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 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 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 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 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 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 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 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 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 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是普通文本文件 显示详细信息 可以一次性展示文件的名称.类型.大小等信息 可以让我们尽可能地看到更多有用的信息 下面分别演示…
Description Given two strings, write a method to decide if one is a permutation of the other. Example abcd is a permutation of bcad, but abbe is not a permutation of abe 解题:遇到过类似的题目,比较简单的方法是,把字符串转化为字符数组,然后排序.比较每一位的数是否相等.这样做效率比较低,代码如下: public class So…
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…
Public static void main(String[] args) public :main方法是jvm运行的入口,所以必须是public来供外部调用 static :main方法无需生成对象便可由jvm直接调用 void :在退出时,返回值没有意义,jvm规定必须设成void,不然代码可以通过编译,但不能运行 main :jvm唯一标识的main方法,程序的入口 string[] args :命令行运行Java时传入的字符串数组(每个数组以空格隔开) main方法的重载:main方法…
这段时间看的部分感觉没啥需要记录下来的,个人也没什么想法,不过以后还是要多记,多写 package cn.xf.cp.ch02.item66; import java.util.concurrent.TimeUnit; import org.junit.Test; public class StopThread { /** * 停止线程变量 */ private static boolean stopRequested; //吧对变量的读和写方法都进行同步 private static sync…
package cn.xf.cp.ch02.item35; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * *功能:这个是我的注解 *时间:下午7:01:27 *文件:CutterPoint.java *@auth…
有的时候我们一个容器只有一个类型或几个类型并不能满足我们的要求,比如set中存放的元素类型都是同一种,map也就指定的两种 这里我们可以将键进行参数化,而不是将容器参数化,也就是我们可以给容器传一个键的类型,然后value用来放对应的实例,这样就可以存放多个不同的类型了 如: package cn.xf.cp.ch02.item29; import java.util.HashMap; import java.util.Map; public class ManyTypeClass { //一个…
package cn.xf.cp.ch02.item27; import java.util.HashSet; import java.util.Set; public class Union { /** * 这个方法就会有警告 * @param s1 * @param s2 * @return */ public static Set union1(Set s1, Set s2) { Set result = new HashSet(s1); result.addAll(s2); return…
package cn.xf.cp.ch02.item9; import java.util.HashMap; import java.util.Map; public class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; public PhoneNumber(int areaCode, int prefix, int lineNum…