首页 文章 精选 留言 我的

精选列表

搜索[匹配算法],共10005篇文章
优秀的个人博客,低调大师

leetcode 20 Valid Parentheses 括号匹配

Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. 写了一个0ms 的代码: // 20150630.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <stack> #include <string> using namespace std; bool isValid(string s) { if (s=="")return false; stack<char> Parentheses; int size =s.size(); Parentheses.push(s[0]); for(int i = 1;i < size ;++i) { if(Parentheses.top()=='('&&s[i]==')'||Parentheses.top()=='['&&s[i]==']'||Parentheses.top()=='{'&&s[i]=='}') { Parentheses.pop(); if (Parentheses.empty()&&(i+1)!=size) { Parentheses.push(s[i+1]); i++; } } else { Parentheses.push(s[i]); } } if(Parentheses.empty()) { return true; } else { return false; } } int _tmain(int argc, _TCHAR* argv[]) { string s = "()[]{}"; isValid(s); return 0; } 另外一个看着好看点的: class Solution { public: bool isValid(string s) { std::stack<char> openStack; for(int i = 0; i < s.length(); i++) { switch(s[i]) { case '(': case '{': case '[': openStack.push(s[i]); break; case ')': if(!openStack.empty() && openStack.top() == '(' ) openStack.pop(); else return false; break; case '}': if(!openStack.empty() && openStack.top() == '{' ) openStack.pop(); else return false; break; case ']': if(!openStack.empty() && openStack.top() == '[' ) openStack.pop(); else return false; break; default: return false; } } if(openStack.empty()) return true; else return false; } }; python代码: class Solution: # @return a boolean def isValid(self, s): stack = [] dict = {"]":"[", "}":"{", ")":"("} for char in s: if char in dict.values(): stack.append(char) elif char in dict.keys(): if stack == [] or dict[char] != stack.pop(): return False else: return False return stack == [] </pre><pre class="python" name="code">class Solution: # @param s, a string # @return a boolean def isValid(self, s): paren_map = { '(': ')', '{': '}', '[': ']' } stack = [] for p in s: if p in paren_map: stack.append(paren_map[p]) else: if not stack or stack.pop() != p: return False return not stack class Solution: # @param s, a string # @return a boolean def isValid(self, s): d = {'(':')', '[':']','{':'}'} sl = [] for i in s: if i in d: sl.append(i) else: if not sl or d[sl.pop()] != i: return False if sl: return False return True 

优秀的个人博客,低调大师

Elasticsearch使用filter进行匹配关系and,or,not,range查询

