判断是否字符串重组 Scramble String @LeetCode

判断一个字符串能否在顺序重组后成为回文串 顺序读入一个字符串数据(不含空格),判断能否将串中字符重新组合可以构成一个回文串。如用lvele可以构成回文串level、elvle,而用label则无法构成回文串。 立即下载

思路:

1 递归:

简单的说,就是s1和s2是scramble的话,那么必然存在一个在s1上的长度l1,将s1分成s11和s12两段,同样有s21和s22。
那么要么s11和s21是scramble的并且s12和s22是scramble的;
要么s11和s22是scramble的并且s12和s21是scramble的。

如果要能通过OJ,还必须把字符串排序后进行剪枝

http://blog.unieagle.net/2012/10/23/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ascramble-string%EF%BC%8C%E4%B8%89%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/


2 DP:

使用了一个三维数组boolean result[len][len][len],其中第一维为子串的长度,第二维为s1的起始索引,第三维为s2的起始索引。
result[k][i][j]表示s1[i...i+k]是否可以由s2[j...j+k]变化得来。

http://www.blogjava.net/sandy/archive/2013/05/22/399605.html


package Level5;

import java.util.Arrays;


/**
 * Scramble String
 * 
 * Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

Below is one possible representation of s1 = "great":

    great
   /    \
  gr    eat
 / \    /  \
g   r  e   at
           / \
          a   t
To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

    rgeat
   /    \
  rg    eat
 / \    /  \
r   g  e   at
           / \
          a   t
We say that "rgeat" is a scrambled string of "great".

Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

    rgtae
   /    \
  rg    tae
 / \    /  \
r   g  ta  e
       / \
      t   a
We say that "rgtae" is a scrambled string of "great".

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
 *
 */
public class S87 {

	public static void main(String[] args) {
		String s1 = "abab";
		String s2 = "bbaa";
		
		System.out.println(isScramble(s1, s2));
		System.out.println(isScrambleDP(s1, s2));
	}
	
	public static boolean isScramble(String s1, String s2) {
		if(s1.length() != s2.length()){
			return false;
		}
        if(s1.length()==1 && s2.length()==1){
        	return s1.charAt(0) == s2.charAt(0);
        }
        
        // 排序后可以通过
        char[] s1ch = s1.toCharArray();
		char[] s2ch = s2.toCharArray();
		Arrays.sort(s1ch);
		Arrays.sort(s2ch);
		if(!new String(s1ch).equals(new String(s2ch))){
			return false;
		}
		
		for(int i=1; i<s1.length(); i++){		// 至少分出一个字符出来
			String s11 = s1.substring(0, i);
			String s12 = s1.substring(i);
			String s21 = s2.substring(0, i);
			String s22 = s2.substring(i);
//			System.out.println(s1 + "-" + s2 + ": "+ s11 + ", " + s12 + ", " + s21 + ", " + s22);
			// 检测前半部是否匹配
			if(isScramble(s11, s21) && isScramble(s12, s22)){
				return true;
			}
			// 前半部不匹配,检测后半部是否匹配
			s21 = s2.substring(0, s2.length()-i);
			s22 = s2.substring(s2.length()-i);
			if(isScramble(s11, s22) && isScramble(s12, s21)){
				return true;
			}
		}
		return false;
    }
	
	public static boolean isScrambleDP(String s1, String s2) {
		int len = s1.length();
		if(len != s2.length()){
			return false;
		}
		if(len == 0){
			return true;
		}
		
		char[] c1 = s1.toCharArray();
		char[] c2 = s2.toCharArray();
		// canTransform 第一维为子串的长度delta,第二维为s1的起始索引,第三维为s2的起始索引
		// canTransform[k][i][j]表示s1[i...i+k]是否可以由s2[j...j+k]变化得来。
		boolean[][][] canT = new boolean[len][len][len];	
		for(int i=0; i<len; i++){
			for(int j=0; j<len; j++){	// 如果字符串总长度为1,则取决于唯一的字符是否想到
				canT[0][i][j] = c1[i] == c2[j];
			}
		}
		
		for(int k=2; k<=len; k++){		// 子串的长度
			for(int i=len-k; i>=0; i--){			// s1[i...i+k]
				for(int j=len-k; j>=0; j--){	// s2[j...j+k]
					boolean canTransform = false;
					for(int m=1; m<k; m++){	// 尝试以m为长度分割子串
						// canT[k][i][j]
						canTransform = (canT[m-1][i][j] && canT[k-m-1][i+m][j+m]) ||	// 前前后后匹配
											  (canT[m-1][i][j+k-m] && canT[k-m-1][i+m][j]);	// 前后后前匹配
						if(canTransform){
							break;
						}
					}
					canT[k-1][i][j] = canTransform;
				}
			}
		}
		
		return canT[len-1][0][0];
	}

}


