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 ...
随机推荐
- Go 只读/只写channel
Go中channel可以是只读.只写.同时可读写的. //定义只读的channel read_only := make (<-chan int) //定义只写的channel write_onl ...
- 基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
今天我们来盘一盘Socket通讯和WebSocket协议在即时通讯的小应用——聊天. 理论大家估计都知道得差不多了,小编也通过查阅各种资料对理论知识进行了充电,发现好多demo似懂非懂,拷贝回来又运行 ...
- .net工具类 获取枚举类型的描述
一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述. 为了方便获取这些信息,就封装了一个枚举扩展类. /// <summary> /// ...
- ajax的嵌套需要注意的问题
当我们要嵌套ajax的时候,需要注意 异步/同步 的处理,一般是要设置成同步,如果是异步,那么被嵌套的ajax的操作很可能获取不到想要的值,因为他可能比嵌套他的ajax跑的更早 在ajax中有一个as ...
- WPF 自定义 ImageButton
控件源码: public class ImageButton : Button { public ImageButton() { } public string No ...
- 数据结构——Java实现链栈
一.分析 栈是限定仅在表的一端进行插入或删除操作的线性表,对于栈来说,操作端称为栈顶,另一端则称为栈底,栈的修改是按照后进先出的原则进行的,因此又称为后进先出的线性表. 链栈是指采用链式存储结构实现的 ...
- mysql判断条件不存在插入存在更新某字段
insert into mst_sequence(seq_type, seq_desc, seq_date, seq_no, create_time) VALUES('CK', 'XXX', NOW( ...
- tomcat,httpd 日志格式说明
tomcat 日志说明 配置文件server.xml 默认日志格式为 pattern="%h %l %u %t "%r" %s %b" 推荐使用 pattern ...
- 【English】十三、英语中的连词有哪些,都有什么作用
一.什么是连词 参考:https://m.hujiang.com/en_cixing/yylc/ 连词是一种虚词,用于连接单词.短语.从句或句子,在句子中不单独用作句子成分. 连词按其性质可分为并列连 ...
- Jenkins分布式部署配置
为什要使用Jenkins分布式? 利用jenkins分布式来构建job,当job量足够大的时候,可以有效的缓解jenkins-master上的压力,提高并行job数量, 减少job处于pending状 ...