reverse array java
/* package whatever; // don't place package name! */ import java.util.*;
import java.lang.*;
import java.io.*; /* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] a = {1,2,4,5};
System.out.println( Arrays.toString(reverseArray(a)));
} public static int[] reverseArray(int[] nums){
int begin = 0;
int end = nums.length -1;
while(begin < end){
swap(nums, begin++, end--);
}
return nums;
} private static int[] swap(int[] nums, int begin, int end){
int temp = nums[begin];
nums[begin] = nums[end];
nums[end] = temp;
return nums;
}
}
reverse array java的更多相关文章
- [Algorithm] Reverse array of Chars by word
For example we have: ["p", "r", "e", "f", "e", &qu ...
- 7. Reverse Integer java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 代码如下: pub ...
- leetcode 153. Find Minimum in Rotated Sorted Array --------- java
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Reverse Integer (JAVA)
public class Solution { public int reverse(int x) { StringBuffer sb = new StringBuffer(x+"" ...
- 【数据结构】算法 LinkList (Reverse LinkedList) Java
反转链表,该链表为单链表. head 节点指向的是头节点. 最简单的方法,就是建一个新链表,将原来链表的节点一个个找到,并且使用头插法插入新链表.时间复杂度也就是O(n),空间复杂度就需要定义2个节点 ...
- leetcode 7. Reverse Integer [java]
public int reverse(int x) { long res = 0; while (x != 0){ res = res* 10 + x % 10; x /= 10; } if(res ...
- Reverse array
数组颠倒算法 #include <iostream> #include <iterator> using namespace std; void reverse(int* A, ...
- CUDA学习,使用shared memory实现Reverse Array
- leetcode 33 Search in Rotated Sorted Array JAVA
题目: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个 ...
随机推荐
- EntityFramework中的线程安全,又是Dictionary
继上次记一次w3wp占用CPU过高的解决过程(Dictionary和线程安全)后又再次与Dictionary博弈,这一次是在EntityFramework中的Dictionary. 从一个异常说起 这 ...
- AngularJs——grunt神器的使用
前面我们已经知道了如何安装grunt,本章节给各位道友介绍如何使用 grunt 的插件,grunt是重点在于如何配置使用 Gruntfile.js,官网上也有很多范例. 1,包装函数 module.e ...
- Xdebug开源PHP程序调试器
Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况. 本文为大家讲解的是在linux下xdebug的安装和配置方法,感兴趣的同学参考下 ...
- 搭建andiord sdk和安装eclipse adt插件的个人小体会
由于以前就已经搭建好了java jdk的运行环境这里就不多说了,不过这几天看了一篇博客才明白jdk变量环境设置的某些要义,不在是人云亦云而不知其所以然. 其博客的地址:http://www.cnblo ...
- OC基础--OC中类的定义
OC中类的定义与使用跟C#和java差别相当明显,做个笔记,牢记并加以区别! 一.OC中类的定义:关键字@implementation 和 @end 注意事项: 1.定义好一个类之后,要让这个类继承N ...
- 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...
- 【OpenJudge1814】 恼人的青蛙 暴力+剪枝优化
此题poj1054上也有 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...
- .net String.Format数字格式化输出
内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充.请留意我增加的 ...
- UvaLive 5026 Building Roads
传送门 Time Limit: 3000MS Description There is a magic planet in the space. There is a magical country ...
- HD2157How many wasy??(十大矩阵问题之八 + 邻接矩阵的应用)
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...