同样思路,换一种写法:

这其实是一道三维动态规划的题目,我们提出维护量res[i][j][n],其中i是s1的起始字符,j是s2的起始字符,而n是当前的字符串长度,res[i][j][len]表示的是以i和j分别为s1和s2起点的长度为len的字符串是不是互为scramble。
有了维护量我们接下来看看递推式,也就是怎么根据历史信息来得到res[i][j][len]。判断这个是不是满足,其实我们首先是把当前s1[i...i+len-1]字符串劈一刀分成两部分,然后分两种情况:第一种是左边和s2[j...j+len-1]左边部分是不是scramble,以及右边和s2[j...j+len-1]右边部分是不是scramble;第二种情况是左边和s2[j...j+len-1]右边部分是不是scramble,以及右边和s2[j...j+len-1]左边部分是不是scramble。如果以上两种情况有一种成立,说明s1[i...i+len-1]和s2[j...j+len-1]是scramble的。而对于判断这些左右部分是不是scramble我们是有历史信息的,因为长度小于n的所有情况我们都在前面求解过了(也就是长度是最外层循环)。
上面说的是劈一刀的情况,对于s1[i...i+len-1]我们有len-1种劈法,在这些劈法中只要有一种成立,那么两个串就是scramble的。
总结起来递推式是res[i][j][len] = || (res[i][j][k]&&res[i+k][j+k][len-k] || res[i][j+len-k][k]&&res[i+k][j][len-k]) 对于所有1<=k<len,也就是对于所有len-1种劈法的结果求或运算。因为信息都是计算过的,对于每种劈法只需要常量操作即可完成,因此求解递推式是需要O(len)(因为len-1种劈法)。
如此总时间复杂度因为是三维动态规划,需要三层循环,加上每一步需要线行时间求解递推式,所以是O(n^4)。虽然已经比较高了,但是至少不是指数量级的,动态规划还是有很大有事的,空间复杂度是O(n^3)。

public class Solution {
    public boolean isScramble(String s1, String s2) {
        int len = s1.length();
        if(len != s2.length()) {
            return false;
        }
        
        boolean[][][] canScramble = new boolean[len][len][len+1];   // i,j with sublength
        for(int i=0; i<len; i++) {
            for(int j=0; j<len; j++) {
                canScramble[i][j][1] = s1.charAt(i) == s2.charAt(j);    // substring start from i with length 1, compared with substring start from j with length 1
            }
        }
        
        for(int sublen=2; sublen<=len; sublen++) {
            // end_pos = x+(sublen-1) <= len-1, so x <= len-sublen
            for(int i=0; i<=len-sublen; i++) {
                for(int j=0; j<=len-sublen; j++) {
                    for(int p=1; p<sublen; p++) {       // split position
                        canScramble[i][j][sublen] |= (canScramble[i][j][p] && canScramble[i+p][j+p][sublen-p]) || 
                                                     (canScramble[i][j+sublen-p][p] && canScramble[i+p][j][sublen-p]);
                    }
                }
            }
        }
        
        return canScramble[0][0][len];
    }
}


http://blog.youkuaiyun.com/linhuanmars/article/details/24506703


《Mac 配置 Codex App 代理完整指南:端口检测 + launchctl 设置 + 取消代理》 本文详细介绍 Mac 上 Codex App 代理配置完整流程,包括如何查看代理端口、如何使用 launchctl 设置 HTTP_PROXY / HTTPS_PROXY、如何验证代理是否生效以及如何取消代理配置。适用于 Mac Codex 代理、Mac HTTP_PROXY 设置、Mac ChatGPT 代理等开发环境场景。 阅读详情

相关推荐

利用按键精灵实现批量截图与智能命名的高效工作流