RESTful接口URL的格式: http://localhost:9200/<index>/<type>/[<id>] 其中index、type是必须提供的。 id是可选的,不提供es会自动生成。 index、type将信息进行分层,利于管理。 index可以理解为数据库;type理解为数据表;id相当于数据库表中记录的主键,是唯一的。 #向store索引中添加一些书籍 curl -XPUT 'http://172.16.0.14:9200/store/books/1' -d '{ "title": "Elasticsearch: The Definitive Guide", "name" : { "first" : "Zachary", "last" : "Tong" }, "publish_date":"2015-02-06", "price":"49.99" }' #通过浏览器查询 http://172.16.0.14:9200/store/books/1 #在linux中通过curl的方式查询 curl -XGET 'http://172.16.0.14:9200/store/books/1' #在添加一个书的信息 curl -XPUT 'http://172.16.0.14:9200/store/books/2' -d '{ "title": "Elasticsearch Blueprints", "name" : { "first" : "Vineeth", "last" : "Mohan" }, "publish_date":"2015-06-06", "price":"35.99" }' # 通过ID获得文档信息 curl -XGET 'http://172.16.0.14:9200/bookstore/books/1' #在浏览器中查看 http://172.16.0.14:9200/bookstore/books/1 # 通过_source获取指定的字段 curl -XGET 'http://172.16.0.14:9200/store/books/1?_source=title' curl -XGET 'http://172.16.0.14:9200/store/books/1?_source=title,price' curl -XGET 'http://172.16.0.14:9200/store/books/1?_source' #可以通过覆盖的方式更新 curl -XPUT 'http://172.16.0.14:9200/store/books/1' -d '{ "title": "Elasticsearch: The Definitive Guide", "name" : { "first" : "Zachary", "last" : "Tong" }, "publish_date":"2016-02-06", "price":"99.99" }' # 或者通过 _update API的方式单独更新你想要更新的 curl -XPOST 'http://172.16.0.14:9200/store/books/1/_update' -d '{ "doc": { "price" : 88.88 } }' curl -XGET 'http://172.16.0.14:9200/store/books/1' #删除一个文档 curl -XDELETE 'http://172.16.0.14:9200/store/books/1' # 最简单filter查询 # SELECT * FROM books WHERE price = 35.99 # filtered 查询价格是35.99的 curl -XGET 'http://172.16.0.14:9200/store/books/_search' -d '{ "query" : { "filtered" : { "query" : { "match_all" : {} }, "filter" : { "term" : { "price" : 35.99 } } } } }' #指定多个值 curl -XGET 'http://172.16.0.14:9200/store/books/_search' -d '{ "query" : { "filtered" : { "filter" : { "terms" : { "price" : [35.99, 99.99] } } } } }' # SELECT * FROM books WHERE publish_date = "2015-02-06" curl -XGET 'http://172.16.0.14:9200/bookstore/books/_search' -d '{ "query" : { "filtered" : { "filter" : { "term" : { "publish_date" : "2015-02-06" } } } } }' # bool过滤查询,可以做组合过滤查询 # SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND (publish_date != "2016-02-06") # 类似的,Elasticsearch也有 and, or, not这样的组合条件的查询方式 # 格式如下: # { # "bool" : { # "must" : [], # "should" : [], # "must_not" : [], # } # } # # must: 条件必须满足,相当于 and # should: 条件可以满足也可以不满足,相当于 or # must_not: 条件不需要满足,相当于 not curl -XGET 'http://172.16.0.14:9200/bookstore/books/_search' -d '{ "query" : { "filtered" : { "filter" : { "bool" : { "should" : [ { "term" : {"price" : 35.99}}, { "term" : {"price" : 99.99}} ], "must_not" : { "term" : {"publish_date" : "2016-02-06"} } } } } } }' # 嵌套查询 # SELECT * FROM books WHERE price = 35.99 OR ( publish_date = "2016-02-06" AND price = 99.99 ) curl -XGET 'http://172.16.0.14:9200/bookstore/books/_search' -d '{ "query" : { "filtered" : { "filter" : { "bool" : { "should" : [ { "term" : {"price" : 35.99}}, { "bool" : { "must" : [ {"term" : {"publish_date" : "2016-02-06"}}, {"term" : {"price" : 99.99}} ] }} ] } } } } }' # range范围过滤 # SELECT * FROM books WHERE price >= 20 AND price < 100 # gt : > 大于 # lt : < 小于 # gte : >= 大于等于 # lte : <= 小于等于 curl -XGET 'http://172.16.0.14:9200/store/books/_search' -d '{ "query" : { "filtered" : { "filter" : { "range" : { "price" : { "gt" : 20.0, "lt" : 100 } } } } } }' # 另外一种 and, or, not查询 # 没有bool, 直接使用and , or , not # 注意: 不带bool的这种查询不能利用缓存 # 查询价格既是35.99,publish_date又为"2015-02-06"的结果 curl -XGET 'http://172.16.0.14:9200/bookstore/books/_search' -d '{ "query": { "filtered": { "filter": { "and": [ { "term": { "price":59.99 } }, { "term": { "publish_date":"2015-02-06" } } ] }, "query": { "match_all": {} } } } }' 本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/6819449.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Java算法-排序算法

冒泡排序 方法sort是基本的冒泡排序, sort1/sort2是冒泡排序的两种优化 package me.zx.algorithm.program.sort; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 冒泡排序. * Created by zhangxin on 2017/12/27. * * @author zhangxin * @since 0.0.1 */ public final class BubbleSort { private static final Logger LOGGER = LoggerFactory.getLogger(BubbleSort.class); /** * 基本的冒泡排序. * @param a 待排序数组 */ public static void sort(int[] a) { int temp = 0; for(int i = a.length - 1; i > 0; i--) { for(int j = 0; j < i; j++) { if(a[j + 1] < a[j]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } /** * 优化的冒泡排序1. * 当某一趟遍历没有交换,就说明已经遍历好了,就不用再迭代了 * @param a 待排序数组 */ public static void sort1(int[] a) { int temp = 0; boolean sorted = false; for(int i = a.length - 1; i > 0; i--) { sorted = false; //初始值设置为未排序 for(int j = 0; j < i; j++) { if(a[j + 1] < a[j]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; sorted = true; //发生排序时重新设值 } } if(!sorted){ //当经过一次遍历没有发生一次排序, 或者上次排序位置与本次排序位置相同 break; } } } /** * 优化的冒泡排序2. * 记录每次遍历数据之后交换次序的位置,显然这个位置之后的数据已经有序了不用再排序了。因此通过记录最后发生数据交换的位置就可以确定下次循环的范围了 * @param a 待排序数组 */ public static void sort2(int[] a) { int temp = 0; int lastChangeLocation; //上次排序发生的位置 int nowChangeLocation = a.length - 1; //本次排序发生的位置 for(int i = a.length - 1; i > 0; i--) { lastChangeLocation = nowChangeLocation; for(int j = 0; j < i; j++) { if(a[j + 1] < a[j]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; nowChangeLocation = j; //发生排序时重新设值 } } if(lastChangeLocation == nowChangeLocation){ //当经过一次遍历没有发生一次排序, 或者上次排序位置与本次排序位置相同 break; } } } public static void main(final String[] args){ // int[] a = {0, 1, 2, 3, 4, 5, 6}; int[] a = {6, 5, 4, 3, 2, 1, 0}; LOGGER.info("原数组:{}", a); sort1(a); LOGGER.info("现数组:{}", a); } }

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册