/**
* @Author: weblee
* @Email: likaiweb@163.com
* @Blog: http://www.cnblogs.com/lkzf/
* @Time: 2015年2月18日上午10:03:42
*
************* function description ***************
*
****************************************************
*/ public class BestTime_to_Buy_and_SellStockIV {
public int maxProfit(int k, int[] prices) {
if (prices.length == 0) {
return 0;
} if (k >= prices.length) {
return solveMaxProfit(prices);
} /*
* 局部最优值是比较前一天并少交易一次的全局最优加上大于0的差值,
* 和前一天的局部最优加上差值后相比,两者之中取较大值,
* 而全局最优比较局部最优和前一天的全局最优
*/
int[] global = new int[k + 1];
int[] local = new int[k + 1]; for (int i = 0; i < prices.length - 1; ++i) {
int diff = prices[i + 1] - prices[i]; for (int j = k; j >= 1; --j) {
local[j] = Math.max(global[j - 1] + Math.max(diff, 0), local[j]
+ diff); global[j] = Math.max(global[j], local[j]);
}
} return global[k];
} public static int solveMaxProfit(int[] prices) {
int result = 0; for (int i = 1; i < prices.length; i++) {
if (prices[i] - prices[i - 1] > 0) {
result += (prices[i] - prices[i - 1]);
}
} return result;
} }
package com.weblee.Medium;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map; /**
* @Author: weblee
* @Email: likaiweb@163.com
* @Blog: http://www.cnblogs.com/lkzf/
* @Time: 2015年2月18日上午9:20:12
*
************* function description ***************
*
****************************************************
*/ public class RepeatedDNASequences {
public List<String> findRepeatedDnaSequences(String s) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<String> result = new ArrayList<String>(); if (s.length() <= 10) {
return result;
} // encode
char[] convert = new char[26];
convert[0] = 0;
convert[2] = 1;
convert[6] = 2;
convert[19] = 3; int hashValue = 0; /*
* the first 10 characters string
*/
for (int pos = 0; pos < 10; ++pos) {
hashValue <<= 2;
hashValue |= convert[s.charAt(pos) - 'A'];
} // first 10-letter-long sequences encode -> value
map.put(hashValue, 1); // remove duplicate
HashSet<Integer> set = new HashSet<>(); for (int pos = 10; pos < s.length(); ++pos) {
// left 2 bit, equal to multiply 4
hashValue <<= 2;
// the 2 right bit valued encode of s.char(pos)
hashValue |= convert[s.charAt(pos) - 'A'];
// 最高两位置0
hashValue &= ~(0x300000); if (!map.containsKey(hashValue)) {
map.put(hashValue, 1);
} else {
if (!set.contains(hashValue)) {
// 10-letter-long sequences
result.add(s.substring(pos - 9, pos + 1)); set.add(hashValue);
} map.replace(hashValue, 1 + map.get(hashValue));
}
} return result;
} }

RepeatedDNASequences BestTime_to_Buy_and_SellStockIV的更多相关文章

  1. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. 187. Repeated DNA Sequences

    题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  3. Leetcode:Repeated DNA Sequences详细题解

    题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  4. 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告

    原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...

  5. LeetCode哈希表

    1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...

  6. leetcode187. Repeated DNA Sequences

    https://leetcode.com/problems/repeated-dna-sequences/#/description   https://leetcode.com/problems/r ...

  7. 187 Repeated DNA Sequences 重复的DNA序列

    所有DNA由一系列缩写为A,C,G和 T 的核苷酸组成,例如:“ACGAATTCCG”.在研究DNA时,识别DNA中的重复序列有时非常有用.编写一个函数来查找DNA分子中所有出现超多一次的10个字母长 ...

  8. [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  9. 重复的DNA序列[哈希表] LeetCode.187

    所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...

随机推荐

  1. Struts2 中的数据传输

    1.     如何将参数从界面传递到Action? 你可以把Struts2中的Action看做是Struts1的Action+ActionForm,即只需在Action中定义相关的属性(要有gette ...

  2. cocos2d 小游戏

    今天写了一个小游戏,发现看过的代码自己来写还是会经常出错,还是要多自己动手写写哈. 先上几张游戏界面图 void HelloWorld::addTarget() { //首先初始化精灵 CCSprit ...

  3. Zend Studio 10正式版注册破解

    1.文件和汉化文件 ZendStudio官方下载地址:http://www.geekso.com/component/zendstudio-downloads/ 百度云地址: 10.0.0.msi文件 ...

  4. Mantis 安装与配置

    1. 适用范围 a. 本文介绍基于 Windows 下的缺陷管理平台 Mantis. 2. 软件准备 a. 下载 EasyPHP:http://www.easyphp.org/easyphp-devs ...

  5. 介绍一些适用于 Web 开发者的 Atom 编辑器插件

    Atom 的社区很繁荣,有着丰富的扩展/插件(packages).安装 Atom 的 Package 非常简单,可以在编辑器的偏好设置里面安装,也可以在命令行中使用 apm 命令来安装. 在介绍适用于 ...

  6. oracle学习总结3

    1:ddl语句创建一张学生表 create table stu(id number(6) not null,name varchar2(20),age number(3),gender char(1) ...

  7. php json_encode转JSON 编码显示中文

    对变量进行 JSON 编码显示中文 /**context":"/u2345/u43245/u2345 转成中文显示 * 对变量进行 JSON 编码[{"time" ...

  8. Android 常见adb命令

    1.  查看所有已链接的设备 命令: adb devices   例: C:\Users\laiyu>adb devices List of devices attached 5d3b5aac  ...

  9. ADO.NET通用数据库访问类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. Android的几种alert对话框

    @Override public void onClick(View v) { switch (v.getId()) { case R.id.d1: AlertDialog.Builder build ...