本文详细介绍了如何利用按键精灵软件构建高效的批量截图与智能命名自动化工作流。通过基础脚本编写、循环逻辑实现及高级命名策略,解决手动操作耗时易错的问题,帮助用户实现一键自动截图、智能保存与文件重命名,大幅提升软件测试、教程制作及数据报告等场景的工作效率。

weixin_29268267的博客 569

LeetCode——87. 扰乱字符串(Scramble String)[困难]——分析及代码(Java)

LeetCode——87. 扰乱字符串[Scramble String][困难]——分析及代码[Java]一、题目二、分析及代码1. 动态规划(1)思路(2)代码(3)结果三、其他 一、题目 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行下述步骤: - 在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 x 和 y ,且满足 s = x + y 。 - 随机 决定是要「

江南土豆的博客 428

IGS精密星历高效获取与解析指南

本文详细介绍了IGS精密星历在高精度定位中的核心作用,对比了IGS Final、IGR和IGU等不同产品的精度与时效性差异。指南提供了从武汉大学、NASA CDDIS等数据中心高效下载数据的实战方法,并深入解析了SP3文件格式,帮助用户掌握精密星历的获取、解析与自动化处理技巧,以支持厘米级定位应用。

garlic的博客 839

leetcode 87. Scramble String(扰乱字符串)

DP memorize, 递归, 字符串

蓝羽飞鸟的博客 450

服务器卡死排查实战:从现象分类到根因定位的完整手册

服务器卡死是运维中最令人头疼的故障之一,从游戏圈吐槽的“土豆服务器”到生产环境中的Linux系统无响应、rsync复制挂起,本质都是系统或进程失去正常响应能力。其原理通常涉及CPU、内存、磁盘IO或网络资源耗尽,以及内核和驱动异常。掌握系统化的排查方法,能够帮助工程师快速定位根因、缩短故障时间,避免盲目重启。本文从资源、层级、场景等多维度分类,结合top、dmesg、strace等工具,梳理一套从现象确认、指标收集、日志分析到进程定位的实战排查流程,并覆盖常见高频场景与预防治理建议。无论你是运维新手还是老手

weixin_33851177的博客 375

了解随机振动疲劳分析中 Ansys nCode DesignLife 的平均应力校正

通过选择静态环境,我们可以将其结果视为疲劳计算的平均应力,如图 3 所示,因此从静态偏移载荷工况中提取的平均应力用于转移预测雨流计数中所有循环的平均值。您可以通过观看下面的教学视频获得宝贵的见解,该视频将指导您完成利用 Ansys nCode DesignLife 分析平均应力对随机振动载荷下结构的影响的过程。它基于 R 比率或平均应力水平。另一方面,平均应力校正可以帮助消除平均应力的负面影响,提高结果的保真度。Ansys nCode 提供先进的平均应力校正技术,使工程师能够考虑平均应力对疲劳寿命的影响。

jkh920184196的博客 1518

判断字符串里面有多少个重复字符

用map的理由只有一个,那就是快。

qq_33363757的博客 408

Linux DRM驱动实战:手把手教你用drmModeSetCrtc显示纯色画面(附完整代码)

本文提供了一份完整的Linux DRM驱动实战指南,手把手教你使用drmModeSetCrtc在屏幕上显示纯色画面。文章详细解析了从打开设备、获取资源、创建帧缓冲区到最终提交画面的完整调用流程,并附带了可直接编译运行的完整代码示例,帮助开发者快速上手Linux图形底层开发。

weixin_29216353的博客 512

【字符串】scramble string

相当于按这种二分(也不算找中心分啦)的结构,结构之间可以互换顺序,结构之内(由于又可以二分)也可以互换顺序。问你两个字符串互不互为这种关系。 大致思路: 刚开始没反应过来——其实这也是求“字符串的全排列”是否对应——>看各字母出现的次数。 只不过,这涉及到二叉树的结构,很明显又让人知道要——递归。 所以,就是遍历i来分割出不同的二叉树结构,然后递归地来看两个子树是不是满足全排...

It's the Climb 478

华为OD机试双机位C卷-魔法收积木 (C/C++/Py/Java/Js/Go)

华为OD机试(机考)双机位C卷 【魔法收积木】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为od - 魔法收积木。

