Leetcode 4.28 string
1. 38. Count and Say
就是对于前一个数,找出相同元素的个数,把个数和该元素存到新的string里。数量+字符
class Solution {
public String countAndSay(int n) {
if( n == 1)
return "1";
StringBuffer sb = new StringBuffer();
String str = countAndSay(n - 1);
int count = 0;
char c = '0';
for( int i = 0; i < str.length(); i++ ){
c = str.charAt(i);
count = 1;
while((i+1) < str.length() && str.charAt(i) == str.charAt(i+1)){
count++;
i++;
}
sb.append(count + "" + c);
}
return sb.toString();
}
}
2. 58. Length of Last Word
String.lastIndexOf();
;1 class Solution {
public int lengthOfLastWord(String s) {
s = s.trim();
int lastIndex = s.lastIndexOf(' ') + 1;
return s.length() - lastIndex;
}
}
3. 100. Same Tree
采用递归的方法,return中逻辑的运用。
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if( p == null & q == null) return true;
if( p == null || q == null) return false;
if(p.val == q.val)
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
return false;
}
}
Leetcode 4.28 string的更多相关文章
- 【LeetCode算法-28/35】Implement strStr()/Search Insert Position
LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- LeetCode 笔记28 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- [LeetCode] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
随机推荐
- MVC中使用Ninject依赖注入
在.NET MVC中使用Ninject注入,主要分为以下几步: 使用NuGet包添加Ninject引用,我添加的是目前最新版本3.34 在App_Start文件夹下,创建NinjectControll ...
- Centos7上安装docker
Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE. 社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设施.容器 ...
- 从零开始学安全(四十五)●browser_autopwn2漏洞利用配合Ettercap工具实施DNS欺骗攻击
系统:kali 2019 工具Ettercap,Metasploit 环境内网地址 首先 cd /etc/ettercap/ 移动在ettercap 文件下 在用vim 打开编辑 etter.dns ...
- Java_基础篇(数组排序)
Java_基础之数组排序(从小到大) 1.冒泡排序: 冒泡排序可以写成两层循环. 每次循环将最大的数值交换到数组的最后一个. 每排序完一次,后面就少比较一次.所以二层循环的判断条件写成:arry.le ...
- vue的组件化运用(数据在两个组件互传,小问题总结)
一.vue的组件化应用 首先,知道有哪些相关的属性需要用到,再慢慢去理解,运用. 1.两个vue页面 2. slot占位符(可用可不用) 3.props内置属性 4.watch监听函数 5.impor ...
- Spark MLlib KMeans 聚类算法
一.简介 KMeans 算法的基本思想是初始随机给定K个簇中心,按照最邻近原则把分类样本点分到各个簇.然后按平均法重新计算各个簇的质心,从而确定新的簇心.一直迭代,直到簇心的移动距离小于某个给定的值. ...
- vue 导出xlsx表功能
详细步骤: 1.需要安装三个依赖: npm install -S file-saver xlsx npm install -D script-loader 两个命令行包含三个依赖. 2.项目中src下 ...
- MySql安装教程
一.MYSQL的安装 1.打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩,运行“setup.exe”. 2.选择安装类型,有“Typical(默认)”.“Comp ...
- C#事件与委托详解
from https://www.cnblogs.com/sjqq/p/6917497.html C#事件与委托详解[精华 多看看] Delegatedelegate是C#中的一种类型,它实际上是一个 ...
- Mysql事务与锁详解
脏读: 不可重复读: 幻读: 锁: 表级别的意向锁为了提高效率, 我们能给一张表成功加上一个表锁的前提是:没有任何一个事务对这张表的某些行加了锁. 如果没有意向表锁: 如果现在要给一个表加上表锁. 如 ...