首页 / 算法 / [java实现]常见算法之字符串操作
[java实现]常见算法之字符串操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[java实现]常见算法之字符串操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5446字,纯文字阅读大概需要8分钟。
内容图文
![[java实现]常见算法之字符串操作](/upload/InfoBanner/zyjiaocheng/1096/2b3a4da09c914297be09ad8d61994645.jpg)
一、字符串反转
把一个句子中的打次进行反转,比如“how are you” ,变为 “you are how”
![技术分享图片](/img/jia.gif)
![技术分享图片](/img/jian.gif)
// 字符串反转 public class StringTest { // 字符反转的方法 private void swap(char[] c, int front, int end) { if (front > end || end >= c.length) { return; } while (front < end) { char tmp = c[front]; c[front] = c[end]; c[end] = tmp; front++; end--; } } // O(n)public String swapStr(String str) { char[] cArr = str.toCharArray(); // 整个字符串的字符反转 swap(cArr, 0, cArr.length - 1); // 反转整个字符串中的所有字母,how are you -> uoy era wohint begin = 0; // 对字符串中的每个单词反转,除了最后一单词for (int i = 0; i < cArr.length; i++) { if (cArr[i] == ‘ ‘) { swap(cArr, begin, i - 1); begin = i + 1; } } // 最后一个单词的反转 swap(cArr, begin, cArr.length - 1); returnnew String(cArr); } publicstaticvoid main(String[] args) { String str = "how are you"; System.out.println(new StringTest().swapStr(str)); } }
二、判断字符串是否由相同的字符组成
判断连个字符串的字母个字母的个数是否一样,顺序可以不同, 如“aaaabc” 和“cbaaaa”是相同的
![技术分享图片](/img/jia.gif)
![技术分享图片](/img/jian.gif)
// 判断字符串是否由相同的字符组成 public class StringTest { // 方法一 可以死任意字符 O(nlogn) public boolean compareStr(String str1, String str2) { byte[] bs1 = str1.getBytes(); byte[] bs2 = str2.getBytes(); Arrays.sort(bs1); Arrays.sort(bs2); str1 = new String(bs1); str2 = new String(bs2); if (str1.equals(str2)) { returntrue; } else { returnfalse; } } // 只能是ASCII码 方法二 O(n)publicboolean compareStr2(String str1, String str2) { byte[] bs1 = str1.getBytes(); byte[] bs2 = str2.getBytes(); int bCount[] = newint[256]; for (int i = 0; i < bs1.length; i++) bCount[bs1[i] ]++; for (int i = 0; i < bs2.length; i++) bCount[bs2[i] ]--; for (int i = 0; i < 256; i++) { if (bCount[i] != 0) { returnfalse; } } returntrue; } publicstaticvoid main(String[] args) { String str1 = "aaaabbc"; String str2 = "cbaaaab"; System.out.println(new StringTest().compareStr2(str1, str2)); } }
三、字符串中单词的统计
给定一段空格分开的字符串,判断单词的个数
![技术分享图片](/img/jia.gif)
![技术分享图片](/img/jian.gif)
// 字符串中单词的统计 public class StringTest { // O(n) public int wordCount(String str) { int word = 0; int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ‘ ‘) word = 0; elseif (word == 0 ) { word = 1; count++; } } return count; } publicstaticvoid main(String[] args) { String str = "i am a good boy"; System.out.println(new StringTest().wordCount(str)); } }
四、删除字符串中重复的字符
删除字符串中国重复的字符。如good -> god
![技术分享图片](/img/jia.gif)
![技术分享图片](/img/jian.gif)
// 删除字符串中重复的字符 public class StringTest { // O(n^2) public String removeDuplicate(String str) { char[] cs = str.toCharArray(); int n = cs.length; for (int i = 0; i < n; i++) { if (cs[i] == ‘\0‘) continue; for (int j = i + 1; j < n; j++) { if (cs[j] == ‘\0‘) continue; if (cs[i] == cs[j]) cs[j] = ‘\0‘; } } int be = 0; for (int i = 0; i < n; i++) { if (cs[i] != ‘\0‘) cs[be++] =cs[i]; } returnnew String(cs, 0, be); } // 方法二: O(n)public String removeDuplicate2(String str) { char[] cs = str.toCharArray(); int n = cs.length; int count[] = newint[256]; for (int i = 0; i < cs.length; i++) { if (count[cs[i]] != 0) cs[i] = ‘\0‘; count[cs[i]]++; } int be = 0; for (int i = 0; i < n; i++) { if (cs[i] != ‘\0‘) cs[be++] = cs[i]; } returnnew String(cs, 0, be); } publicstaticvoid main(String[] args) { String str = "aaaabbc"; System.out.println(new StringTest().removeDuplicate(str)); } }
五、按要求打印给定数组的排列情况
如1,2,2,3,4,5,要求第四位不为4,3和5不能相连
![技术分享图片](/img/jia.gif)
![技术分享图片](/img/jian.gif)
// 按要求打印数组的排列情况 import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class StringTest { boolean visited[]; String combination = ""; int graph[][] = null; publicvoid getAllCombination(int arr[]) { int n = arr.length; graph = newint[n][n]; visited = newboolean[n]; buildGraph(arr, graph); Set<String> set = new HashSet<>(); for (int i = 0; i < n; i++) { depthFirstSearch(i, set, arr); } Iterator<String> iterator = set.iterator(); while (iterator.hasNext()) { String string = (String) iterator.next(); System.out.println(string); } } /** * 按照深度优先遍历 图,将符合要求的组合加入到set中,自动去重 * * @param start * @param set * @param arr */privatevoid depthFirstSearch(int start, Set<String> set, int arr[]) { visited[start] = true; combination += arr[start]; if (combination.length() == arr.length) { if (combination.indexOf("4") != 2) { set.add(combination); } } for (int j = 0; j < arr.length; j++) { if (graph[start][j] == 1 && visited[j] == false) depthFirstSearch(j, set, arr); } // 什么意思? combination = combination.substring(0, combination.length() - 1); visited[start] = false; } /** * 根据传入的数构建一个图,图中的 3,5 不能相连 * * @param arr * @param graph * @return*/privateint[][] buildGraph(int arr[], int[][] graph) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i] == arr[j]) graph[i][j] = 0; else graph[i][j] = 1; } } graph[3][5] = 0; graph[5][3] = 0; return graph; } publicstaticvoid main(String[] args) { int arr[] = { 1, 2, 2, 3, 4, 5 }; new StringTest().getAllCombination(arr); } }
六、输出字符串的所有组合
给定一个字符串,输出该字符串中字符的所有组合
![技术分享图片](/img/jia.gif)
![技术分享图片](/img/jian.gif)
// 输出字符串的所有组合 public class StringTest { public void combineRecursive(char[] c, int begin, int len, StringBuffer sb) { if (len == 0) { System.out.print(sb + " "); return; } if (begin == c.length) return; sb.append(c[begin]); combineRecursive(c, begin + 1, len - 1, sb); sb.deleteCharAt(sb.length() - 1); combineRecursive(c, begin + 1, len, sb); } publicstaticvoid main(String[] args) { String s = "abc"; char[] cs = s.toCharArray(); StringBuffer sb = new StringBuffer(); for (int j = 1; j < cs.length; j++) { new StringTest().combineRecursive(cs, 0, j, sb); } } }
原文:https://www.cnblogs.com/ytuan996/p/10679288.html
内容总结
以上是互联网集市为您收集整理的[java实现]常见算法之字符串操作全部内容,希望文章能够帮你解决[java实现]常见算法之字符串操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。