qq_45776114的博客 1940

CloudCompare 二次开发(6)——插件中拖拽添加Qt窗口(区域生长算法为例)

手动拖拽的方式搭建Qt对话框界面的制作流程,以PCL中的点云区域生长算法为例进行制作。

点云侠的博客 2153

87. 扰乱字符串(Scramble String) LeetCode C++版本

题目:难度Hard(请自行点击链接查看) https://leetcode-cn.com/problems/scramble-string/ V2版本的执行时间已经能达到较为理想的结果了。后面只是为了追求更高执行效率。 一、分析: 由题意:这个“乱序”字符串,是由原字符串任意二叉树分割(每一支至少要有一个字符)成每个叶子只有一个字符,再选择任意多个子节点交换左右子树得到。 这操作很明显属于...

玮智能的博客 602

【华为OD技术面试手撕真题】134、爬楼梯 | 手撕真题+思路参考+代码解析(C & C++ & Java & Python & JS)

【华为OD技术面试手撕真题】134、爬楼梯 | 手撕真题+思路参考+代码解析(C & C++ & Java & Python & JS) 华为od机试,华为od机试真题,华为od机试E卷,华为odE卷,华为od机试真题E卷,华为OD,华为OD机试,华为OD机试真题,华为od机考,华为od机试D卷,华为od机试真题D卷,华为od手撕代码,华为od,华为od机试C卷,华为od e卷,华为2025e卷,od机试,2025E卷,华为OD机试E卷,什么是华为od,华为od介绍,全面了解华为od,2025华为od,如何

KJ.JK 2184

Scramble String

题目描述: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / \

小武思密达的专栏 531

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

scramble-string

【题目描述】Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 =“great”: great / gr eat / \...

weixin_36909758的博客 527

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

87. Scramble String(扰乱字符串)

87. Scramble String 记录下讨论区中非常好的一个解题思路,老样子我们还是先上代码然后解析。 class Solution { public: bool isScramble(string s1, string s2) { int m = s1.size();int n = s2.size(); string res; if (m == 0||n==0) re...

asd994144293的博客 471

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

字符串:重组

    给定一个字符串,要求对其进行重组,使相同的字符不相邻,如果无法满足要求,则返回空串。如字符串“aab”,重组后为“aba”。    分析:先统计每个字符出现的次数,把字符和次数存入HashMap中,对HashMap根据次数进行排序,然后再进行重组:前一半append,后一半间隔插入。(其实也可以不用排序,只需找到出现次数最多的那个字符,先append该字符即可) public Str...

Peanut 大本营 1182

重组字符串

题目描述 输入一个以回车结束的字符串,将字符串中的字符按ASCII码从小到大顺序重组后输出。 输入 输入一个以回车结束的字符串(少于80个字符)。 输出 将字符串中的字符按ASCII码从小到大顺序重组后输出 样例输入 Student 样例输出 Sdenttu #include<stdio.h> #include<string.h> int main() { char a[...

hqm793899006的博客 1502

[LeetCode-767]字符串重组

题目:https://leetcode-cn.com/problems/reorganize-string 给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同。 若可行,输出任意可行的结果。若不可行,返回空字符串。 示例1: 输入: S = "aab" 输出: "aba" 示例 2: 输入: S = "aaab" 输出: "" 注意: S 只包含小写字母并且长度在[...

也许(Oliver) 727

Scramble String -- LeetCode

原题链接:http://oj.leetcode.com/problems/scramble-string/这道题看起来是比较复杂的,如果用brute force,每次做切割,然后递归求解,是一个非多项式的复杂度,一般来说这不是面试官想要的答案。这其实是一道三维动态规划的题目,我们提出维护量res[i][j][n],其中i是s1的起始字符,j是s2的起始字符,而n是当前的字符串长度,res[i][j...

编程语言小筑 265

Scramble String (Java)

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / \ gr

豆腐脑是咸的 542

scramble-string Java code

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 =”great”: great / \

qq_29606255的博客 304
上一篇: DP36 如何切出最大长度乘积 Maximum Product Cutting @geeksforgeeks
下一篇: 求全是1的最大矩阵面积 Maximal Rectangle @LeetCode
chiiis
博客等级 码龄14年 422粉丝 429原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值