左旋转字符串

/**题目描述:
 *定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾
 *部,如把字符串 abcdef 左旋转 2 位得到字符串 cdefab。请实现字符串左旋转的
 *函数, 要求对长度为n的字符串操作的时间复杂度为O(n), 空间复杂度为O(1)。
 * @author binbin
 *

 */


运行结果就有过程了,方法参考了july的方法


package july37;


import java.awt.Adjustable;


/**题目描述:
 *定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾
 *部,如把字符串 abcdef 左旋转 2 位得到字符串 cdefab。请实现字符串左旋转的
 *函数, 要求对长度为n的字符串操作的时间复杂度为O(n), 空间复杂度为O(1)。
 * @author Administrator
 *
 */
public class Arith1 {


<span style="white-space:pre">	</span>public static void main(String[] args) {
<span style="white-space:pre">		</span>// TODO Auto-generated method stub
<span style="white-space:pre">		</span>String str = "abcd";
<span style="white-space:pre">		</span>int k=4;
<span style="white-space:pre">		</span>char[] chars = {'a','b','c','d','e','f','g','h','i','j','k'};
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>//adjust(chars, 0, chars.length, k);
<span style="white-space:pre">		</span>invert(chars, k);
<span style="white-space:pre">		</span>System.out.print("最终结果:");
<span style="white-space:pre">		</span>printChars(chars);
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**方法一,字符串的移位,其中start,end之间的字符才是要调整的
<span style="white-space:pre">	</span> * @param chars<span style="white-space:pre">	</span>字符串
<span style="white-space:pre">	</span> * @param start<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span> * @param end
<span style="white-space:pre">	</span> * @param k
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static int adjust(char[] chars,int start,int end,int k){
<span style="white-space:pre">		</span>int index=start;
<span style="white-space:pre">		</span>while((index+2*k)<=end){//把前k个字符往后移动
<span style="white-space:pre">			</span>System.out.print("把从下标为"+index+"的后"+k+"个字符往后移动"+k+"位");
<span style="white-space:pre">			</span>for(int i = 0;i<k;i++){
<span style="white-space:pre">				</span>changeChar(chars, index, index+k);
<span style="white-space:pre">				</span>index++;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>printChars(chars);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if(index!=end-k){//如果调整没有结果,就把剩下的当做子问题递归解决
<span style="white-space:pre">			</span>int t = end-index-k;
<span style="white-space:pre">			</span>System.out.print("把后"+t+"个字符与下标为"+index+"的后"+t+"个字符,调整后成为子问题:");
<span style="white-space:pre">			</span>for(int i=0;i<t;i++){//把剩下的部分调整一下,让其符合子问题
<span style="white-space:pre">				</span>changeChar(chars, index, index+k);
<span style="white-space:pre">				</span>index++;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>printChars(chars);
<span style="white-space:pre">			</span>//把剩下还没调整的,当作子问题递归解决
<span style="white-space:pre">			</span>adjust(chars, index, end, k-t);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return index;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**方法二,用翻转的方法实现
<span style="white-space:pre">	</span> * @param chars
<span style="white-space:pre">	</span> * @param k
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static void invert(char[] chars,int k){
<span style="white-space:pre">		</span>turnString(chars, 0, k-1);//把前k个逆序
<span style="white-space:pre">		</span>turnString(chars, k, chars.length-1);//把K后面的逆序
<span style="white-space:pre">		</span>turnString(chars, 0, chars.length-1);//把整个字符串逆序
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**指定字串中逆序
<span style="white-space:pre">	</span> * @param chars
<span style="white-space:pre">	</span> * @param start
<span style="white-space:pre">	</span> * @param end
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static void turnString(char[] chars,int start,int end){
<span style="white-space:pre">		</span>int a = start;
<span style="white-space:pre">		</span>int b = end;
<span style="white-space:pre">		</span>while(a<b){
<span style="white-space:pre">			</span>changeChar(chars, a++, b--);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>printChars(chars);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**交换两个字符
<span style="white-space:pre">	</span> * @param chars<span style="white-space:pre">	</span>字符数组
<span style="white-space:pre">	</span> * @param a<span style="white-space:pre">	</span>下标
<span style="white-space:pre">	</span> * @param b
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static void changeChar(char[] chars,int a,int b){
<span style="white-space:pre">		</span>chars[a]=(char) (chars[a]^chars[b]);
<span style="white-space:pre">		</span>chars[b]=(char) (chars[a]^chars[b]);
<span style="white-space:pre">		</span>chars[a]=(char) (chars[a]^chars[b]);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**打印chars字符数组
<span style="white-space:pre">	</span> * @param chars
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static void printChars(char[] chars){
<span style="white-space:pre">		</span>String s = new String(chars);
<span style="white-space:pre">		</span>System.out.println(s);
<span style="white-space:pre">	</span>}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值