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 ...
随机推荐
- zk分布式任务管理
在我们的系统开发过程 中不可避免的会使用到定时任务的功能,而当我们在生产环境部署的服务超过1台时,就需要考虑任务调度的问题,防止两台或多台服务器上执行同一个任务,这个问题今天咱们就用zookeeper ...
- [翻译]Linux 内核里的数据结构 —— 基数树
目录 Linux 内核里的数据结构 -- 基数树 基数树 Radix tree Linux内核基数树API 链接 Linux 内核里的数据结构 -- 基数树 基数树 Radix tree 正如你所知道 ...
- 从零开始搭建Prometheus自动监控报警系统
从零搭建Prometheus监控报警系统 什么是Prometheus? Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB).Prometheus使用Go语言开 ...
- 使用cookie来做身份认证
文章是msdn的官方文档,链接在这里.其实也有中文的文档,这里还是想做一个记录. 文章有asp.net core 2.x 和1.x 版本,我这里就忽略1.x了. 下面先说几点额外的东西有助于理解. A ...
- 记一次按需加载和npm模块发布实践
按需加载 在使用 lodash 的时候我们可以使用这样的代码 //一 import {omit} from "lodash"; //二 import l from "lo ...
- 使用JsonProperty Attribute修改返回json
使用JsonProperty Attribute修改返回 json 值的name 本例使用JsonPropertyAttribute在序列化为JSON时更改属性的名称. public class Vi ...
- Vcomputer简介
1.Vcompter存储程序式计算机虚拟机软件简介 Vcompter存储程序式计算机虚拟机软件的文件名为comp_alpha(一般要先安装java运行环境,然后双击该软件即可运行),该软件是桂林电 ...
- C# 以函数Action/Func/Task作为方法参数
以Action.Func.Task作为方法参数,mark一下 以Action为参数 public void TestAction() { //Action参数 ExecuteFunction(() = ...
- vue-cli3 中跨域解决方案
此方案只能用于开发环境,线上最好设置同源策略(遇到个后端,装你妈批) 前后端不在同一服务器的情况下,前端要访问后端API,可通过在vue.config.js中配置代理服务器. 0:前提条件 1:安装v ...
- python的学习笔记01_6练习
# 一.[用户登陆程序]# 基础需求:# 让用户输入用户名密码# 认证成功后显示欢迎信息# 输错三次后退出程序 count = 0 name = "cheng" password ...